blob: 80781025e49625cbd412402245fac823cc63fdf7 [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
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Tiago Vignattibe143262012-04-16 17:31:41 +030041struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040043
44 struct wl_listener lock_listener;
45 struct wl_listener unlock_listener;
46 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050048 struct weston_layer fullscreen_layer;
49 struct weston_layer panel_layer;
50 struct weston_layer toplevel_layer;
51 struct weston_layer background_layer;
52 struct weston_layer lock_layer;
53
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020056 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020057 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020058
59 unsigned deathcount;
60 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062
63 bool locked;
64 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020065
Pekka Paalanen068ae942011-11-28 14:11:15 +020066 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050067 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010068
69 struct wl_list backgrounds;
70 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020071
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020073 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020074 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 struct wl_resource *binding;
76 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050079
Tiago Vignatti0b52d482012-04-20 18:54:25 +030080 uint32_t binding_modifier;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050081 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040082};
83
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050084enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020085 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050086
Pekka Paalanen57da4a82011-11-23 16:42:16 +020087 SHELL_SURFACE_PANEL,
88 SHELL_SURFACE_BACKGROUND,
89 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020090 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050091
92 SHELL_SURFACE_TOPLEVEL,
93 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050094 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080095 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050096 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020097};
98
Scott Moreauff1db4a2012-04-17 19:06:18 -060099struct ping_timer {
100 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600101 uint32_t serial;
102};
103
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200104struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200105 struct wl_resource resource;
106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200108 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500109 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300110 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200111
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500112 enum shell_surface_type type;
113 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800114 bool saved_position_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600115 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700123 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500124 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200125 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500126 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400127 struct wl_input_device *device;
128 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 } popup;
130
Alex Wu4539b082012-03-01 12:57:46 +0800131 struct {
132 enum wl_shell_surface_fullscreen_method type;
133 struct weston_transform transform; /* matrix from x, y */
134 uint32_t framerate;
135 struct weston_surface *black_surface;
136 } fullscreen;
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138 struct ping_timer *ping_timer;
139
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600140 struct {
141 struct weston_animation current;
142 int exists;
143 int fading_in;
144 uint32_t timestamp;
145 } unresponsive_animation;
146
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500147 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100149 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300150
151 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200152};
153
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300154struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700155 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300156 struct shell_surface *shsurf;
157 struct wl_listener shsurf_destroy_listener;
158};
159
160struct weston_move_grab {
161 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500162 int32_t dx, dy;
163};
164
Pekka Paalanen460099f2012-01-20 16:48:25 +0200165struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300166 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500167 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200168 struct {
169 int32_t x;
170 int32_t y;
171 } center;
172};
173
Alex Wubd3354b2012-04-17 17:20:49 +0800174static struct shell_surface *
175get_shell_surface(struct weston_surface *surface);
176
177static struct desktop_shell *
178shell_surface_get_shell(struct shell_surface *shsurf);
179
180static bool
181shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
182{
183 struct desktop_shell *shell;
184 struct weston_surface *top_fs_es;
185
186 shell = shell_surface_get_shell(shsurf);
187
188 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
189 return false;
190
191 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
192 struct weston_surface,
193 layer_link);
194 return (shsurf == get_shell_surface(top_fs_es));
195}
196
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500197static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400198destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199{
200 struct shell_grab *grab;
201
202 grab = container_of(listener, struct shell_grab,
203 shsurf_destroy_listener);
204
205 grab->shsurf = NULL;
206}
207
208static void
209shell_grab_init(struct shell_grab *grab,
210 const struct wl_pointer_grab_interface *interface,
211 struct shell_surface *shsurf)
212{
213 grab->grab.interface = interface;
214 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400215 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
216 wl_signal_add(&shsurf->resource.destroy_signal,
217 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300218
219}
220
221static void
222shell_grab_finish(struct shell_grab *grab)
223{
224 wl_list_remove(&grab->shsurf_destroy_listener.link);
225}
226
227static void
Alex Wu4539b082012-03-01 12:57:46 +0800228center_on_output(struct weston_surface *surface,
229 struct weston_output *output);
230
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300231static uint32_t
232get_modifier(char *modifier)
233{
234 if (!modifier)
235 return MODIFIER_SUPER;
236
237 if (!strcmp("ctrl", modifier))
238 return MODIFIER_CTRL;
239 else if (!strcmp("alt", modifier))
240 return MODIFIER_ALT;
241 else if (!strcmp("super", modifier))
242 return MODIFIER_SUPER;
243 else
244 return MODIFIER_SUPER;
245}
246
Alex Wu4539b082012-03-01 12:57:46 +0800247static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300248shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200249{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200250 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200251 char *path = NULL;
252 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300253 char *modifier = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200254
255 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200256 { "path", CONFIG_KEY_STRING, &path },
257 { "duration", CONFIG_KEY_INTEGER, &duration },
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300258 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200259 };
260
261 struct config_section cs[] = {
262 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
263 };
264
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200265 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500266 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200267 free(config_file);
268
Pekka Paalanen7296e792011-12-07 16:22:00 +0200269 shell->screensaver.path = path;
270 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300271 shell->binding_modifier = get_modifier(modifier);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200272}
273
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200274static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400275noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500276 struct wl_surface *surface, int32_t x, int32_t y)
277{
278 grab->focus = NULL;
279}
280
281static void
Scott Moreau447013d2012-02-18 05:05:29 -0700282move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500283 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500285 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500286 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300287 struct shell_surface *shsurf = move->base.shsurf;
288 struct weston_surface *es;
289
290 if (!shsurf)
291 return;
292
293 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500294
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500295 weston_surface_configure(es,
296 device->x + move->dx,
297 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200298 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500299}
300
301static void
Scott Moreau447013d2012-02-18 05:05:29 -0700302move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400303 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500304{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300305 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
306 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500307 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500309 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300310 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500312 free(grab);
313 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500314}
315
Scott Moreau447013d2012-02-18 05:05:29 -0700316static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500317 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318 move_grab_motion,
319 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500320};
321
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600322static void
323unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
324{
325 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
326
327 if(!shsurf->unresponsive_animation.exists) {
328 wl_list_insert(&shsurf->surface->compositor->animation_list,
329 &shsurf->unresponsive_animation.current.link);
330 shsurf->unresponsive_animation.exists = 1;
331 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
332 weston_surface_damage(shsurf->surface);
333 }
334}
335
336static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600337unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600338 struct weston_output *output, uint32_t msecs)
339{
340 struct shell_surface *shsurf =
341 container_of(animation, struct shell_surface, unresponsive_animation.current);
342 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600343 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600344
345 if (!surface || !shsurf)
346 return;
347
348 if (shsurf->unresponsive_animation.fading_in) {
349 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
350 if (surface->saturation > 1)
351 surface->saturation -= 5;
352 if (surface->brightness > 200)
353 surface->brightness--;
354
355 shsurf->unresponsive_animation.timestamp += step;
356 }
357
358 if (surface->saturation <= 1 && surface->brightness <= 200) {
359 wl_list_remove(&shsurf->unresponsive_animation.current.link);
360 shsurf->unresponsive_animation.exists = 0;
361 }
362 }
363 else {
364 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
365 if (surface->saturation < 255)
366 surface->saturation += 5;
367 if (surface->brightness < 255)
368 surface->brightness++;
369
370 shsurf->unresponsive_animation.timestamp += step;
371 }
372
373 if (surface->saturation >= 255 && surface->brightness >= 255) {
374 surface->saturation = surface->brightness = 255;
375 wl_list_remove(&shsurf->unresponsive_animation.current.link);
376 shsurf->unresponsive_animation.exists = 0;
377 }
378 }
379
380 surface->geometry.dirty = 1;
381 weston_surface_damage(surface);
382}
383
Scott Moreau9521d5e2012-04-19 13:06:17 -0600384static void
385ping_timer_destroy(struct shell_surface *shsurf)
386{
387 if (!shsurf || !shsurf->ping_timer)
388 return;
389
390 if (shsurf->ping_timer->source)
391 wl_event_source_remove(shsurf->ping_timer->source);
392
393 free(shsurf->ping_timer);
394 shsurf->ping_timer = NULL;
395}
396
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400397static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600398ping_timeout_handler(void *data)
399{
400 struct shell_surface *shsurf = data;
401
Scott Moreau9521d5e2012-04-19 13:06:17 -0600402 /* Client is not responding */
403 shsurf->unresponsive = 1;
404 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600405
406 return 1;
407}
408
409static void
410ping_handler(struct weston_surface *surface, uint32_t serial)
411{
412 struct shell_surface *shsurf;
413 shsurf = get_shell_surface(surface);
414 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600415 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600416
417 if (!shsurf)
418 return;
419
420 if (!shsurf->ping_timer) {
421 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
422 if (!shsurf->ping_timer)
423 return;
424
425 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600426 loop = wl_display_get_event_loop(surface->compositor->wl_display);
427 shsurf->ping_timer->source =
428 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
429 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
430
431 wl_shell_surface_send_ping(&shsurf->resource, serial);
432 }
433}
434
435static void
436shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
437 uint32_t serial)
438{
439 struct shell_surface *shsurf = resource->data;
440
Scott Moreauff1db4a2012-04-17 19:06:18 -0600441 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600442 if (shsurf->unresponsive) {
443 /* Received pong from previously unresponsive client */
444 unresponsive_surface_fade(shsurf, true);
445 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600446 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600447 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600448 }
449}
450
451static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500452weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400453 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500455 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300456 struct shell_surface *shsurf = get_shell_surface(es);
457
458 if (!shsurf)
459 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500460
461 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400462 if (!move)
463 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500464
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300465 shell_grab_init(&move->base, &move_grab_interface, shsurf);
466
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200467 move->dx = es->geometry.x - wd->input_device.grab_x;
468 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500469
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300470 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400471 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500472
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400473 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400474
475 return 0;
476}
477
478static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200479shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400480 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400481{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500482 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200483 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400484
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500485 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400486 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500487 wd->input_device.pointer_focus != &shsurf->surface->surface)
488 return;
489
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400490 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400491 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500492}
493
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500494struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300495 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500496 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200497 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500498};
499
500static void
Scott Moreau447013d2012-02-18 05:05:29 -0700501resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500502 uint32_t time, int32_t x, int32_t y)
503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500504 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500505 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500506 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200507 int32_t from_x, from_y;
508 int32_t to_x, to_y;
509
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300510 if (!resize->base.shsurf)
511 return;
512
513 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200514 device->grab_x, device->grab_y,
515 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300516 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200517 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500518
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200519 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200520 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200521 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200522 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500523 } else {
524 width = resize->width;
525 }
526
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200527 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200528 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200529 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200530 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500531 } else {
532 height = resize->height;
533 }
534
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300535 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400536 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500537}
538
539static void
Scott Moreau447013d2012-02-18 05:05:29 -0700540resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400541 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500542{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300543 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500544 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500545
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500546 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300547 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400548 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500549 free(grab);
550 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500551}
552
Scott Moreau447013d2012-02-18 05:05:29 -0700553static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500554 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555 resize_grab_motion,
556 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500557};
558
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400559static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500560weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400561 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500563 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500564
Alex Wu4539b082012-03-01 12:57:46 +0800565 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
566 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500567
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200568 if (edges == 0 || edges > 15 ||
569 (edges & 3) == 3 || (edges & 12) == 12)
570 return 0;
571
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500572 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400573 if (!resize)
574 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500575
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300576 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
577
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500578 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200579 resize->width = shsurf->surface->geometry.width;
580 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400581
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300582 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400583 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500584
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400586
587 return 0;
588}
589
590static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200591shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400592 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200593 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400594{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500595 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200596 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400597
Alex Wu4539b082012-03-01 12:57:46 +0800598 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
599 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400600
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500601 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400602 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500603 wd->input_device.pointer_focus != &shsurf->surface->surface)
604 return;
605
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400606 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400607 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500608}
609
Juan Zhao96879df2012-02-07 08:45:41 +0800610static struct weston_output *
611get_default_output(struct weston_compositor *compositor)
612{
613 return container_of(compositor->output_list.next,
614 struct weston_output, link);
615}
616
Alex Wu4539b082012-03-01 12:57:46 +0800617static void
618shell_unset_fullscreen(struct shell_surface *shsurf)
619{
620 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800621 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
622 shell_surface_is_top_fullscreen(shsurf)) {
623 weston_output_switch_mode(shsurf->fullscreen_output,
624 shsurf->fullscreen_output->origin);
625 }
Alex Wu4539b082012-03-01 12:57:46 +0800626 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
627 shsurf->fullscreen.framerate = 0;
628 wl_list_remove(&shsurf->fullscreen.transform.link);
629 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800630 if (shsurf->fullscreen.black_surface)
631 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800632 shsurf->fullscreen.black_surface = NULL;
633 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300634 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800635 weston_surface_set_position(shsurf->surface,
636 shsurf->saved_x, shsurf->saved_y);
637}
638
Pekka Paalanen98262232011-12-01 10:42:22 +0200639static int
640reset_shell_surface_type(struct shell_surface *surface)
641{
642 switch (surface->type) {
643 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800644 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200645 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800646 case SHELL_SURFACE_MAXIMIZED:
647 surface->output = get_default_output(surface->surface->compositor);
648 weston_surface_set_position(surface->surface,
649 surface->saved_x,
650 surface->saved_y);
651 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200652 case SHELL_SURFACE_PANEL:
653 case SHELL_SURFACE_BACKGROUND:
654 wl_list_remove(&surface->link);
655 wl_list_init(&surface->link);
656 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200657 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200658 case SHELL_SURFACE_LOCK:
659 wl_resource_post_error(&surface->resource,
660 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200661 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200662 return -1;
663 case SHELL_SURFACE_NONE:
664 case SHELL_SURFACE_TOPLEVEL:
665 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500666 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200667 break;
668 }
669
670 surface->type = SHELL_SURFACE_NONE;
671 return 0;
672}
673
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500674static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200675shell_surface_set_toplevel(struct wl_client *client,
676 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400677
678{
Pekka Paalanen98262232011-12-01 10:42:22 +0200679 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400680
Pekka Paalanen98262232011-12-01 10:42:22 +0200681 if (reset_shell_surface_type(surface))
682 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400683
Pekka Paalanen98262232011-12-01 10:42:22 +0200684 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400685}
686
687static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200688shell_surface_set_transient(struct wl_client *client,
689 struct wl_resource *resource,
690 struct wl_resource *parent_resource,
691 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400692{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200693 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500694 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200695 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500696 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400697
Pekka Paalanen98262232011-12-01 10:42:22 +0200698 if (reset_shell_surface_type(shsurf))
699 return;
700
Alex Wu4539b082012-03-01 12:57:46 +0800701 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800702 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200703 weston_surface_set_position(es, pes->geometry.x + x,
704 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400705
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200706 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400707}
708
Tiago Vignattibe143262012-04-16 17:31:41 +0300709static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800710shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200711{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400712 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800713}
714
715static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300716get_output_panel_height(struct desktop_shell *shell,
717 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800718{
719 struct shell_surface *priv;
720 int panel_height = 0;
721
722 if (!output)
723 return 0;
724
Tiago Vignattibe143262012-04-16 17:31:41 +0300725 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800726 if (priv->output == output) {
727 panel_height = priv->surface->geometry.height;
728 break;
729 }
730 }
731 return panel_height;
732}
733
734static void
735shell_surface_set_maximized(struct wl_client *client,
736 struct wl_resource *resource,
737 struct wl_resource *output_resource )
738{
739 struct shell_surface *shsurf = resource->data;
740 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300741 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800742 uint32_t edges = 0, panel_height = 0;
743
744 /* get the default output, if the client set it as NULL
745 check whether the ouput is available */
746 if (output_resource)
747 shsurf->output = output_resource->data;
748 else
749 shsurf->output = get_default_output(es->compositor);
750
751 if (reset_shell_surface_type(shsurf))
752 return;
753
754 shsurf->saved_x = es->geometry.x;
755 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800756 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800757
Tiago Vignattibe143262012-04-16 17:31:41 +0300758 shell = shell_surface_get_shell(shsurf);
759 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800760 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500761
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400762 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500763 es->output->current->width,
764 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800765
766 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200767}
768
Alex Wu21858432012-04-01 20:13:08 +0800769static void
770black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
771
Alex Wu4539b082012-03-01 12:57:46 +0800772static struct weston_surface *
773create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800774 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800775 GLfloat x, GLfloat y, int w, int h)
776{
777 struct weston_surface *surface = NULL;
778
779 surface = weston_surface_create(ec);
780 if (surface == NULL) {
781 fprintf(stderr, "no memory\n");
782 return NULL;
783 }
784
Alex Wu21858432012-04-01 20:13:08 +0800785 surface->configure = black_surface_configure;
786 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800787 weston_surface_configure(surface, x, y, w, h);
788 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
789 return surface;
790}
791
792/* Create black surface and append it to the associated fullscreen surface.
793 * Handle size dismatch and positioning according to the method. */
794static void
795shell_configure_fullscreen(struct shell_surface *shsurf)
796{
797 struct weston_output *output = shsurf->fullscreen_output;
798 struct weston_surface *surface = shsurf->surface;
799 struct weston_matrix *matrix;
800 float scale;
801
802 center_on_output(surface, output);
803
804 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500805 shsurf->fullscreen.black_surface =
806 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800807 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500808 output->x, output->y,
809 output->current->width,
810 output->current->height);
811
812 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
813 wl_list_insert(&surface->layer_link,
814 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800815 shsurf->fullscreen.black_surface->output = output;
816
817 switch (shsurf->fullscreen.type) {
818 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
819 break;
820 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
821 matrix = &shsurf->fullscreen.transform.matrix;
822 weston_matrix_init(matrix);
823 scale = (float)output->current->width/(float)surface->geometry.width;
824 weston_matrix_scale(matrix, scale, scale, 1);
825 wl_list_remove(&shsurf->fullscreen.transform.link);
826 wl_list_insert(surface->geometry.transformation_list.prev,
827 &shsurf->fullscreen.transform.link);
828 weston_surface_set_position(surface, output->x, output->y);
829 break;
830 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800831 if (shell_surface_is_top_fullscreen(shsurf)) {
832 struct weston_mode mode = {0,
833 surface->geometry.width,
834 surface->geometry.height,
835 shsurf->fullscreen.framerate};
836
837 if (weston_output_switch_mode(output, &mode) == 0) {
838 weston_surface_configure(shsurf->fullscreen.black_surface,
839 output->x, output->y,
840 output->current->width,
841 output->current->height);
842 weston_surface_set_position(surface, output->x, output->y);
843 break;
844 }
845 }
Alex Wu4539b082012-03-01 12:57:46 +0800846 break;
847 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
848 break;
849 default:
850 break;
851 }
852}
853
854/* make the fullscreen and black surface at the top */
855static void
856shell_stack_fullscreen(struct shell_surface *shsurf)
857{
Alex Wubd3354b2012-04-17 17:20:49 +0800858 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800859 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300860 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800861
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500862 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500863 wl_list_insert(&shell->fullscreen_layer.surface_list,
864 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800865 weston_surface_damage(surface);
866
867 if (!shsurf->fullscreen.black_surface)
868 shsurf->fullscreen.black_surface =
869 create_black_surface(surface->compositor,
870 surface,
871 output->x, output->y,
872 output->current->width,
873 output->current->height);
874
875 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500876 wl_list_insert(&surface->layer_link,
877 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500878 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800879}
880
881static void
882shell_map_fullscreen(struct shell_surface *shsurf)
883{
Alex Wu4539b082012-03-01 12:57:46 +0800884 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800885 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800886}
887
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400888static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200889shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500890 struct wl_resource *resource,
891 uint32_t method,
892 uint32_t framerate,
893 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400894{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200895 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500896 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800897
898 if (output_resource)
899 shsurf->output = output_resource->data;
900 else
901 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400902
Pekka Paalanen98262232011-12-01 10:42:22 +0200903 if (reset_shell_surface_type(shsurf))
904 return;
905
Alex Wu4539b082012-03-01 12:57:46 +0800906 shsurf->fullscreen_output = shsurf->output;
907 shsurf->fullscreen.type = method;
908 shsurf->fullscreen.framerate = framerate;
909 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400910
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200911 shsurf->saved_x = es->geometry.x;
912 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800913 shsurf->saved_position_valid = true;
914
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300915 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300916 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500917
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400918 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500919 shsurf->output->current->width,
920 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400921}
922
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500923static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400924popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500925 struct wl_surface *surface, int32_t x, int32_t y)
926{
927 struct wl_input_device *device = grab->input_device;
928 struct shell_surface *priv =
929 container_of(grab, struct shell_surface, popup.grab);
930 struct wl_client *client = priv->surface->surface.resource.client;
931
Pekka Paalanencb108432012-01-19 16:25:40 +0200932 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400933 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500934 grab->focus = surface;
935 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400936 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500937 grab->focus = NULL;
938 }
939}
940
941static void
Scott Moreau447013d2012-02-18 05:05:29 -0700942popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200943 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500944{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500945 struct wl_resource *resource;
946
947 resource = grab->input_device->pointer_focus_resource;
948 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500949 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500950}
951
952static void
Scott Moreau447013d2012-02-18 05:05:29 -0700953popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400954 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500955{
956 struct wl_resource *resource;
957 struct shell_surface *shsurf =
958 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400959 struct wl_display *display;
960 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500961
962 resource = grab->input_device->pointer_focus_resource;
963 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400964 display = wl_client_get_display(resource->client);
965 serial = wl_display_get_serial(display);
966 wl_input_device_send_button(resource, serial,
967 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500968 } else if (state == 0 &&
969 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400970 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500971 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400972 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500973 shsurf->popup.grab.input_device = NULL;
974 }
975
976 if (state == 0)
977 shsurf->popup.initial_up = 1;
978}
979
Scott Moreau447013d2012-02-18 05:05:29 -0700980static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500981 popup_grab_focus,
982 popup_grab_motion,
983 popup_grab_button,
984};
985
986static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400987shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500988{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400989 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500990 struct weston_surface *es = shsurf->surface;
991 struct weston_surface *parent = shsurf->parent->surface;
992
993 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500994 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500995
Pekka Paalanen938269a2012-02-07 14:19:01 +0200996 weston_surface_update_transform(parent);
997 if (parent->transform.enabled) {
998 shsurf->popup.parent_transform.matrix =
999 parent->transform.matrix;
1000 } else {
1001 /* construct x, y translation matrix */
1002 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1003 shsurf->popup.parent_transform.matrix.d[12] =
1004 parent->geometry.x;
1005 shsurf->popup.parent_transform.matrix.d[13] =
1006 parent->geometry.y;
1007 }
1008 wl_list_insert(es->geometry.transformation_list.prev,
1009 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001010 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001011
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001012 shsurf->popup.initial_up = 0;
1013
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001014 /* We don't require the grab to still be active, but if another
1015 * grab has started in the meantime, we end the popup now. */
1016 if (device->grab_serial == shsurf->popup.serial) {
1017 wl_input_device_start_pointer_grab(device,
1018 &shsurf->popup.grab);
1019 } else {
1020 wl_shell_surface_send_popup_done(&shsurf->resource);
1021 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001022}
1023
1024static void
1025shell_surface_set_popup(struct wl_client *client,
1026 struct wl_resource *resource,
1027 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001028 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001029 struct wl_resource *parent_resource,
1030 int32_t x, int32_t y, uint32_t flags)
1031{
1032 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001033
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001034 shsurf->type = SHELL_SURFACE_POPUP;
1035 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001036 shsurf->popup.device = input_device_resource->data;
1037 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001038 shsurf->popup.x = x;
1039 shsurf->popup.y = y;
1040}
1041
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001042static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001043 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001044 shell_surface_move,
1045 shell_surface_resize,
1046 shell_surface_set_toplevel,
1047 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001048 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001049 shell_surface_set_popup,
1050 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001051};
1052
1053static void
1054destroy_shell_surface(struct wl_resource *resource)
1055{
1056 struct shell_surface *shsurf = resource->data;
1057
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001058 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001059 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001060
Alex Wubd3354b2012-04-17 17:20:49 +08001061 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1062 shell_surface_is_top_fullscreen(shsurf)) {
1063 weston_output_switch_mode(shsurf->fullscreen_output,
1064 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001065 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001066
Alex Wuaa08e2d2012-03-05 11:01:40 +08001067 if (shsurf->fullscreen.black_surface)
1068 weston_surface_destroy(shsurf->fullscreen.black_surface);
1069
Alex Wubd3354b2012-04-17 17:20:49 +08001070 /* As destroy_resource() use wl_list_for_each_safe(),
1071 * we can always remove the listener.
1072 */
1073 wl_list_remove(&shsurf->surface_destroy_listener.link);
1074 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001075 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001076
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077 wl_list_remove(&shsurf->link);
1078 free(shsurf);
1079}
1080
1081static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001082shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001083{
1084 struct shell_surface *shsurf = container_of(listener,
1085 struct shell_surface,
1086 surface_destroy_listener);
1087
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001088 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001089}
1090
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001091static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001092get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001093{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001094 struct wl_listener *listener;
1095
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001096 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1097 shell_handle_surface_destroy);
1098 if (listener)
1099 return container_of(listener, struct shell_surface,
1100 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001101
1102 return NULL;
1103}
1104
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001105static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001106shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1107
1108static void
Pekka Paalanen46229672011-11-29 15:49:31 +02001109shell_get_shell_surface(struct wl_client *client,
1110 struct wl_resource *resource,
1111 uint32_t id,
1112 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001113{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001114 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001115 struct shell_surface *shsurf;
1116
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001117 if (get_shell_surface(surface)) {
1118 wl_resource_post_error(surface_resource,
1119 WL_DISPLAY_ERROR_INVALID_OBJECT,
Alex Wubd3354b2012-04-17 17:20:49 +08001120 "desktop_shell::get_shell_surface already requested");
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001121 return;
1122 }
1123
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001124 if (surface->configure) {
1125 wl_resource_post_error(surface_resource,
1126 WL_DISPLAY_ERROR_INVALID_OBJECT,
1127 "surface->configure already set");
1128 return;
1129 }
1130
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001131 shsurf = calloc(1, sizeof *shsurf);
1132 if (!shsurf) {
1133 wl_resource_post_no_memory(resource);
1134 return;
1135 }
1136
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001137 surface->configure = shell_surface_configure;
1138
Scott Moreauff1db4a2012-04-17 19:06:18 -06001139 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001140 shsurf->unresponsive_animation.exists = 0;
1141 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001142 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001143
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001144 shsurf->resource.destroy = destroy_shell_surface;
1145 shsurf->resource.object.id = id;
1146 shsurf->resource.object.interface = &wl_shell_surface_interface;
1147 shsurf->resource.object.implementation =
1148 (void (**)(void)) &shell_surface_implementation;
1149 shsurf->resource.data = shsurf;
1150
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001151 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +08001152 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001153 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001154 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1155 shsurf->fullscreen.framerate = 0;
1156 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001157 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001158 wl_list_init(&shsurf->fullscreen.transform.link);
1159
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001160 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1161 wl_signal_add(&surface->surface.resource.destroy_signal,
1162 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163
1164 /* init link so its safe to always remove it in destroy_shell_surface */
1165 wl_list_init(&shsurf->link);
1166
Pekka Paalanen460099f2012-01-20 16:48:25 +02001167 /* empty when not in use */
1168 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001169 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001170
Pekka Paalanen98262232011-12-01 10:42:22 +02001171 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001172
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001173 wl_client_add_resource(client, &shsurf->resource);
1174}
1175
1176static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001177 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001178};
1179
Kristian Høgsberg07937562011-04-12 17:25:42 -04001180static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001181handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001182{
1183 proc->pid = 0;
1184}
1185
1186static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001187launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001188{
1189 if (shell->screensaver.binding)
1190 return;
1191
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001192 if (!shell->screensaver.path)
1193 return;
1194
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001195 if (shell->screensaver.process.pid != 0) {
1196 fprintf(stderr, "old screensaver still running\n");
1197 return;
1198 }
1199
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001200 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001201 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001202 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001203 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001204}
1205
1206static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001207terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001208{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001209 if (shell->screensaver.process.pid == 0)
1210 return;
1211
1212 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001213}
1214
1215static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001216show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001217{
1218 struct wl_list *list;
1219
1220 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001221 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001222 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001223 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001224
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225 wl_list_remove(&surface->surface->layer_link);
1226 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001227 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001228 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001229}
1230
1231static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001232hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001233{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001234 wl_list_remove(&surface->surface->layer_link);
1235 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001236 surface->surface->output = NULL;
1237}
1238
1239static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001240desktop_shell_set_background(struct wl_client *client,
1241 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001242 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001243 struct wl_resource *surface_resource)
1244{
Tiago Vignattibe143262012-04-16 17:31:41 +03001245 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001246 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001247 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001248 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001249
Pekka Paalanen98262232011-12-01 10:42:22 +02001250 if (reset_shell_surface_type(shsurf))
1251 return;
1252
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001253 wl_list_for_each(priv, &shell->backgrounds, link) {
1254 if (priv->output == output_resource->data) {
1255 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001256 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001257 wl_list_remove(&priv->link);
1258 break;
1259 }
1260 }
1261
Pekka Paalanen068ae942011-11-28 14:11:15 +02001262 shsurf->type = SHELL_SURFACE_BACKGROUND;
1263 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001264
Pekka Paalanen068ae942011-11-28 14:11:15 +02001265 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001266
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001267 weston_surface_set_position(surface, shsurf->output->x,
1268 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001269
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001270 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001271 surface_resource,
1272 shsurf->output->current->width,
1273 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001274}
1275
1276static void
1277desktop_shell_set_panel(struct wl_client *client,
1278 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001279 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001280 struct wl_resource *surface_resource)
1281{
Tiago Vignattibe143262012-04-16 17:31:41 +03001282 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001283 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001284 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001285 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001286
Pekka Paalanen98262232011-12-01 10:42:22 +02001287 if (reset_shell_surface_type(shsurf))
1288 return;
1289
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001290 wl_list_for_each(priv, &shell->panels, link) {
1291 if (priv->output == output_resource->data) {
1292 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001293 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001294 wl_list_remove(&priv->link);
1295 break;
1296 }
1297 }
1298
Pekka Paalanen068ae942011-11-28 14:11:15 +02001299 shsurf->type = SHELL_SURFACE_PANEL;
1300 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001301
Pekka Paalanen068ae942011-11-28 14:11:15 +02001302 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001303
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001304 weston_surface_set_position(surface, shsurf->output->x,
1305 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001306
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001307 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001308 surface_resource,
1309 shsurf->output->current->width,
1310 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001311}
1312
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001313static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001314handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001315{
Tiago Vignattibe143262012-04-16 17:31:41 +03001316 struct desktop_shell *shell =
1317 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001318
1319 fprintf(stderr, "lock surface gone\n");
1320 shell->lock_surface = NULL;
1321}
1322
1323static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001324desktop_shell_set_lock_surface(struct wl_client *client,
1325 struct wl_resource *resource,
1326 struct wl_resource *surface_resource)
1327{
Tiago Vignattibe143262012-04-16 17:31:41 +03001328 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001329 struct shell_surface *surface = surface_resource->data;
1330
1331 if (reset_shell_surface_type(surface))
1332 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001333
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001334 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001335
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001336 if (!shell->locked)
1337 return;
1338
Pekka Paalanen98262232011-12-01 10:42:22 +02001339 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001340
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001341 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1342 wl_signal_add(&surface_resource->destroy_signal,
1343 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001344
Pekka Paalanen068ae942011-11-28 14:11:15 +02001345 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001346}
1347
1348static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001349resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001350{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001351 struct shell_surface *tmp;
1352
1353 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1354 hide_screensaver(shell, tmp);
1355
1356 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001357
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001358 wl_list_remove(&shell->lock_layer.link);
1359 wl_list_insert(&shell->compositor->cursor_layer.link,
1360 &shell->fullscreen_layer.link);
1361 wl_list_insert(&shell->fullscreen_layer.link,
1362 &shell->panel_layer.link);
1363 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001364
1365 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001366 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001367 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001368 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001369}
1370
1371static void
1372desktop_shell_unlock(struct wl_client *client,
1373 struct wl_resource *resource)
1374{
Tiago Vignattibe143262012-04-16 17:31:41 +03001375 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001376
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001377 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001378
1379 if (shell->locked)
1380 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001381}
1382
Kristian Høgsberg75840622011-09-06 13:48:16 -04001383static const struct desktop_shell_interface desktop_shell_implementation = {
1384 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001385 desktop_shell_set_panel,
1386 desktop_shell_set_lock_surface,
1387 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001388};
1389
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001390static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001391get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001392{
1393 struct shell_surface *shsurf;
1394
1395 shsurf = get_shell_surface(surface);
1396 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001397 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001398 return shsurf->type;
1399}
1400
Kristian Høgsberg75840622011-09-06 13:48:16 -04001401static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001402move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001403 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001404{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001405 struct weston_surface *surface =
1406 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001407
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001408 if (surface == NULL)
1409 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001410
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001411 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001412 case SHELL_SURFACE_PANEL:
1413 case SHELL_SURFACE_BACKGROUND:
1414 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001415 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001416 return;
1417 default:
1418 break;
1419 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001420
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001421 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001422}
1423
1424static void
1425resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001426 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001427{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001428 struct weston_surface *surface =
1429 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001430 uint32_t edges = 0;
1431 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001432 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001433
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001434 if (surface == NULL)
1435 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001436
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001437 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001438 if (!shsurf)
1439 return;
1440
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001441 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001442 case SHELL_SURFACE_PANEL:
1443 case SHELL_SURFACE_BACKGROUND:
1444 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001445 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001446 return;
1447 default:
1448 break;
1449 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001450
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001451 weston_surface_from_global(surface,
1452 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001453
Pekka Paalanen60921e52012-01-25 15:55:43 +02001454 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001455 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001456 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001457 edges |= 0;
1458 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001459 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001460
Pekka Paalanen60921e52012-01-25 15:55:43 +02001461 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001462 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001463 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001464 edges |= 0;
1465 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001466 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001467
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001469 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001470}
1471
1472static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001473surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1474 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1475{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001476 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001477 struct shell_surface *shsurf;
1478 struct weston_surface *surface =
1479 (struct weston_surface *) device->pointer_focus;
1480
1481 if (surface == NULL)
1482 return;
1483
1484 shsurf = get_shell_surface(surface);
1485 if (!shsurf)
1486 return;
1487
1488 switch (shsurf->type) {
1489 case SHELL_SURFACE_BACKGROUND:
1490 case SHELL_SURFACE_SCREENSAVER:
1491 return;
1492 default:
1493 break;
1494 }
1495
1496 surface->alpha += value * step;
1497
1498 if (surface->alpha > 255)
1499 surface->alpha = 255;
1500 if (surface->alpha < step)
1501 surface->alpha = step;
1502
1503 surface->geometry.dirty = 1;
1504 weston_surface_damage(surface);
1505}
1506
1507static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001508zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001509 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001510{
1511 struct weston_input_device *wd = (struct weston_input_device *) device;
1512 struct weston_compositor *compositor = wd->compositor;
1513 struct weston_output *output;
1514
1515 wl_list_for_each(output, &compositor->output_list, link) {
1516 if (pixman_region32_contains_point(&output->region,
1517 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001518 output->zoom.active = 1;
1519 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001520
1521 if (output->zoom.level >= 1.0) {
1522 output->zoom.active = 0;
1523 output->zoom.level = 1.0;
1524 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001525
1526 if (output->zoom.level < output->zoom.increment)
1527 output->zoom.level = output->zoom.increment;
1528
1529 weston_output_update_zoom(output, device->x, device->y);
1530 }
1531 }
1532}
1533
Scott Moreauccbf29d2012-02-22 14:21:41 -07001534static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001535terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001536 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001537{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001539
1540 if (state)
1541 wl_display_terminate(compositor->wl_display);
1542}
1543
1544static void
Scott Moreau447013d2012-02-18 05:05:29 -07001545rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001546 uint32_t time, int32_t x, int32_t y)
1547{
1548 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001549 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001550 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001551 struct shell_surface *shsurf = rotate->base.shsurf;
1552 struct weston_surface *surface;
1553 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1554
1555 if (!shsurf)
1556 return;
1557
1558 surface = shsurf->surface;
1559
1560 cx = 0.5f * surface->geometry.width;
1561 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001562
1563 dx = device->x - rotate->center.x;
1564 dy = device->y - rotate->center.y;
1565 r = sqrtf(dx * dx + dy * dy);
1566
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001567 wl_list_remove(&shsurf->rotation.transform.link);
1568 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001569
1570 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001571 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001572 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001573
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001574 weston_matrix_init(&rotate->rotation);
1575 rotate->rotation.d[0] = dx / r;
1576 rotate->rotation.d[4] = -dy / r;
1577 rotate->rotation.d[1] = -rotate->rotation.d[4];
1578 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001579
1580 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001581 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001582 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001583 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001584 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001585
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001586 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001587 &shsurf->surface->geometry.transformation_list,
1588 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001589 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001590 wl_list_init(&shsurf->rotation.transform.link);
1591 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001592 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001593 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001594
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001595 /* We need to adjust the position of the surface
1596 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001597 cposx = surface->geometry.x + cx;
1598 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001599 dposx = rotate->center.x - cposx;
1600 dposy = rotate->center.y - cposy;
1601 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001602 weston_surface_set_position(surface,
1603 surface->geometry.x + dposx,
1604 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001605 }
1606
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001607 /* Repaint implies weston_surface_update_transform(), which
1608 * lazily applies the damage due to rotation update.
1609 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001610 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001611}
1612
1613static void
Scott Moreau447013d2012-02-18 05:05:29 -07001614rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001615 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001616{
1617 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001618 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001619 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001620 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001621
1622 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001623 if (shsurf)
1624 weston_matrix_multiply(&shsurf->rotation.rotation,
1625 &rotate->rotation);
1626 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001627 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001628 free(rotate);
1629 }
1630}
1631
Scott Moreau447013d2012-02-18 05:05:29 -07001632static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001633 noop_grab_focus,
1634 rotate_grab_motion,
1635 rotate_grab_button,
1636};
1637
1638static void
1639rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001640 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001641{
1642 struct weston_surface *base_surface =
1643 (struct weston_surface *) device->pointer_focus;
1644 struct shell_surface *surface;
1645 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001646 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001647 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001648
1649 if (base_surface == NULL)
1650 return;
1651
1652 surface = get_shell_surface(base_surface);
1653 if (!surface)
1654 return;
1655
1656 switch (surface->type) {
1657 case SHELL_SURFACE_PANEL:
1658 case SHELL_SURFACE_BACKGROUND:
1659 case SHELL_SURFACE_FULLSCREEN:
1660 case SHELL_SURFACE_SCREENSAVER:
1661 return;
1662 default:
1663 break;
1664 }
1665
Pekka Paalanen460099f2012-01-20 16:48:25 +02001666 rotate = malloc(sizeof *rotate);
1667 if (!rotate)
1668 return;
1669
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001670 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001671
1672 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001673 surface->surface->geometry.width / 2,
1674 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001675 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001676
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001677 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001678
1679 dx = device->x - rotate->center.x;
1680 dy = device->y - rotate->center.y;
1681 r = sqrtf(dx * dx + dy * dy);
1682 if (r > 20.0f) {
1683 struct weston_matrix inverse;
1684
1685 weston_matrix_init(&inverse);
1686 inverse.d[0] = dx / r;
1687 inverse.d[4] = dy / r;
1688 inverse.d[1] = -inverse.d[4];
1689 inverse.d[5] = inverse.d[0];
1690 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001691
1692 weston_matrix_init(&rotate->rotation);
1693 rotate->rotation.d[0] = dx / r;
1694 rotate->rotation.d[4] = -dy / r;
1695 rotate->rotation.d[1] = -rotate->rotation.d[4];
1696 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001697 } else {
1698 weston_matrix_init(&surface->rotation.rotation);
1699 weston_matrix_init(&rotate->rotation);
1700 }
1701
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001702 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001703}
1704
1705static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001706activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001707 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001708{
Alex Wu21858432012-04-01 20:13:08 +08001709 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001710
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001711 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001712
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001713 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001714 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001715 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001716 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001717 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001718
Pekka Paalanen77346a62011-11-30 16:26:35 +02001719 case SHELL_SURFACE_SCREENSAVER:
1720 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001721 if (shell->lock_surface)
1722 weston_surface_restack(es,
1723 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001724 break;
Alex Wu4539b082012-03-01 12:57:46 +08001725 case SHELL_SURFACE_FULLSCREEN:
1726 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001727 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001728 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001729 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001730 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001731 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001732 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1733 wl_list_for_each_reverse_safe(surf,
1734 prev,
1735 &shell->fullscreen_layer.surface_list,
1736 layer_link)
1737 weston_surface_restack(surf,
1738 &shell->toplevel_layer.surface_list);
1739 }
1740
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001741 weston_surface_restack(es,
1742 &shell->toplevel_layer.surface_list);
1743 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001744 }
1745}
1746
Alex Wu21858432012-04-01 20:13:08 +08001747/* no-op func for checking black surface */
1748static void
1749black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1750{
1751}
1752
1753static bool
1754is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1755{
1756 if (es->configure == black_surface_configure) {
1757 if (fs_surface)
1758 *fs_surface = (struct weston_surface *)es->private;
1759 return true;
1760 }
1761 return false;
1762}
1763
Kristian Høgsberg75840622011-09-06 13:48:16 -04001764static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001765click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001766 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001767 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001768{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001769 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001770 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001771 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001772 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001773
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001774 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001775 if (!focus)
1776 return;
1777
Alex Wu21858432012-04-01 20:13:08 +08001778 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001779 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001780
Alex Wu9c35e6b2012-03-05 14:13:13 +08001781 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001782 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001783}
1784
1785static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001786lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001787{
Tiago Vignattibe143262012-04-16 17:31:41 +03001788 struct desktop_shell *shell =
1789 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001790 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001791 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001792 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001793
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001794 if (shell->locked) {
1795 wl_list_for_each(output, &shell->compositor->output_list, link)
1796 /* TODO: find a way to jump to other DPMS levels */
1797 if (output->set_dpms)
1798 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001799 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001800 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001801
1802 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001803
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001804 /* Hide all surfaces by removing the fullscreen, panel and
1805 * toplevel layers. This way nothing else can show or receive
1806 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001807
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001808 wl_list_remove(&shell->panel_layer.link);
1809 wl_list_remove(&shell->toplevel_layer.link);
1810 wl_list_remove(&shell->fullscreen_layer.link);
1811 wl_list_insert(&shell->compositor->cursor_layer.link,
1812 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001813
Pekka Paalanen77346a62011-11-30 16:26:35 +02001814 launch_screensaver(shell);
1815
1816 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1817 show_screensaver(shell, shsurf);
1818
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001819 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001820 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001821 weston_compositor_wake(shell->compositor);
1822 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001823 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001824
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001825 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001826 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001827
1828 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001829 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1830 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001831 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001832 }
1833
1834 /* TODO: disable bindings that should not work while locked. */
1835
1836 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001837}
1838
1839static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001840unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001841{
Tiago Vignattibe143262012-04-16 17:31:41 +03001842 struct desktop_shell *shell =
1843 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001844
Pekka Paalanend81c2162011-11-16 13:47:34 +02001845 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001846 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001847 return;
1848 }
1849
1850 /* If desktop-shell client has gone away, unlock immediately. */
1851 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001852 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001853 return;
1854 }
1855
1856 if (shell->prepare_event_sent)
1857 return;
1858
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001859 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001860 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001861}
1862
1863static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001864center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001865{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001866 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001867 GLfloat x = (mode->width - surface->geometry.width) / 2;
1868 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001869
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001870 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001871}
1872
1873static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001874map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001875 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001877 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001878 struct shell_surface *shsurf;
1879 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001880 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001881 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001882
Pekka Paalanen77346a62011-11-30 16:26:35 +02001883 shsurf = get_shell_surface(surface);
1884 if (shsurf)
1885 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001886
Pekka Paalanen60921e52012-01-25 15:55:43 +02001887 surface->geometry.width = width;
1888 surface->geometry.height = height;
1889 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001890
1891 /* initial positioning, see also configure() */
1892 switch (surface_type) {
1893 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001894 weston_surface_set_position(surface, 10 + random() % 400,
1895 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001896 break;
1897 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001898 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001899 break;
Alex Wu4539b082012-03-01 12:57:46 +08001900 case SHELL_SURFACE_FULLSCREEN:
1901 shell_map_fullscreen(shsurf);
1902 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001903 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001904 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001905 panel_height = get_output_panel_height(shell,surface->output);
1906 weston_surface_set_position(surface, surface->output->x,
1907 surface->output->y + panel_height);
1908 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001909 case SHELL_SURFACE_LOCK:
1910 center_on_output(surface, get_default_output(compositor));
1911 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001912 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001913 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001914 case SHELL_SURFACE_NONE:
1915 weston_surface_set_position(surface,
1916 surface->geometry.x + sx,
1917 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001918 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001919 default:
1920 ;
1921 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001922
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001923 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001924 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001925 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001926 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001927 wl_list_insert(&shell->background_layer.surface_list,
1928 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001929 break;
1930 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001931 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001932 wl_list_insert(&shell->panel_layer.surface_list,
1933 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001934 break;
1935 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001936 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001937 wl_list_insert(&shell->lock_layer.surface_list,
1938 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001939 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001940 break;
1941 case SHELL_SURFACE_SCREENSAVER:
1942 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001943 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001944 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001945 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001946 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001947 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001948 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001949 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001950 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001951 case SHELL_SURFACE_POPUP:
1952 case SHELL_SURFACE_TRANSIENT:
1953 parent = shsurf->parent->surface;
1954 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1955 break;
Alex Wu4539b082012-03-01 12:57:46 +08001956 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001957 case SHELL_SURFACE_NONE:
1958 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001959 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001960 wl_list_insert(&shell->toplevel_layer.surface_list,
1961 &surface->layer_link);
1962 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001963 }
1964
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001965 if (surface_type != SHELL_SURFACE_NONE) {
1966 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001967 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1968 surface->output = shsurf->output;
1969 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001970
Juan Zhao7bb92f02011-12-15 11:31:51 -05001971 switch (surface_type) {
1972 case SHELL_SURFACE_TOPLEVEL:
1973 case SHELL_SURFACE_TRANSIENT:
1974 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001975 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001976 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001977 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001978 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001979 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001980 break;
1981 default:
1982 break;
1983 }
1984
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001985 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001986 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001987}
1988
1989static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001990configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001991 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001992{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001993 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1994 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001995
Pekka Paalanen77346a62011-11-30 16:26:35 +02001996 shsurf = get_shell_surface(surface);
1997 if (shsurf)
1998 surface_type = shsurf->type;
1999
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002000 surface->geometry.x = x;
2001 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002002 surface->geometry.width = width;
2003 surface->geometry.height = height;
2004 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002005
2006 switch (surface_type) {
2007 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002008 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002009 break;
Alex Wu4539b082012-03-01 12:57:46 +08002010 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002011 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002012 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002013 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002014 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002015 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002016 surface->geometry.x = surface->output->x;
2017 surface->geometry.y = surface->output->y +
2018 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002019 break;
Alex Wu4539b082012-03-01 12:57:46 +08002020 case SHELL_SURFACE_TOPLEVEL:
2021 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002022 default:
2023 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002024 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002025
Alex Wu4539b082012-03-01 12:57:46 +08002026 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002027 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002028 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002029
2030 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2031 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002032 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2033 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002034 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002035}
2036
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002037static void
2038shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2039{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002040 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002041 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002042
2043 if (!weston_surface_is_mapped(es)) {
2044 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002045 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002046 es->geometry.width != es->buffer->width ||
2047 es->geometry.height != es->buffer->height) {
2048 GLfloat from_x, from_y;
2049 GLfloat to_x, to_y;
2050
2051 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2052 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2053 configure(shell, es,
2054 es->geometry.x + to_x - from_x,
2055 es->geometry.y + to_y - from_y,
2056 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002057 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002058 }
2059}
2060
Tiago Vignattibe143262012-04-16 17:31:41 +03002061static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002062
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002063static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002064desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002065{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002066 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002067 struct desktop_shell *shell =
2068 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002069
2070 shell->child.process.pid = 0;
2071 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002072
2073 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2074 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002075 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002076 shell->child.deathstamp = time;
2077 shell->child.deathcount = 0;
2078 }
2079
2080 shell->child.deathcount++;
2081 if (shell->child.deathcount > 5) {
2082 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2083 return;
2084 }
2085
2086 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2087 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002088}
2089
2090static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002091launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002092{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002093 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002094
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002095 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002096 &shell->child.process,
2097 shell_exe,
2098 desktop_shell_sigchld);
2099
2100 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002101 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002102 return 0;
2103}
2104
2105static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002106bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2107{
Tiago Vignattibe143262012-04-16 17:31:41 +03002108 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002109
2110 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002111 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002112}
2113
Kristian Høgsberg75840622011-09-06 13:48:16 -04002114static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002115unbind_desktop_shell(struct wl_resource *resource)
2116{
Tiago Vignattibe143262012-04-16 17:31:41 +03002117 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002118
2119 if (shell->locked)
2120 resume_desktop(shell);
2121
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002122 shell->child.desktop_shell = NULL;
2123 shell->prepare_event_sent = false;
2124 free(resource);
2125}
2126
2127static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002128bind_desktop_shell(struct wl_client *client,
2129 void *data, uint32_t version, uint32_t id)
2130{
Tiago Vignattibe143262012-04-16 17:31:41 +03002131 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002132 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002133
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002134 resource = wl_client_add_object(client, &desktop_shell_interface,
2135 &desktop_shell_implementation,
2136 id, shell);
2137
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002138 if (client == shell->child.client) {
2139 resource->destroy = unbind_desktop_shell;
2140 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002141 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002142 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002143
2144 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2145 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002146 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002147}
2148
Pekka Paalanen6e168112011-11-24 11:34:05 +02002149static void
2150screensaver_set_surface(struct wl_client *client,
2151 struct wl_resource *resource,
2152 struct wl_resource *shell_surface_resource,
2153 struct wl_resource *output_resource)
2154{
Tiago Vignattibe143262012-04-16 17:31:41 +03002155 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002156 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002157 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002158
Pekka Paalanen98262232011-12-01 10:42:22 +02002159 if (reset_shell_surface_type(surface))
2160 return;
2161
Pekka Paalanen77346a62011-11-30 16:26:35 +02002162 surface->type = SHELL_SURFACE_SCREENSAVER;
2163
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002164 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002165 surface->output = output;
2166 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002167}
2168
2169static const struct screensaver_interface screensaver_implementation = {
2170 screensaver_set_surface
2171};
2172
2173static void
2174unbind_screensaver(struct wl_resource *resource)
2175{
Tiago Vignattibe143262012-04-16 17:31:41 +03002176 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002177
Pekka Paalanen77346a62011-11-30 16:26:35 +02002178 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002179 free(resource);
2180}
2181
2182static void
2183bind_screensaver(struct wl_client *client,
2184 void *data, uint32_t version, uint32_t id)
2185{
Tiago Vignattibe143262012-04-16 17:31:41 +03002186 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002187 struct wl_resource *resource;
2188
2189 resource = wl_client_add_object(client, &screensaver_interface,
2190 &screensaver_implementation,
2191 id, shell);
2192
Pekka Paalanen77346a62011-11-30 16:26:35 +02002193 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002194 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002195 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002196 return;
2197 }
2198
2199 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2200 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002201 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002202}
2203
Kristian Høgsberg07045392012-02-19 18:52:44 -05002204struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002205 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002206 struct weston_surface *current;
2207 struct wl_listener listener;
2208 struct wl_keyboard_grab grab;
2209};
2210
2211static void
2212switcher_next(struct switcher *switcher)
2213{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002214 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002215 struct weston_surface *surface;
2216 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002217 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002218
2219 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002220 switch (get_shell_surface_type(surface)) {
2221 case SHELL_SURFACE_TOPLEVEL:
2222 case SHELL_SURFACE_FULLSCREEN:
2223 case SHELL_SURFACE_MAXIMIZED:
2224 if (first == NULL)
2225 first = surface;
2226 if (prev == switcher->current)
2227 next = surface;
2228 prev = surface;
2229 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002230 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002231 weston_surface_damage(surface);
2232 break;
2233 default:
2234 break;
2235 }
Alex Wu1659daa2012-04-01 20:13:09 +08002236
2237 if (is_black_surface(surface, NULL)) {
2238 surface->alpha = 64;
2239 surface->geometry.dirty = 1;
2240 weston_surface_damage(surface);
2241 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002242 }
2243
2244 if (next == NULL)
2245 next = first;
2246
Alex Wu07b26062012-03-12 16:06:01 +08002247 if (next == NULL)
2248 return;
2249
Kristian Høgsberg07045392012-02-19 18:52:44 -05002250 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002251 wl_signal_add(&next->surface.resource.destroy_signal,
2252 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002253
2254 switcher->current = next;
2255 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002256
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002257 shsurf = get_shell_surface(switcher->current);
2258 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2259 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002260}
2261
2262static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002263switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002264{
2265 struct switcher *switcher =
2266 container_of(listener, struct switcher, listener);
2267
2268 switcher_next(switcher);
2269}
2270
2271static void
2272switcher_destroy(struct switcher *switcher, uint32_t time)
2273{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002274 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002275 struct weston_surface *surface;
2276 struct weston_input_device *device =
2277 (struct weston_input_device *) switcher->grab.input_device;
2278
2279 wl_list_for_each(surface, &compositor->surface_list, link) {
2280 surface->alpha = 255;
2281 weston_surface_damage(surface);
2282 }
2283
Alex Wu07b26062012-03-12 16:06:01 +08002284 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002285 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002286 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002287 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002288 free(switcher);
2289}
2290
2291static void
2292switcher_key(struct wl_keyboard_grab *grab,
2293 uint32_t time, uint32_t key, int32_t state)
2294{
2295 struct switcher *switcher = container_of(grab, struct switcher, grab);
2296 struct weston_input_device *device =
2297 (struct weston_input_device *) grab->input_device;
2298
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002299 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002300 switcher_destroy(switcher, time);
2301 } else if (key == KEY_TAB && state) {
2302 switcher_next(switcher);
2303 }
2304};
2305
2306static const struct wl_keyboard_grab_interface switcher_grab = {
2307 switcher_key
2308};
2309
2310static void
2311switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002312 uint32_t key, uint32_t button, uint32_t axis,
2313 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002314{
Tiago Vignattibe143262012-04-16 17:31:41 +03002315 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002316 struct switcher *switcher;
2317
2318 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002319 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002320 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002321 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002322 wl_list_init(&switcher->listener.link);
2323
2324 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002325 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2326 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002327 switcher_next(switcher);
2328}
2329
Pekka Paalanen3c647232011-12-22 13:43:43 +02002330static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002331backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002332 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002333{
2334 struct weston_compositor *compositor = data;
2335 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002336 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002337
2338 /* TODO: we're limiting to simple use cases, where we assume just
2339 * control on the primary display. We'd have to extend later if we
2340 * ever get support for setting backlights on random desktop LCD
2341 * panels though */
2342 output = get_default_output(compositor);
2343 if (!output)
2344 return;
2345
2346 if (!output->set_backlight)
2347 return;
2348
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002349 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2350 backlight_new = output->backlight_current - 25;
2351 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2352 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002353
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002354 if (backlight_new < 5)
2355 backlight_new = 5;
2356 if (backlight_new > 255)
2357 backlight_new = 255;
2358
2359 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002360 output->set_backlight(output, output->backlight_current);
2361}
2362
2363static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002364debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002365 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002366{
Tiago Vignattibe143262012-04-16 17:31:41 +03002367 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002368 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002369 struct weston_surface *surface;
2370
2371 if (shell->debug_repaint_surface) {
2372 weston_surface_destroy(shell->debug_repaint_surface);
2373 shell->debug_repaint_surface = NULL;
2374 } else {
2375 surface = weston_surface_create(compositor);
2376 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2377 weston_surface_configure(surface, 0, 0, 8192, 8192);
2378 wl_list_insert(&compositor->fade_layer.surface_list,
2379 &surface->layer_link);
2380 weston_surface_assign_output(surface);
2381 pixman_region32_init(&surface->input);
2382
2383 /* Here's the dirty little trick that makes the
2384 * repaint debugging work: we force an
2385 * update_transform first to update dependent state
2386 * and clear the geometry.dirty bit. Then we clear
2387 * the surface damage so it only gets repainted
2388 * piecewise as we repaint other things. */
2389
2390 weston_surface_update_transform(surface);
2391 pixman_region32_fini(&surface->damage);
2392 pixman_region32_init(&surface->damage);
2393 shell->debug_repaint_surface = surface;
2394 }
2395}
2396
2397static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002398shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002399{
Tiago Vignattibe143262012-04-16 17:31:41 +03002400 struct desktop_shell *shell =
2401 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002402
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002403 if (shell->child.client)
2404 wl_client_destroy(shell->child.client);
2405
Pekka Paalanen3c647232011-12-22 13:43:43 +02002406 free(shell->screensaver.path);
2407 free(shell);
2408}
2409
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002410static void
2411shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2412{
2413 uint32_t mod;
2414
2415 /* fixed bindings */
2416 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2417 MODIFIER_CTRL | MODIFIER_ALT,
2418 terminate_binding, ec);
2419 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2420 click_to_activate_binding, shell);
2421 weston_compositor_add_binding(ec, 0, 0,
2422 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2423 MODIFIER_SUPER | MODIFIER_ALT,
2424 surface_opacity_binding, NULL);
2425 weston_compositor_add_binding(ec, 0, 0,
2426 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2427 MODIFIER_SUPER, zoom_binding, NULL);
2428
2429 /* configurable bindings */
2430 mod = shell->binding_modifier;
2431 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2432 move_binding, shell);
2433 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2434 resize_binding, shell);
2435 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2436 rotate_binding, NULL);
2437 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2438 switcher_binding, shell);
2439 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2440 backlight_binding, ec);
2441 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2442 backlight_binding, ec);
2443 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2444 backlight_binding, ec);
2445 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2446 backlight_binding, ec);
2447 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2448 debug_repaint_binding, shell);
2449}
2450
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002451int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002452shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002453
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002454WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002455shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002456{
Tiago Vignattibe143262012-04-16 17:31:41 +03002457 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002458
2459 shell = malloc(sizeof *shell);
2460 if (shell == NULL)
2461 return -1;
2462
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002463 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002464 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002465
2466 shell->destroy_listener.notify = shell_destroy;
2467 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2468 shell->lock_listener.notify = lock;
2469 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2470 shell->unlock_listener.notify = unlock;
2471 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002472 ec->ping_handler = ping_handler;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002473
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002474 wl_list_init(&shell->backgrounds);
2475 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002476 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002477
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002478 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2479 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2480 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2481 weston_layer_init(&shell->background_layer,
2482 &shell->toplevel_layer.link);
2483 wl_list_init(&shell->lock_layer.surface_list);
2484
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002485 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002486
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002487 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2488 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002489 return -1;
2490
Kristian Høgsberg75840622011-09-06 13:48:16 -04002491 if (wl_display_add_global(ec->wl_display,
2492 &desktop_shell_interface,
2493 shell, bind_desktop_shell) == NULL)
2494 return -1;
2495
Pekka Paalanen6e168112011-11-24 11:34:05 +02002496 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2497 shell, bind_screensaver) == NULL)
2498 return -1;
2499
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002500 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002501 if (launch_desktop_shell_process(shell) != 0)
2502 return -1;
2503
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002504 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002505
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002506 return 0;
2507}