blob: d026fda07cddbc4733df4b0331191ada197da788 [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øgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Juan Zhaoe10d2792012-04-25 19:09:52 +080039enum animation_type {
40 ANIMATION_NONE,
41
42 ANIMATION_ZOOM,
43 ANIMATION_FADE
44};
45
Tiago Vignattibe143262012-04-16 17:31:41 +030046struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050047 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040048
49 struct wl_listener lock_listener;
50 struct wl_listener unlock_listener;
51 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050053 struct weston_layer fullscreen_layer;
54 struct weston_layer panel_layer;
55 struct weston_layer toplevel_layer;
56 struct weston_layer background_layer;
57 struct weston_layer lock_layer;
58
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020059 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050060 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020063
64 unsigned deathcount;
65 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020066 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020067
68 bool locked;
69 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020070
Pekka Paalanen068ae942011-11-28 14:11:15 +020071 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050072 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073
74 struct wl_list backgrounds;
75 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020076
Pekka Paalanen77346a62011-11-30 16:26:35 +020077 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020078 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020079 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020080 struct wl_resource *binding;
81 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020083 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050084
Tiago Vignatti0b52d482012-04-20 18:54:25 +030085 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080086 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050087 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040088};
89
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020091 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050092
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093 SHELL_SURFACE_PANEL,
94 SHELL_SURFACE_BACKGROUND,
95 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020096 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050097
98 SHELL_SURFACE_TOPLEVEL,
99 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800101 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200103};
104
Scott Moreauff1db4a2012-04-17 19:06:18 -0600105struct ping_timer {
106 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600107 uint32_t serial;
108};
109
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_resource resource;
112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300116 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200117
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400118 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400119 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500120 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800121 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800122 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600123 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100124
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500125 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500127 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200128 } rotation;
129
130 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700131 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200133 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100135 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400136 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 } popup;
138
Alex Wu4539b082012-03-01 12:57:46 +0800139 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300140 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400141 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300142 } transient;
143
144 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800145 enum wl_shell_surface_fullscreen_method type;
146 struct weston_transform transform; /* matrix from x, y */
147 uint32_t framerate;
148 struct weston_surface *black_surface;
149 } fullscreen;
150
Scott Moreauff1db4a2012-04-17 19:06:18 -0600151 struct ping_timer *ping_timer;
152
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600153 struct {
154 struct weston_animation current;
155 int exists;
156 int fading_in;
157 uint32_t timestamp;
158 } unresponsive_animation;
159
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500160 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500161 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100162 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200163};
164
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300165struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700166 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300167 struct shell_surface *shsurf;
168 struct wl_listener shsurf_destroy_listener;
169};
170
171struct weston_move_grab {
172 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100173 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174};
175
Pekka Paalanen460099f2012-01-20 16:48:25 +0200176struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300177 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500178 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200179 struct {
180 int32_t x;
181 int32_t y;
182 } center;
183};
184
Alex Wubd3354b2012-04-17 17:20:49 +0800185static struct shell_surface *
186get_shell_surface(struct weston_surface *surface);
187
188static struct desktop_shell *
189shell_surface_get_shell(struct shell_surface *shsurf);
190
191static bool
192shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
193{
194 struct desktop_shell *shell;
195 struct weston_surface *top_fs_es;
196
197 shell = shell_surface_get_shell(shsurf);
198
199 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
200 return false;
201
202 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
203 struct weston_surface,
204 layer_link);
205 return (shsurf == get_shell_surface(top_fs_es));
206}
207
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500208static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400209destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210{
211 struct shell_grab *grab;
212
213 grab = container_of(listener, struct shell_grab,
214 shsurf_destroy_listener);
215
216 grab->shsurf = NULL;
217}
218
219static void
220shell_grab_init(struct shell_grab *grab,
221 const struct wl_pointer_grab_interface *interface,
222 struct shell_surface *shsurf)
223{
224 grab->grab.interface = interface;
225 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400226 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
227 wl_signal_add(&shsurf->resource.destroy_signal,
228 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300229
230}
231
232static void
233shell_grab_finish(struct shell_grab *grab)
234{
235 wl_list_remove(&grab->shsurf_destroy_listener.link);
236}
237
238static void
Alex Wu4539b082012-03-01 12:57:46 +0800239center_on_output(struct weston_surface *surface,
240 struct weston_output *output);
241
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300242static uint32_t
243get_modifier(char *modifier)
244{
245 if (!modifier)
246 return MODIFIER_SUPER;
247
248 if (!strcmp("ctrl", modifier))
249 return MODIFIER_CTRL;
250 else if (!strcmp("alt", modifier))
251 return MODIFIER_ALT;
252 else if (!strcmp("super", modifier))
253 return MODIFIER_SUPER;
254 else
255 return MODIFIER_SUPER;
256}
257
Juan Zhaoe10d2792012-04-25 19:09:52 +0800258static enum animation_type
259get_animation_type(char *animation)
260{
261 if (!animation)
262 return ANIMATION_NONE;
263
264 if (!strcmp("zoom", animation))
265 return ANIMATION_ZOOM;
266 else if (!strcmp("fade", animation))
267 return ANIMATION_FADE;
268 else
269 return ANIMATION_NONE;
270}
271
Alex Wu4539b082012-03-01 12:57:46 +0800272static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300273shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200274{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200275 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200276 char *path = NULL;
277 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300278 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800279 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200280
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400281 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300282 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800283 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200284 };
285
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400286 struct config_key saver_keys[] = {
287 { "path", CONFIG_KEY_STRING, &path },
288 { "duration", CONFIG_KEY_INTEGER, &duration },
289 };
290
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200291 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400292 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200293 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
294 };
295
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200296 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500297 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200298 free(config_file);
299
Pekka Paalanen7296e792011-12-07 16:22:00 +0200300 shell->screensaver.path = path;
301 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300302 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800303 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200304}
305
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200306static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400307noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100308 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500309{
310 grab->focus = NULL;
311}
312
313static void
Scott Moreau447013d2012-02-18 05:05:29 -0700314move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100315 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500316{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100318 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319 struct shell_surface *shsurf = move->base.shsurf;
320 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100321 int dx = wl_fixed_to_int(pointer->x + move->dx);
322 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323
324 if (!shsurf)
325 return;
326
327 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500328
Daniel Stone103db7f2012-05-08 17:17:55 +0100329 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200330 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500331}
332
333static void
Scott Moreau447013d2012-02-18 05:05:29 -0700334move_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100335 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500336{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
338 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100339 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500340
Daniel Stone37816df2012-05-16 18:45:18 +0100341 if (pointer->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100343 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500344 free(grab);
345 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500346}
347
Scott Moreau447013d2012-02-18 05:05:29 -0700348static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500349 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500350 move_grab_motion,
351 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500352};
353
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600354static void
355unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
356{
357 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
358
359 if(!shsurf->unresponsive_animation.exists) {
360 wl_list_insert(&shsurf->surface->compositor->animation_list,
361 &shsurf->unresponsive_animation.current.link);
362 shsurf->unresponsive_animation.exists = 1;
363 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
364 weston_surface_damage(shsurf->surface);
365 }
366}
367
368static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600369unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600370 struct weston_output *output, uint32_t msecs)
371{
372 struct shell_surface *shsurf =
373 container_of(animation, struct shell_surface, unresponsive_animation.current);
374 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600375 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600376
377 if (!surface || !shsurf)
378 return;
379
380 if (shsurf->unresponsive_animation.fading_in) {
381 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
382 if (surface->saturation > 1)
383 surface->saturation -= 5;
384 if (surface->brightness > 200)
385 surface->brightness--;
386
387 shsurf->unresponsive_animation.timestamp += step;
388 }
389
390 if (surface->saturation <= 1 && surface->brightness <= 200) {
391 wl_list_remove(&shsurf->unresponsive_animation.current.link);
392 shsurf->unresponsive_animation.exists = 0;
393 }
394 }
395 else {
396 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
397 if (surface->saturation < 255)
398 surface->saturation += 5;
399 if (surface->brightness < 255)
400 surface->brightness++;
401
402 shsurf->unresponsive_animation.timestamp += step;
403 }
404
405 if (surface->saturation >= 255 && surface->brightness >= 255) {
406 surface->saturation = surface->brightness = 255;
407 wl_list_remove(&shsurf->unresponsive_animation.current.link);
408 shsurf->unresponsive_animation.exists = 0;
409 }
410 }
411
412 surface->geometry.dirty = 1;
413 weston_surface_damage(surface);
414}
415
Scott Moreau9521d5e2012-04-19 13:06:17 -0600416static void
417ping_timer_destroy(struct shell_surface *shsurf)
418{
419 if (!shsurf || !shsurf->ping_timer)
420 return;
421
422 if (shsurf->ping_timer->source)
423 wl_event_source_remove(shsurf->ping_timer->source);
424
425 free(shsurf->ping_timer);
426 shsurf->ping_timer = NULL;
427}
428
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400429static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600430ping_timeout_handler(void *data)
431{
432 struct shell_surface *shsurf = data;
433
Scott Moreau9521d5e2012-04-19 13:06:17 -0600434 /* Client is not responding */
435 shsurf->unresponsive = 1;
436 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600437
438 return 1;
439}
440
441static void
442ping_handler(struct weston_surface *surface, uint32_t serial)
443{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400444 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600445 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600446 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600447
448 if (!shsurf)
449 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400450 if (!shsurf->resource.client)
451 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600452
453 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300454 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600455 if (!shsurf->ping_timer)
456 return;
457
458 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600459 loop = wl_display_get_event_loop(surface->compositor->wl_display);
460 shsurf->ping_timer->source =
461 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
462 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
463
464 wl_shell_surface_send_ping(&shsurf->resource, serial);
465 }
466}
467
468static void
469shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
470 uint32_t serial)
471{
472 struct shell_surface *shsurf = resource->data;
473
Scott Moreauff1db4a2012-04-17 19:06:18 -0600474 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600475 if (shsurf->unresponsive) {
476 /* Received pong from previously unresponsive client */
477 unresponsive_surface_fade(shsurf, true);
478 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600479 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600480 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600481 }
482}
483
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400484static void
485shell_surface_set_title(struct wl_client *client,
486 struct wl_resource *resource, const char *title)
487{
488 struct shell_surface *shsurf = resource->data;
489
490 free(shsurf->title);
491 shsurf->title = strdup(title);
492}
493
494static void
495shell_surface_set_class(struct wl_client *client,
496 struct wl_resource *resource, const char *class)
497{
498 struct shell_surface *shsurf = resource->data;
499
500 free(shsurf->class);
501 shsurf->class = strdup(class);
502}
503
Scott Moreauff1db4a2012-04-17 19:06:18 -0600504static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400505surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500507 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300508
509 if (!shsurf)
510 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500511
512 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400513 if (!move)
514 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500515
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300516 shell_grab_init(&move->base, &move_grab_interface, shsurf);
517
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400518 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100519 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400520 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100521 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500522
Daniel Stone37816df2012-05-16 18:45:18 +0100523 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500524
Daniel Stone37816df2012-05-16 18:45:18 +0100525 wl_pointer_set_focus(ws->seat.pointer, NULL,
526 wl_fixed_from_int(0),
527 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400528
529 return 0;
530}
531
532static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200533shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100534 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400535{
Daniel Stone37816df2012-05-16 18:45:18 +0100536 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200537 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400538
Daniel Stone37816df2012-05-16 18:45:18 +0100539 if (ws->seat.pointer->button_count == 0 ||
540 ws->seat.pointer->grab_serial != serial ||
541 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500542 return;
543
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400544 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400545 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500546}
547
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500548struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300549 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500550 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200551 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500552};
553
554static void
Scott Moreau447013d2012-02-18 05:05:29 -0700555resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100556 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500557{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500558 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100559 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500560 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100561 wl_fixed_t from_x, from_y;
562 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200563
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300564 if (!resize->base.shsurf)
565 return;
566
Daniel Stone103db7f2012-05-08 17:17:55 +0100567 weston_surface_from_global_fixed(resize->base.shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100568 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100569 &from_x, &from_y);
570 weston_surface_from_global_fixed(resize->base.shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100571 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500572
Daniel Stone103db7f2012-05-08 17:17:55 +0100573 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200574 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100575 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200576 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100577 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500578 }
579
Daniel Stone103db7f2012-05-08 17:17:55 +0100580 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200581 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100582 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200583 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100584 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500585 }
586
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300587 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400588 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500589}
590
591static void
Scott Moreau447013d2012-02-18 05:05:29 -0700592resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100593 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500594{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300595 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100596 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500597
Daniel Stone37816df2012-05-16 18:45:18 +0100598 if (pointer->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300599 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100600 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500601 free(grab);
602 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500603}
604
Scott Moreau447013d2012-02-18 05:05:29 -0700605static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500606 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500607 resize_grab_motion,
608 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500609};
610
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400611static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612weston_surface_resize(struct shell_surface *shsurf,
Daniel Stone37816df2012-05-16 18:45:18 +0100613 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500614{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500615 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500616
Alex Wu4539b082012-03-01 12:57:46 +0800617 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
618 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500619
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200620 if (edges == 0 || edges > 15 ||
621 (edges & 3) == 3 || (edges & 12) == 12)
622 return 0;
623
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500624 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400625 if (!resize)
626 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500627
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300628 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
629
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500630 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200631 resize->width = shsurf->surface->geometry.width;
632 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400633
Daniel Stone37816df2012-05-16 18:45:18 +0100634 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500635
Daniel Stone37816df2012-05-16 18:45:18 +0100636 wl_pointer_set_focus(ws->seat.pointer, NULL,
637 wl_fixed_from_int(0),
638 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400639
640 return 0;
641}
642
643static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200644shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100645 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200646 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400647{
Daniel Stone37816df2012-05-16 18:45:18 +0100648 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200649 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400650
Alex Wu4539b082012-03-01 12:57:46 +0800651 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
652 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400653
Daniel Stone37816df2012-05-16 18:45:18 +0100654 if (ws->seat.pointer->button_count == 0 ||
655 ws->seat.pointer->grab_serial != serial ||
656 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500657 return;
658
Daniel Stone37816df2012-05-16 18:45:18 +0100659 if (weston_surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400660 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500661}
662
Juan Zhao96879df2012-02-07 08:45:41 +0800663static struct weston_output *
664get_default_output(struct weston_compositor *compositor)
665{
666 return container_of(compositor->output_list.next,
667 struct weston_output, link);
668}
669
Alex Wu4539b082012-03-01 12:57:46 +0800670static void
671shell_unset_fullscreen(struct shell_surface *shsurf)
672{
673 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800674 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
675 shell_surface_is_top_fullscreen(shsurf)) {
676 weston_output_switch_mode(shsurf->fullscreen_output,
677 shsurf->fullscreen_output->origin);
678 }
Alex Wu4539b082012-03-01 12:57:46 +0800679 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
680 shsurf->fullscreen.framerate = 0;
681 wl_list_remove(&shsurf->fullscreen.transform.link);
682 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800683 if (shsurf->fullscreen.black_surface)
684 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800685 shsurf->fullscreen.black_surface = NULL;
686 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800687 weston_surface_set_position(shsurf->surface,
688 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800689 if (shsurf->saved_rotation_valid) {
690 wl_list_insert(&shsurf->surface->geometry.transformation_list,
691 &shsurf->rotation.transform.link);
692 shsurf->saved_rotation_valid = false;
693 }
Alex Wu4539b082012-03-01 12:57:46 +0800694}
695
Pekka Paalanen98262232011-12-01 10:42:22 +0200696static int
697reset_shell_surface_type(struct shell_surface *surface)
698{
699 switch (surface->type) {
700 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800701 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200702 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800703 case SHELL_SURFACE_MAXIMIZED:
704 surface->output = get_default_output(surface->surface->compositor);
705 weston_surface_set_position(surface->surface,
706 surface->saved_x,
707 surface->saved_y);
708 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200709 case SHELL_SURFACE_PANEL:
710 case SHELL_SURFACE_BACKGROUND:
711 wl_list_remove(&surface->link);
712 wl_list_init(&surface->link);
713 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200714 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200715 case SHELL_SURFACE_LOCK:
716 wl_resource_post_error(&surface->resource,
717 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200718 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200719 return -1;
720 case SHELL_SURFACE_NONE:
721 case SHELL_SURFACE_TOPLEVEL:
722 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500723 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200724 break;
725 }
726
727 surface->type = SHELL_SURFACE_NONE;
728 return 0;
729}
730
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500731static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400732set_surface_type(struct shell_surface *shsurf)
733{
734 struct weston_surface *surface = shsurf->surface;
735 struct shell_surface *pshsurf = shsurf->parent;
736 struct weston_surface *pes;
737 struct shell_surface *priv;
738 struct desktop_shell *shell = shsurf->shell;
739
740 reset_shell_surface_type(shsurf);
741
742 shsurf->type = shsurf->next_type;
743 shsurf->next_type = SHELL_SURFACE_NONE;
744
745 switch (shsurf->type) {
746 case SHELL_SURFACE_TOPLEVEL:
747 break;
748 case SHELL_SURFACE_TRANSIENT:
749 pes = pshsurf->surface;
750 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300751 pes->geometry.x + shsurf->transient.x,
752 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400753 break;
754
755 case SHELL_SURFACE_MAXIMIZED:
756 shsurf->saved_x = surface->geometry.x;
757 shsurf->saved_y = surface->geometry.y;
758 shsurf->saved_position_valid = true;
759 break;
760
761 case SHELL_SURFACE_FULLSCREEN:
762 shsurf->saved_x = surface->geometry.x;
763 shsurf->saved_y = surface->geometry.y;
764 shsurf->saved_position_valid = true;
765
766 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
767 wl_list_remove(&shsurf->rotation.transform.link);
768 wl_list_init(&shsurf->rotation.transform.link);
769 shsurf->surface->geometry.dirty = 1;
770 shsurf->saved_rotation_valid = true;
771 }
772 break;
773
774 case SHELL_SURFACE_BACKGROUND:
775 wl_list_for_each(priv, &shell->backgrounds, link) {
776 if (priv->output == shsurf->output) {
777 priv->surface->output = NULL;
778 wl_list_remove(&priv->surface->layer_link);
779 wl_list_remove(&priv->link);
780 break;
781 }
782 }
783
784 wl_list_insert(&shell->backgrounds, &shsurf->link);
785
786 weston_surface_set_position(surface, shsurf->output->x,
787 shsurf->output->y);
788 break;
789
790 case SHELL_SURFACE_PANEL:
791 wl_list_for_each(priv, &shell->panels, link) {
792 if (priv->output == shsurf->output) {
793 priv->surface->output = NULL;
794 wl_list_remove(&priv->surface->layer_link);
795 wl_list_remove(&priv->link);
796 break;
797 }
798 }
799
800 wl_list_insert(&shell->panels, &shsurf->link);
801
802 weston_surface_set_position(surface, shsurf->output->x,
803 shsurf->output->y);
804 break;
805
806 default:
807 break;
808 }
809}
810
811static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300812set_toplevel(struct shell_surface *shsurf)
813{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400814 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300815}
816
817static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200818shell_surface_set_toplevel(struct wl_client *client,
819 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400820{
Pekka Paalanen98262232011-12-01 10:42:22 +0200821 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400822
Tiago Vignattibc052c92012-04-19 16:18:18 +0300823 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400824}
825
826static void
Tiago Vignatti491bac12012-05-18 16:37:43 -0400827set_transient(struct shell_surface *shsurf,
828 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
829{
830 /* assign to parents output */
831 shsurf->parent = pshsurf;
832 shsurf->transient.x = x;
833 shsurf->transient.y = y;
834 shsurf->transient.flags = flags;
835 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
836}
837
838static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200839shell_surface_set_transient(struct wl_client *client,
840 struct wl_resource *resource,
841 struct wl_resource *parent_resource,
842 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400843{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200844 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400845 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200846
Tiago Vignatti491bac12012-05-18 16:37:43 -0400847 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400848}
849
Tiago Vignattibe143262012-04-16 17:31:41 +0300850static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800851shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200852{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400853 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800854}
855
856static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300857get_output_panel_height(struct desktop_shell *shell,
858 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800859{
860 struct shell_surface *priv;
861 int panel_height = 0;
862
863 if (!output)
864 return 0;
865
Tiago Vignattibe143262012-04-16 17:31:41 +0300866 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800867 if (priv->output == output) {
868 panel_height = priv->surface->geometry.height;
869 break;
870 }
871 }
872 return panel_height;
873}
874
875static void
876shell_surface_set_maximized(struct wl_client *client,
877 struct wl_resource *resource,
878 struct wl_resource *output_resource )
879{
880 struct shell_surface *shsurf = resource->data;
881 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300882 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800883 uint32_t edges = 0, panel_height = 0;
884
885 /* get the default output, if the client set it as NULL
886 check whether the ouput is available */
887 if (output_resource)
888 shsurf->output = output_resource->data;
889 else
890 shsurf->output = get_default_output(es->compositor);
891
Tiago Vignattibe143262012-04-16 17:31:41 +0300892 shell = shell_surface_get_shell(shsurf);
893 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800894 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500895
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400896 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500897 es->output->current->width,
898 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800899
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400900 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200901}
902
Alex Wu21858432012-04-01 20:13:08 +0800903static void
904black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
905
Alex Wu4539b082012-03-01 12:57:46 +0800906static struct weston_surface *
907create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800908 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800909 GLfloat x, GLfloat y, int w, int h)
910{
911 struct weston_surface *surface = NULL;
912
913 surface = weston_surface_create(ec);
914 if (surface == NULL) {
915 fprintf(stderr, "no memory\n");
916 return NULL;
917 }
918
Alex Wu21858432012-04-01 20:13:08 +0800919 surface->configure = black_surface_configure;
920 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800921 weston_surface_configure(surface, x, y, w, h);
922 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
923 return surface;
924}
925
926/* Create black surface and append it to the associated fullscreen surface.
927 * Handle size dismatch and positioning according to the method. */
928static void
929shell_configure_fullscreen(struct shell_surface *shsurf)
930{
931 struct weston_output *output = shsurf->fullscreen_output;
932 struct weston_surface *surface = shsurf->surface;
933 struct weston_matrix *matrix;
934 float scale;
935
936 center_on_output(surface, output);
937
938 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500939 shsurf->fullscreen.black_surface =
940 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800941 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500942 output->x, output->y,
943 output->current->width,
944 output->current->height);
945
946 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
947 wl_list_insert(&surface->layer_link,
948 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800949 shsurf->fullscreen.black_surface->output = output;
950
951 switch (shsurf->fullscreen.type) {
952 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
953 break;
954 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
955 matrix = &shsurf->fullscreen.transform.matrix;
956 weston_matrix_init(matrix);
957 scale = (float)output->current->width/(float)surface->geometry.width;
958 weston_matrix_scale(matrix, scale, scale, 1);
959 wl_list_remove(&shsurf->fullscreen.transform.link);
960 wl_list_insert(surface->geometry.transformation_list.prev,
961 &shsurf->fullscreen.transform.link);
962 weston_surface_set_position(surface, output->x, output->y);
963 break;
964 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800965 if (shell_surface_is_top_fullscreen(shsurf)) {
966 struct weston_mode mode = {0,
967 surface->geometry.width,
968 surface->geometry.height,
969 shsurf->fullscreen.framerate};
970
971 if (weston_output_switch_mode(output, &mode) == 0) {
972 weston_surface_configure(shsurf->fullscreen.black_surface,
973 output->x, output->y,
974 output->current->width,
975 output->current->height);
976 weston_surface_set_position(surface, output->x, output->y);
977 break;
978 }
979 }
Alex Wu4539b082012-03-01 12:57:46 +0800980 break;
981 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
982 break;
983 default:
984 break;
985 }
986}
987
988/* make the fullscreen and black surface at the top */
989static void
990shell_stack_fullscreen(struct shell_surface *shsurf)
991{
Alex Wubd3354b2012-04-17 17:20:49 +0800992 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800993 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300994 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800995
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500996 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500997 wl_list_insert(&shell->fullscreen_layer.surface_list,
998 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800999 weston_surface_damage(surface);
1000
1001 if (!shsurf->fullscreen.black_surface)
1002 shsurf->fullscreen.black_surface =
1003 create_black_surface(surface->compositor,
1004 surface,
1005 output->x, output->y,
1006 output->current->width,
1007 output->current->height);
1008
1009 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001010 wl_list_insert(&surface->layer_link,
1011 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001012 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001013}
1014
1015static void
1016shell_map_fullscreen(struct shell_surface *shsurf)
1017{
Alex Wu4539b082012-03-01 12:57:46 +08001018 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001019 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001020}
1021
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001022static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001023shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001024 struct wl_resource *resource,
1025 uint32_t method,
1026 uint32_t framerate,
1027 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001028{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001029 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001030 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001031
1032 if (output_resource)
1033 shsurf->output = output_resource->data;
1034 else
1035 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001036
Alex Wu4539b082012-03-01 12:57:46 +08001037 shsurf->fullscreen_output = shsurf->output;
1038 shsurf->fullscreen.type = method;
1039 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001040 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001041
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001042 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001043 shsurf->output->current->width,
1044 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001045}
1046
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001047static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001048popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001049 struct wl_surface *surface,
1050 wl_fixed_t x,
1051 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001052{
Daniel Stone37816df2012-05-16 18:45:18 +01001053 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001054 struct shell_surface *priv =
1055 container_of(grab, struct shell_surface, popup.grab);
1056 struct wl_client *client = priv->surface->surface.resource.client;
1057
Pekka Paalanencb108432012-01-19 16:25:40 +02001058 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001059 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001060 grab->focus = surface;
1061 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001062 wl_pointer_set_focus(pointer, NULL,
1063 wl_fixed_from_int(0),
1064 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001065 grab->focus = NULL;
1066 }
1067}
1068
1069static void
Scott Moreau447013d2012-02-18 05:05:29 -07001070popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001071 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001072{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001073 struct wl_resource *resource;
1074
Daniel Stone37816df2012-05-16 18:45:18 +01001075 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001076 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001077 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001078}
1079
1080static void
Scott Moreau447013d2012-02-18 05:05:29 -07001081popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001082 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001083{
1084 struct wl_resource *resource;
1085 struct shell_surface *shsurf =
1086 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001087 struct wl_display *display;
1088 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001089
Daniel Stone37816df2012-05-16 18:45:18 +01001090 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001091 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001092 display = wl_client_get_display(resource->client);
1093 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001094 wl_pointer_send_button(resource, serial, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001095 } else if (state == 0 &&
1096 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001097 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001098 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001099 wl_pointer_end_grab(grab->pointer);
1100 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001101 }
1102
1103 if (state == 0)
1104 shsurf->popup.initial_up = 1;
1105}
1106
Scott Moreau447013d2012-02-18 05:05:29 -07001107static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001108 popup_grab_focus,
1109 popup_grab_motion,
1110 popup_grab_button,
1111};
1112
1113static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001114shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001115{
Daniel Stone37816df2012-05-16 18:45:18 +01001116 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001117 struct weston_surface *es = shsurf->surface;
1118 struct weston_surface *parent = shsurf->parent->surface;
1119
1120 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001121 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001122
Pekka Paalanen938269a2012-02-07 14:19:01 +02001123 weston_surface_update_transform(parent);
1124 if (parent->transform.enabled) {
1125 shsurf->popup.parent_transform.matrix =
1126 parent->transform.matrix;
1127 } else {
1128 /* construct x, y translation matrix */
1129 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1130 shsurf->popup.parent_transform.matrix.d[12] =
1131 parent->geometry.x;
1132 shsurf->popup.parent_transform.matrix.d[13] =
1133 parent->geometry.y;
1134 }
1135 wl_list_insert(es->geometry.transformation_list.prev,
1136 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001137 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001138
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001139 shsurf->popup.initial_up = 0;
1140
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001141 /* We don't require the grab to still be active, but if another
1142 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001143 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1144 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001145 } else {
1146 wl_shell_surface_send_popup_done(&shsurf->resource);
1147 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001148}
1149
1150static void
1151shell_surface_set_popup(struct wl_client *client,
1152 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001153 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001154 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001155 struct wl_resource *parent_resource,
1156 int32_t x, int32_t y, uint32_t flags)
1157{
1158 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001159
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001160 shsurf->type = SHELL_SURFACE_POPUP;
1161 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001162 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001163 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001164 shsurf->popup.x = x;
1165 shsurf->popup.y = y;
1166}
1167
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001169 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001170 shell_surface_move,
1171 shell_surface_resize,
1172 shell_surface_set_toplevel,
1173 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001174 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001175 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001176 shell_surface_set_maximized,
1177 shell_surface_set_title,
1178 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001179};
1180
1181static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001182destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001183{
Daniel Stone37816df2012-05-16 18:45:18 +01001184 if (shsurf->popup.grab.pointer)
1185 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001186
Alex Wubd3354b2012-04-17 17:20:49 +08001187 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1188 shell_surface_is_top_fullscreen(shsurf)) {
1189 weston_output_switch_mode(shsurf->fullscreen_output,
1190 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001191 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001192
Alex Wuaa08e2d2012-03-05 11:01:40 +08001193 if (shsurf->fullscreen.black_surface)
1194 weston_surface_destroy(shsurf->fullscreen.black_surface);
1195
Alex Wubd3354b2012-04-17 17:20:49 +08001196 /* As destroy_resource() use wl_list_for_each_safe(),
1197 * we can always remove the listener.
1198 */
1199 wl_list_remove(&shsurf->surface_destroy_listener.link);
1200 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001201 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001202
Rob Bradford579f2932012-05-21 18:04:15 +01001203 if (shsurf->unresponsive_animation.exists)
1204 wl_list_remove(&shsurf->unresponsive_animation.current.link);
1205
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001206 wl_list_remove(&shsurf->link);
1207 free(shsurf);
1208}
1209
1210static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001211shell_destroy_shell_surface(struct wl_resource *resource)
1212{
1213 struct shell_surface *shsurf = resource->data;
1214
1215 destroy_shell_surface(shsurf);
1216}
1217
1218static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001219shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001220{
1221 struct shell_surface *shsurf = container_of(listener,
1222 struct shell_surface,
1223 surface_destroy_listener);
1224
Tiago Vignattibc052c92012-04-19 16:18:18 +03001225 /* tricky way to check if resource was in fact created */
1226 if (shsurf->resource.object.implementation != 0)
1227 wl_resource_destroy(&shsurf->resource);
1228 else
1229 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001230}
1231
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001232static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001233get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001234{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001235 struct wl_listener *listener;
1236
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001237 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1238 shell_handle_surface_destroy);
1239 if (listener)
1240 return container_of(listener, struct shell_surface,
1241 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001242
1243 return NULL;
1244}
1245
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001246static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001247shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1248
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001249static struct shell_surface *
1250create_shell_surface(void *shell, struct weston_surface *surface)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001251{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001252 struct shell_surface *shsurf;
1253
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001254 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001255 fprintf(stderr, "surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001256 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001257 }
1258
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001259 shsurf = calloc(1, sizeof *shsurf);
1260 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001261 fprintf(stderr, "no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001262 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001263 }
1264
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001265 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001266 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001267
Tiago Vignattibc052c92012-04-19 16:18:18 +03001268 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001269 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001270 shsurf->unresponsive_animation.exists = 0;
1271 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001272 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001273 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001274 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001275 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001276 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1277 shsurf->fullscreen.framerate = 0;
1278 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001279 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001280 wl_list_init(&shsurf->fullscreen.transform.link);
1281
Tiago Vignattibc052c92012-04-19 16:18:18 +03001282 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001283 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1284 wl_signal_add(&surface->surface.resource.destroy_signal,
1285 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001286
1287 /* init link so its safe to always remove it in destroy_shell_surface */
1288 wl_list_init(&shsurf->link);
1289
Pekka Paalanen460099f2012-01-20 16:48:25 +02001290 /* empty when not in use */
1291 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001292 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001293
Pekka Paalanen98262232011-12-01 10:42:22 +02001294 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001295 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001296
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001297 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001298}
1299
1300static void
1301shell_get_shell_surface(struct wl_client *client,
1302 struct wl_resource *resource,
1303 uint32_t id,
1304 struct wl_resource *surface_resource)
1305{
1306 struct weston_surface *surface = surface_resource->data;
1307 struct desktop_shell *shell = resource->data;
1308 struct shell_surface *shsurf;
1309
1310 if (get_shell_surface(surface)) {
1311 wl_resource_post_error(surface_resource,
1312 WL_DISPLAY_ERROR_INVALID_OBJECT,
1313 "desktop_shell::get_shell_surface already requested");
1314 return;
1315 }
1316
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001317 shsurf = create_shell_surface(shell, surface);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001318 if (!shsurf) {
1319 wl_resource_post_error(surface_resource,
1320 WL_DISPLAY_ERROR_INVALID_OBJECT,
1321 "surface->configure already set");
1322 return;
1323 }
1324
1325 shsurf->resource.destroy = shell_destroy_shell_surface;
1326 shsurf->resource.object.id = id;
1327 shsurf->resource.object.interface = &wl_shell_surface_interface;
1328 shsurf->resource.object.implementation =
1329 (void (**)(void)) &shell_surface_implementation;
1330 shsurf->resource.data = shsurf;
1331
1332 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001333}
1334
1335static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001336 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001337};
1338
Kristian Høgsberg07937562011-04-12 17:25:42 -04001339static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001340handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001341{
1342 proc->pid = 0;
1343}
1344
1345static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001346launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001347{
1348 if (shell->screensaver.binding)
1349 return;
1350
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001351 if (!shell->screensaver.path)
1352 return;
1353
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001354 if (shell->screensaver.process.pid != 0) {
1355 fprintf(stderr, "old screensaver still running\n");
1356 return;
1357 }
1358
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001360 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001361 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001362 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001363}
1364
1365static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001366terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001367{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001368 if (shell->screensaver.process.pid == 0)
1369 return;
1370
1371 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001372}
1373
1374static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001375show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001376{
1377 struct wl_list *list;
1378
1379 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001380 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001381 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001382 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001383
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001384 wl_list_remove(&surface->surface->layer_link);
1385 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001386 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001387 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001388}
1389
1390static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001391hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001392{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001393 wl_list_remove(&surface->surface->layer_link);
1394 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001395 surface->surface->output = NULL;
1396}
1397
1398static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001399desktop_shell_set_background(struct wl_client *client,
1400 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001401 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001402 struct wl_resource *surface_resource)
1403{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001404 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001405
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001406 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001407 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001408
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001409 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001410 surface_resource,
1411 shsurf->output->current->width,
1412 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001413}
1414
1415static void
1416desktop_shell_set_panel(struct wl_client *client,
1417 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001418 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001419 struct wl_resource *surface_resource)
1420{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001421 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001422
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001423 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001424 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001425
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001426 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001427 surface_resource,
1428 shsurf->output->current->width,
1429 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001430}
1431
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001432static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001433handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001434{
Tiago Vignattibe143262012-04-16 17:31:41 +03001435 struct desktop_shell *shell =
1436 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001437
1438 fprintf(stderr, "lock surface gone\n");
1439 shell->lock_surface = NULL;
1440}
1441
1442static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001443desktop_shell_set_lock_surface(struct wl_client *client,
1444 struct wl_resource *resource,
1445 struct wl_resource *surface_resource)
1446{
Tiago Vignattibe143262012-04-16 17:31:41 +03001447 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001448 struct shell_surface *surface = surface_resource->data;
1449
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001450 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001451
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001452 if (!shell->locked)
1453 return;
1454
Pekka Paalanen98262232011-12-01 10:42:22 +02001455 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001456
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001457 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1458 wl_signal_add(&surface_resource->destroy_signal,
1459 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001460
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001461 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001462}
1463
1464static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001465resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001466{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001467 struct shell_surface *tmp;
1468
1469 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1470 hide_screensaver(shell, tmp);
1471
1472 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001473
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001474 wl_list_remove(&shell->lock_layer.link);
1475 wl_list_insert(&shell->compositor->cursor_layer.link,
1476 &shell->fullscreen_layer.link);
1477 wl_list_insert(&shell->fullscreen_layer.link,
1478 &shell->panel_layer.link);
1479 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001480
1481 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001482 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001484 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001485}
1486
1487static void
1488desktop_shell_unlock(struct wl_client *client,
1489 struct wl_resource *resource)
1490{
Tiago Vignattibe143262012-04-16 17:31:41 +03001491 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001492
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001493 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001494
1495 if (shell->locked)
1496 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001497}
1498
Kristian Høgsberg75840622011-09-06 13:48:16 -04001499static const struct desktop_shell_interface desktop_shell_implementation = {
1500 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001501 desktop_shell_set_panel,
1502 desktop_shell_set_lock_surface,
1503 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001504};
1505
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001506static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001508{
1509 struct shell_surface *shsurf;
1510
1511 shsurf = get_shell_surface(surface);
1512 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001513 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001514 return shsurf->type;
1515}
1516
Kristian Høgsberg75840622011-09-06 13:48:16 -04001517static void
Daniel Stone37816df2012-05-16 18:45:18 +01001518move_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001519 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1520 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001521{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001522 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001523 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001524 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001525
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001526 if (surface == NULL)
1527 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001528
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001529 shsurf = get_shell_surface(surface);
1530 if (shsurf == NULL)
1531 return;
1532
1533 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001534 case SHELL_SURFACE_PANEL:
1535 case SHELL_SURFACE_BACKGROUND:
1536 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001537 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001538 return;
1539 default:
1540 break;
1541 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001542
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001543 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001544}
1545
1546static void
Daniel Stone37816df2012-05-16 18:45:18 +01001547resize_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001548 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1549 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001550{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001551 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001552 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001553 uint32_t edges = 0;
1554 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001555 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001556
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001557 if (surface == NULL)
1558 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001559
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001560 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001561 if (!shsurf)
1562 return;
1563
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001564 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001565 case SHELL_SURFACE_PANEL:
1566 case SHELL_SURFACE_BACKGROUND:
1567 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001568 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001569 return;
1570 default:
1571 break;
1572 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001573
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001574 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001575 wl_fixed_to_int(seat->pointer->grab_x),
1576 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001577 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001578
Pekka Paalanen60921e52012-01-25 15:55:43 +02001579 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001580 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001581 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001582 edges |= 0;
1583 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001584 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001585
Pekka Paalanen60921e52012-01-25 15:55:43 +02001586 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001587 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001588 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001589 edges |= 0;
1590 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001591 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001592
Daniel Stone37816df2012-05-16 18:45:18 +01001593 weston_surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001594}
1595
1596static void
Daniel Stone37816df2012-05-16 18:45:18 +01001597surface_opacity_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001598 uint32_t key, uint32_t button, uint32_t axis,
1599 int32_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001600{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001601 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001602 struct shell_surface *shsurf;
1603 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001604 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001605
1606 if (surface == NULL)
1607 return;
1608
1609 shsurf = get_shell_surface(surface);
1610 if (!shsurf)
1611 return;
1612
1613 switch (shsurf->type) {
1614 case SHELL_SURFACE_BACKGROUND:
1615 case SHELL_SURFACE_SCREENSAVER:
1616 return;
1617 default:
1618 break;
1619 }
1620
1621 surface->alpha += value * step;
1622
1623 if (surface->alpha > 255)
1624 surface->alpha = 255;
1625 if (surface->alpha < step)
1626 surface->alpha = step;
1627
1628 surface->geometry.dirty = 1;
1629 weston_surface_damage(surface);
1630}
1631
1632static void
Daniel Stone37816df2012-05-16 18:45:18 +01001633zoom_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001634 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1635 void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001636{
Daniel Stone37816df2012-05-16 18:45:18 +01001637 struct weston_seat *ws = (struct weston_seat *) seat;
1638 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001639 struct weston_output *output;
1640
1641 wl_list_for_each(output, &compositor->output_list, link) {
1642 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01001643 wl_fixed_to_double(seat->pointer->x),
1644 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001645 NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001646 output->zoom.active = 1;
1647 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001648
1649 if (output->zoom.level >= 1.0) {
1650 output->zoom.active = 0;
1651 output->zoom.level = 1.0;
1652 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001653
1654 if (output->zoom.level < output->zoom.increment)
1655 output->zoom.level = output->zoom.increment;
1656
Daniel Stone37816df2012-05-16 18:45:18 +01001657 weston_output_update_zoom(output,
1658 seat->pointer->x,
1659 seat->pointer->y);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001660 }
1661 }
1662}
1663
Scott Moreauccbf29d2012-02-22 14:21:41 -07001664static void
Daniel Stone37816df2012-05-16 18:45:18 +01001665terminate_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001666 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001667{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001669
1670 if (state)
1671 wl_display_terminate(compositor->wl_display);
1672}
1673
1674static void
Scott Moreau447013d2012-02-18 05:05:29 -07001675rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001676 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001677{
1678 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001679 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001680 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001681 struct shell_surface *shsurf = rotate->base.shsurf;
1682 struct weston_surface *surface;
1683 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1684
1685 if (!shsurf)
1686 return;
1687
1688 surface = shsurf->surface;
1689
1690 cx = 0.5f * surface->geometry.width;
1691 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001692
Daniel Stone37816df2012-05-16 18:45:18 +01001693 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1694 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001695 r = sqrtf(dx * dx + dy * dy);
1696
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001697 wl_list_remove(&shsurf->rotation.transform.link);
1698 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001699
1700 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001701 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001702 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001703
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001704 weston_matrix_init(&rotate->rotation);
1705 rotate->rotation.d[0] = dx / r;
1706 rotate->rotation.d[4] = -dy / r;
1707 rotate->rotation.d[1] = -rotate->rotation.d[4];
1708 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001709
1710 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001711 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001712 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001713 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001714 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001715
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001716 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001717 &shsurf->surface->geometry.transformation_list,
1718 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001719 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001720 wl_list_init(&shsurf->rotation.transform.link);
1721 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001722 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001723 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001724
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001725 /* We need to adjust the position of the surface
1726 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001727 cposx = surface->geometry.x + cx;
1728 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001729 dposx = rotate->center.x - cposx;
1730 dposy = rotate->center.y - cposy;
1731 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001732 weston_surface_set_position(surface,
1733 surface->geometry.x + dposx,
1734 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001735 }
1736
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001737 /* Repaint implies weston_surface_update_transform(), which
1738 * lazily applies the damage due to rotation update.
1739 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001740 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001741}
1742
1743static void
Scott Moreau447013d2012-02-18 05:05:29 -07001744rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001745 uint32_t time, uint32_t button, uint32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001746{
1747 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001748 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001749 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001750 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001751
Daniel Stone37816df2012-05-16 18:45:18 +01001752 if (pointer->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001753 if (shsurf)
1754 weston_matrix_multiply(&shsurf->rotation.rotation,
1755 &rotate->rotation);
1756 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001757 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001758 free(rotate);
1759 }
1760}
1761
Scott Moreau447013d2012-02-18 05:05:29 -07001762static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001763 noop_grab_focus,
1764 rotate_grab_motion,
1765 rotate_grab_button,
1766};
1767
1768static void
Daniel Stone37816df2012-05-16 18:45:18 +01001769rotate_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001770 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1771 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001772{
1773 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001774 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001775 struct shell_surface *surface;
1776 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001777 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001778 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001779
1780 if (base_surface == NULL)
1781 return;
1782
1783 surface = get_shell_surface(base_surface);
1784 if (!surface)
1785 return;
1786
1787 switch (surface->type) {
1788 case SHELL_SURFACE_PANEL:
1789 case SHELL_SURFACE_BACKGROUND:
1790 case SHELL_SURFACE_FULLSCREEN:
1791 case SHELL_SURFACE_SCREENSAVER:
1792 return;
1793 default:
1794 break;
1795 }
1796
Pekka Paalanen460099f2012-01-20 16:48:25 +02001797 rotate = malloc(sizeof *rotate);
1798 if (!rotate)
1799 return;
1800
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001801 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001802
1803 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001804 surface->surface->geometry.width / 2,
1805 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001806 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001807
Daniel Stone37816df2012-05-16 18:45:18 +01001808 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001809
Daniel Stone37816df2012-05-16 18:45:18 +01001810 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1811 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001812 r = sqrtf(dx * dx + dy * dy);
1813 if (r > 20.0f) {
1814 struct weston_matrix inverse;
1815
1816 weston_matrix_init(&inverse);
1817 inverse.d[0] = dx / r;
1818 inverse.d[4] = dy / r;
1819 inverse.d[1] = -inverse.d[4];
1820 inverse.d[5] = inverse.d[0];
1821 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001822
1823 weston_matrix_init(&rotate->rotation);
1824 rotate->rotation.d[0] = dx / r;
1825 rotate->rotation.d[4] = -dy / r;
1826 rotate->rotation.d[1] = -rotate->rotation.d[4];
1827 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001828 } else {
1829 weston_matrix_init(&surface->rotation.rotation);
1830 weston_matrix_init(&rotate->rotation);
1831 }
1832
Daniel Stone37816df2012-05-16 18:45:18 +01001833 wl_pointer_set_focus(seat->pointer, NULL,
1834 wl_fixed_from_int(0),
1835 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02001836}
1837
1838static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001839activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01001840 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001841{
Alex Wu21858432012-04-01 20:13:08 +08001842 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001843
Daniel Stone37816df2012-05-16 18:45:18 +01001844 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001845
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001846 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001847 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001848 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001849 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001850 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001851
Pekka Paalanen77346a62011-11-30 16:26:35 +02001852 case SHELL_SURFACE_SCREENSAVER:
1853 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001854 if (shell->lock_surface)
1855 weston_surface_restack(es,
1856 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001857 break;
Alex Wu4539b082012-03-01 12:57:46 +08001858 case SHELL_SURFACE_FULLSCREEN:
1859 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001860 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001861 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001862 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001863 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001864 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001865 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1866 wl_list_for_each_reverse_safe(surf,
1867 prev,
1868 &shell->fullscreen_layer.surface_list,
1869 layer_link)
1870 weston_surface_restack(surf,
1871 &shell->toplevel_layer.surface_list);
1872 }
1873
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001874 weston_surface_restack(es,
1875 &shell->toplevel_layer.surface_list);
1876 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001877 }
1878}
1879
Alex Wu21858432012-04-01 20:13:08 +08001880/* no-op func for checking black surface */
1881static void
1882black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1883{
1884}
1885
1886static bool
1887is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1888{
1889 if (es->configure == black_surface_configure) {
1890 if (fs_surface)
1891 *fs_surface = (struct weston_surface *)es->private;
1892 return true;
1893 }
1894 return false;
1895}
1896
Kristian Høgsberg75840622011-09-06 13:48:16 -04001897static void
Daniel Stone37816df2012-05-16 18:45:18 +01001898click_to_activate_binding(struct wl_seat *seat,
Alex Wu4539b082012-03-01 12:57:46 +08001899 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001900 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001901{
Daniel Stone37816df2012-05-16 18:45:18 +01001902 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03001903 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001904 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001905 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001906
Daniel Stone37816df2012-05-16 18:45:18 +01001907 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001908 if (!focus)
1909 return;
1910
Alex Wu21858432012-04-01 20:13:08 +08001911 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001912 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001913
Daniel Stone37816df2012-05-16 18:45:18 +01001914 if (state && seat->pointer->grab == &seat->pointer->default_grab)
1915 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001916}
1917
1918static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001919lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001920{
Tiago Vignattibe143262012-04-16 17:31:41 +03001921 struct desktop_shell *shell =
1922 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01001923 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001924 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001925 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001926
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001927 if (shell->locked) {
1928 wl_list_for_each(output, &shell->compositor->output_list, link)
1929 /* TODO: find a way to jump to other DPMS levels */
1930 if (output->set_dpms)
1931 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001932 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001933 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001934
1935 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001936
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001937 /* Hide all surfaces by removing the fullscreen, panel and
1938 * toplevel layers. This way nothing else can show or receive
1939 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001940
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001941 wl_list_remove(&shell->panel_layer.link);
1942 wl_list_remove(&shell->toplevel_layer.link);
1943 wl_list_remove(&shell->fullscreen_layer.link);
1944 wl_list_insert(&shell->compositor->cursor_layer.link,
1945 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001946
Pekka Paalanen77346a62011-11-30 16:26:35 +02001947 launch_screensaver(shell);
1948
1949 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1950 show_screensaver(shell, shsurf);
1951
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001952 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001953 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001954 weston_compositor_wake(shell->compositor);
1955 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001956 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001957
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001958 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001959 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001960
1961 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01001962 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1963 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001964 }
1965
1966 /* TODO: disable bindings that should not work while locked. */
1967
1968 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001969}
1970
1971static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001972unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001973{
Tiago Vignattibe143262012-04-16 17:31:41 +03001974 struct desktop_shell *shell =
1975 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001976
Pekka Paalanend81c2162011-11-16 13:47:34 +02001977 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001978 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001979 return;
1980 }
1981
1982 /* If desktop-shell client has gone away, unlock immediately. */
1983 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001984 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001985 return;
1986 }
1987
1988 if (shell->prepare_event_sent)
1989 return;
1990
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001991 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001992 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001993}
1994
1995static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001996center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001997{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001998 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001999 GLfloat x = (mode->width - surface->geometry.width) / 2;
2000 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002001
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002002 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002003}
2004
2005static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002006map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002007 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002008{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002009 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002010 struct shell_surface *shsurf;
2011 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002012 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08002013 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002014
Pekka Paalanen77346a62011-11-30 16:26:35 +02002015 shsurf = get_shell_surface(surface);
2016 if (shsurf)
2017 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002018
Pekka Paalanen60921e52012-01-25 15:55:43 +02002019 surface->geometry.width = width;
2020 surface->geometry.height = height;
2021 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002022
2023 /* initial positioning, see also configure() */
2024 switch (surface_type) {
2025 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002026 weston_surface_set_position(surface, 10 + random() % 400,
2027 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002028 break;
2029 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002030 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002031 break;
Alex Wu4539b082012-03-01 12:57:46 +08002032 case SHELL_SURFACE_FULLSCREEN:
2033 shell_map_fullscreen(shsurf);
2034 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002035 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002036 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002037 panel_height = get_output_panel_height(shell,surface->output);
2038 weston_surface_set_position(surface, surface->output->x,
2039 surface->output->y + panel_height);
2040 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002041 case SHELL_SURFACE_LOCK:
2042 center_on_output(surface, get_default_output(compositor));
2043 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002044 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002045 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002046 case SHELL_SURFACE_NONE:
2047 weston_surface_set_position(surface,
2048 surface->geometry.x + sx,
2049 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002050 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002051 default:
2052 ;
2053 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002054
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002055 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002056 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002057 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002058 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002059 wl_list_insert(&shell->background_layer.surface_list,
2060 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002061 break;
2062 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002063 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002064 wl_list_insert(&shell->panel_layer.surface_list,
2065 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002066 break;
2067 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002068 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002069 wl_list_insert(&shell->lock_layer.surface_list,
2070 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002071 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002072 break;
2073 case SHELL_SURFACE_SCREENSAVER:
2074 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002075 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002076 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002077 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002078 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002079 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002080 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002081 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002082 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002083 case SHELL_SURFACE_POPUP:
2084 case SHELL_SURFACE_TRANSIENT:
2085 parent = shsurf->parent->surface;
2086 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2087 break;
Alex Wu4539b082012-03-01 12:57:46 +08002088 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002089 case SHELL_SURFACE_NONE:
2090 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002091 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002092 wl_list_insert(&shell->toplevel_layer.surface_list,
2093 &surface->layer_link);
2094 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002095 }
2096
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002097 if (surface_type != SHELL_SURFACE_NONE) {
2098 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002099 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2100 surface->output = shsurf->output;
2101 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002102
Juan Zhao7bb92f02011-12-15 11:31:51 -05002103 switch (surface_type) {
2104 case SHELL_SURFACE_TOPLEVEL:
2105 case SHELL_SURFACE_TRANSIENT:
2106 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002107 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002108 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002109 activate(shell, surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002110 (struct weston_seat *) compositor->seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002111 break;
2112 default:
2113 break;
2114 }
2115
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002116 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002117 {
2118 switch (shell->win_animation_type) {
2119 case ANIMATION_FADE:
2120 weston_fade_run(surface, NULL, NULL);
2121 break;
2122 case ANIMATION_ZOOM:
2123 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2124 break;
2125 default:
2126 break;
2127 }
2128 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002129}
2130
2131static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002132configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002133 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002134{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002135 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2136 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002137
Pekka Paalanen77346a62011-11-30 16:26:35 +02002138 shsurf = get_shell_surface(surface);
2139 if (shsurf)
2140 surface_type = shsurf->type;
2141
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002142 surface->geometry.x = x;
2143 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002144 surface->geometry.width = width;
2145 surface->geometry.height = height;
2146 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002147
2148 switch (surface_type) {
2149 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002150 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002151 break;
Alex Wu4539b082012-03-01 12:57:46 +08002152 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002153 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002154 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002155 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002156 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002157 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002158 surface->geometry.x = surface->output->x;
2159 surface->geometry.y = surface->output->y +
2160 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002161 break;
Alex Wu4539b082012-03-01 12:57:46 +08002162 case SHELL_SURFACE_TOPLEVEL:
2163 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002164 default:
2165 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002166 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002167
Alex Wu4539b082012-03-01 12:57:46 +08002168 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002169 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002170 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002171
2172 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2173 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002174 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2175 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002176 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002177}
2178
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002179static void
2180shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2181{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002182 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002183 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002184 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002185
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002186 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002187 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002188 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002189 type_changed = 1;
2190 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002191
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002192 if (!weston_surface_is_mapped(es)) {
2193 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002194 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002195 es->geometry.width != es->buffer->width ||
2196 es->geometry.height != es->buffer->height) {
2197 GLfloat from_x, from_y;
2198 GLfloat to_x, to_y;
2199
2200 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2201 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2202 configure(shell, es,
2203 es->geometry.x + to_x - from_x,
2204 es->geometry.y + to_y - from_y,
2205 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002206 }
2207}
2208
Tiago Vignattibe143262012-04-16 17:31:41 +03002209static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002210
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002211static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002212desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002213{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002214 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002215 struct desktop_shell *shell =
2216 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002217
2218 shell->child.process.pid = 0;
2219 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002220
2221 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2222 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002223 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002224 shell->child.deathstamp = time;
2225 shell->child.deathcount = 0;
2226 }
2227
2228 shell->child.deathcount++;
2229 if (shell->child.deathcount > 5) {
2230 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2231 return;
2232 }
2233
2234 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2235 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002236}
2237
2238static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002239launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002240{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002241 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002242
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002243 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002244 &shell->child.process,
2245 shell_exe,
2246 desktop_shell_sigchld);
2247
2248 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002249 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002250 return 0;
2251}
2252
2253static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002254bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2255{
Tiago Vignattibe143262012-04-16 17:31:41 +03002256 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002257
2258 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002259 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002260}
2261
Kristian Høgsberg75840622011-09-06 13:48:16 -04002262static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002263unbind_desktop_shell(struct wl_resource *resource)
2264{
Tiago Vignattibe143262012-04-16 17:31:41 +03002265 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002266
2267 if (shell->locked)
2268 resume_desktop(shell);
2269
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002270 shell->child.desktop_shell = NULL;
2271 shell->prepare_event_sent = false;
2272 free(resource);
2273}
2274
2275static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002276bind_desktop_shell(struct wl_client *client,
2277 void *data, uint32_t version, uint32_t id)
2278{
Tiago Vignattibe143262012-04-16 17:31:41 +03002279 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002280 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002281
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002282 resource = wl_client_add_object(client, &desktop_shell_interface,
2283 &desktop_shell_implementation,
2284 id, shell);
2285
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002286 if (client == shell->child.client) {
2287 resource->destroy = unbind_desktop_shell;
2288 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002289 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002290 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002291
2292 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2293 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002294 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002295}
2296
Pekka Paalanen6e168112011-11-24 11:34:05 +02002297static void
2298screensaver_set_surface(struct wl_client *client,
2299 struct wl_resource *resource,
2300 struct wl_resource *shell_surface_resource,
2301 struct wl_resource *output_resource)
2302{
Tiago Vignattibe143262012-04-16 17:31:41 +03002303 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002304 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002305 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002306
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002307 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002308
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002309 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002310 surface->output = output;
2311 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002312}
2313
2314static const struct screensaver_interface screensaver_implementation = {
2315 screensaver_set_surface
2316};
2317
2318static void
2319unbind_screensaver(struct wl_resource *resource)
2320{
Tiago Vignattibe143262012-04-16 17:31:41 +03002321 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002322
Pekka Paalanen77346a62011-11-30 16:26:35 +02002323 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002324 free(resource);
2325}
2326
2327static void
2328bind_screensaver(struct wl_client *client,
2329 void *data, uint32_t version, uint32_t id)
2330{
Tiago Vignattibe143262012-04-16 17:31:41 +03002331 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002332 struct wl_resource *resource;
2333
2334 resource = wl_client_add_object(client, &screensaver_interface,
2335 &screensaver_implementation,
2336 id, shell);
2337
Pekka Paalanen77346a62011-11-30 16:26:35 +02002338 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002339 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002340 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002341 return;
2342 }
2343
2344 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2345 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002346 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002347}
2348
Kristian Høgsberg07045392012-02-19 18:52:44 -05002349struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002350 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002351 struct weston_surface *current;
2352 struct wl_listener listener;
2353 struct wl_keyboard_grab grab;
2354};
2355
2356static void
2357switcher_next(struct switcher *switcher)
2358{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002359 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002360 struct weston_surface *surface;
2361 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002362 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002363
2364 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002365 switch (get_shell_surface_type(surface)) {
2366 case SHELL_SURFACE_TOPLEVEL:
2367 case SHELL_SURFACE_FULLSCREEN:
2368 case SHELL_SURFACE_MAXIMIZED:
2369 if (first == NULL)
2370 first = surface;
2371 if (prev == switcher->current)
2372 next = surface;
2373 prev = surface;
2374 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002375 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002376 weston_surface_damage(surface);
2377 break;
2378 default:
2379 break;
2380 }
Alex Wu1659daa2012-04-01 20:13:09 +08002381
2382 if (is_black_surface(surface, NULL)) {
2383 surface->alpha = 64;
2384 surface->geometry.dirty = 1;
2385 weston_surface_damage(surface);
2386 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002387 }
2388
2389 if (next == NULL)
2390 next = first;
2391
Alex Wu07b26062012-03-12 16:06:01 +08002392 if (next == NULL)
2393 return;
2394
Kristian Høgsberg07045392012-02-19 18:52:44 -05002395 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002396 wl_signal_add(&next->surface.resource.destroy_signal,
2397 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002398
2399 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002400 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002401
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002402 shsurf = get_shell_surface(switcher->current);
2403 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002404 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002405}
2406
2407static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002408switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002409{
2410 struct switcher *switcher =
2411 container_of(listener, struct switcher, listener);
2412
2413 switcher_next(switcher);
2414}
2415
2416static void
2417switcher_destroy(struct switcher *switcher, uint32_t time)
2418{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002419 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002420 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002421 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002422
2423 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002424 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002425 weston_surface_damage(surface);
2426 }
2427
Alex Wu07b26062012-03-12 16:06:01 +08002428 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002429 activate(switcher->shell, switcher->current,
2430 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002431 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002432 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002433 free(switcher);
2434}
2435
2436static void
2437switcher_key(struct wl_keyboard_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01002438 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002439{
2440 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002441 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002442
Daniel Stone37816df2012-05-16 18:45:18 +01002443 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002444 switcher_destroy(switcher, time);
2445 } else if (key == KEY_TAB && state) {
2446 switcher_next(switcher);
2447 }
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002448}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002449
2450static const struct wl_keyboard_grab_interface switcher_grab = {
2451 switcher_key
2452};
2453
2454static void
Daniel Stone37816df2012-05-16 18:45:18 +01002455switcher_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002456 uint32_t key, uint32_t button, uint32_t axis,
Daniel Stonee5a01202012-05-04 11:21:57 +01002457 int32_t value, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002458{
Tiago Vignattibe143262012-04-16 17:31:41 +03002459 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002460 struct switcher *switcher;
2461
2462 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002463 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002464 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002465 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002466 wl_list_init(&switcher->listener.link);
2467
2468 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002469 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2470 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002471 switcher_next(switcher);
2472}
2473
Pekka Paalanen3c647232011-12-22 13:43:43 +02002474static void
Daniel Stone37816df2012-05-16 18:45:18 +01002475backlight_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002476 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002477{
2478 struct weston_compositor *compositor = data;
2479 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002480 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002481
2482 /* TODO: we're limiting to simple use cases, where we assume just
2483 * control on the primary display. We'd have to extend later if we
2484 * ever get support for setting backlights on random desktop LCD
2485 * panels though */
2486 output = get_default_output(compositor);
2487 if (!output)
2488 return;
2489
2490 if (!output->set_backlight)
2491 return;
2492
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002493 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2494 backlight_new = output->backlight_current - 25;
2495 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2496 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002497
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002498 if (backlight_new < 5)
2499 backlight_new = 5;
2500 if (backlight_new > 255)
2501 backlight_new = 255;
2502
2503 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002504 output->set_backlight(output, output->backlight_current);
2505}
2506
2507static void
Daniel Stone37816df2012-05-16 18:45:18 +01002508debug_repaint_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01002509 uint32_t key, uint32_t button, uint32_t axis,
2510 int32_t value, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002511{
Tiago Vignattibe143262012-04-16 17:31:41 +03002512 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002513 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002514 struct weston_surface *surface;
2515
2516 if (shell->debug_repaint_surface) {
2517 weston_surface_destroy(shell->debug_repaint_surface);
2518 shell->debug_repaint_surface = NULL;
2519 } else {
2520 surface = weston_surface_create(compositor);
2521 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2522 weston_surface_configure(surface, 0, 0, 8192, 8192);
2523 wl_list_insert(&compositor->fade_layer.surface_list,
2524 &surface->layer_link);
2525 weston_surface_assign_output(surface);
2526 pixman_region32_init(&surface->input);
2527
2528 /* Here's the dirty little trick that makes the
2529 * repaint debugging work: we force an
2530 * update_transform first to update dependent state
2531 * and clear the geometry.dirty bit. Then we clear
2532 * the surface damage so it only gets repainted
2533 * piecewise as we repaint other things. */
2534
2535 weston_surface_update_transform(surface);
2536 pixman_region32_fini(&surface->damage);
2537 pixman_region32_init(&surface->damage);
2538 shell->debug_repaint_surface = surface;
2539 }
2540}
2541
2542static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002543shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002544{
Tiago Vignattibe143262012-04-16 17:31:41 +03002545 struct desktop_shell *shell =
2546 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002547
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002548 if (shell->child.client)
2549 wl_client_destroy(shell->child.client);
2550
Kristian Høgsberg88c16072012-05-16 08:04:19 -04002551 wl_list_remove(&shell->lock_listener.link);
2552 wl_list_remove(&shell->unlock_listener.link);
2553
Pekka Paalanen3c647232011-12-22 13:43:43 +02002554 free(shell->screensaver.path);
2555 free(shell);
2556}
2557
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002558static void
2559shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2560{
2561 uint32_t mod;
2562
2563 /* fixed bindings */
2564 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2565 MODIFIER_CTRL | MODIFIER_ALT,
2566 terminate_binding, ec);
2567 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2568 click_to_activate_binding, shell);
2569 weston_compositor_add_binding(ec, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +01002570 WL_POINTER_AXIS_VERTICAL_SCROLL,
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002571 MODIFIER_SUPER | MODIFIER_ALT,
2572 surface_opacity_binding, NULL);
2573 weston_compositor_add_binding(ec, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +01002574 WL_POINTER_AXIS_VERTICAL_SCROLL,
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002575 MODIFIER_SUPER, zoom_binding, NULL);
2576
2577 /* configurable bindings */
2578 mod = shell->binding_modifier;
2579 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2580 move_binding, shell);
2581 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2582 resize_binding, shell);
2583 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2584 rotate_binding, NULL);
2585 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2586 switcher_binding, shell);
2587 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2588 backlight_binding, ec);
2589 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2590 backlight_binding, ec);
2591 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2592 backlight_binding, ec);
2593 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2594 backlight_binding, ec);
2595 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2596 debug_repaint_binding, shell);
2597}
2598
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002599int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002600shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002601
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002602WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002603shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002604{
Tiago Vignattibe143262012-04-16 17:31:41 +03002605 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002606
2607 shell = malloc(sizeof *shell);
2608 if (shell == NULL)
2609 return -1;
2610
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002611 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002612 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002613
2614 shell->destroy_listener.notify = shell_destroy;
2615 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2616 shell->lock_listener.notify = lock;
2617 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2618 shell->unlock_listener.notify = unlock;
2619 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002620 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002621 ec->shell_interface.create_shell_surface = create_shell_surface;
2622 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002623 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002624 ec->shell_interface.move = surface_move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002625
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002626 wl_list_init(&shell->backgrounds);
2627 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002628 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002629
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002630 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2631 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2632 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2633 weston_layer_init(&shell->background_layer,
2634 &shell->toplevel_layer.link);
2635 wl_list_init(&shell->lock_layer.surface_list);
2636
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002637 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002638
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002639 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2640 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002641 return -1;
2642
Kristian Høgsberg75840622011-09-06 13:48:16 -04002643 if (wl_display_add_global(ec->wl_display,
2644 &desktop_shell_interface,
2645 shell, bind_desktop_shell) == NULL)
2646 return -1;
2647
Pekka Paalanen6e168112011-11-24 11:34:05 +02002648 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2649 shell, bind_screensaver) == NULL)
2650 return -1;
2651
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002652 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002653 if (launch_desktop_shell_process(shell) != 0)
2654 return -1;
2655
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002656 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002657
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002658 return 0;
2659}