blob: 42c980ce45a7016f9ae89f3aba1042b0de5c2305 [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
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400255 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300256 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200257 };
258
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400259 struct config_key saver_keys[] = {
260 { "path", CONFIG_KEY_STRING, &path },
261 { "duration", CONFIG_KEY_INTEGER, &duration },
262 };
263
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200264 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400265 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200266 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
267 };
268
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200269 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500270 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200271 free(config_file);
272
Pekka Paalanen7296e792011-12-07 16:22:00 +0200273 shell->screensaver.path = path;
274 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300275 shell->binding_modifier = get_modifier(modifier);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200276}
277
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200278static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400279noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500280 struct wl_surface *surface, int32_t x, int32_t y)
281{
282 grab->focus = NULL;
283}
284
285static void
Scott Moreau447013d2012-02-18 05:05:29 -0700286move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500287 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500288{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500289 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500290 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291 struct shell_surface *shsurf = move->base.shsurf;
292 struct weston_surface *es;
293
294 if (!shsurf)
295 return;
296
297 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500298
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500299 weston_surface_configure(es,
300 device->x + move->dx,
301 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200302 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500303}
304
305static void
Scott Moreau447013d2012-02-18 05:05:29 -0700306move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400307 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300309 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
310 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500311 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500312
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500313 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300314 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400315 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500316 free(grab);
317 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318}
319
Scott Moreau447013d2012-02-18 05:05:29 -0700320static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500321 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322 move_grab_motion,
323 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500324};
325
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600326static void
327unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
328{
329 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
330
331 if(!shsurf->unresponsive_animation.exists) {
332 wl_list_insert(&shsurf->surface->compositor->animation_list,
333 &shsurf->unresponsive_animation.current.link);
334 shsurf->unresponsive_animation.exists = 1;
335 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
336 weston_surface_damage(shsurf->surface);
337 }
338}
339
340static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600341unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600342 struct weston_output *output, uint32_t msecs)
343{
344 struct shell_surface *shsurf =
345 container_of(animation, struct shell_surface, unresponsive_animation.current);
346 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600347 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600348
349 if (!surface || !shsurf)
350 return;
351
352 if (shsurf->unresponsive_animation.fading_in) {
353 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
354 if (surface->saturation > 1)
355 surface->saturation -= 5;
356 if (surface->brightness > 200)
357 surface->brightness--;
358
359 shsurf->unresponsive_animation.timestamp += step;
360 }
361
362 if (surface->saturation <= 1 && surface->brightness <= 200) {
363 wl_list_remove(&shsurf->unresponsive_animation.current.link);
364 shsurf->unresponsive_animation.exists = 0;
365 }
366 }
367 else {
368 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
369 if (surface->saturation < 255)
370 surface->saturation += 5;
371 if (surface->brightness < 255)
372 surface->brightness++;
373
374 shsurf->unresponsive_animation.timestamp += step;
375 }
376
377 if (surface->saturation >= 255 && surface->brightness >= 255) {
378 surface->saturation = surface->brightness = 255;
379 wl_list_remove(&shsurf->unresponsive_animation.current.link);
380 shsurf->unresponsive_animation.exists = 0;
381 }
382 }
383
384 surface->geometry.dirty = 1;
385 weston_surface_damage(surface);
386}
387
Scott Moreau9521d5e2012-04-19 13:06:17 -0600388static void
389ping_timer_destroy(struct shell_surface *shsurf)
390{
391 if (!shsurf || !shsurf->ping_timer)
392 return;
393
394 if (shsurf->ping_timer->source)
395 wl_event_source_remove(shsurf->ping_timer->source);
396
397 free(shsurf->ping_timer);
398 shsurf->ping_timer = NULL;
399}
400
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400401static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600402ping_timeout_handler(void *data)
403{
404 struct shell_surface *shsurf = data;
405
Scott Moreau9521d5e2012-04-19 13:06:17 -0600406 /* Client is not responding */
407 shsurf->unresponsive = 1;
408 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600409
410 return 1;
411}
412
413static void
414ping_handler(struct weston_surface *surface, uint32_t serial)
415{
416 struct shell_surface *shsurf;
417 shsurf = get_shell_surface(surface);
418 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600419 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600420
421 if (!shsurf)
422 return;
423
424 if (!shsurf->ping_timer) {
425 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
426 if (!shsurf->ping_timer)
427 return;
428
429 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600430 loop = wl_display_get_event_loop(surface->compositor->wl_display);
431 shsurf->ping_timer->source =
432 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
433 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
434
435 wl_shell_surface_send_ping(&shsurf->resource, serial);
436 }
437}
438
439static void
440shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
441 uint32_t serial)
442{
443 struct shell_surface *shsurf = resource->data;
444
Scott Moreauff1db4a2012-04-17 19:06:18 -0600445 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600446 if (shsurf->unresponsive) {
447 /* Received pong from previously unresponsive client */
448 unresponsive_surface_fade(shsurf, true);
449 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600450 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600451 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600452 }
453}
454
455static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400457 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500458{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500459 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300460 struct shell_surface *shsurf = get_shell_surface(es);
461
462 if (!shsurf)
463 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500464
465 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400466 if (!move)
467 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500468
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300469 shell_grab_init(&move->base, &move_grab_interface, shsurf);
470
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200471 move->dx = es->geometry.x - wd->input_device.grab_x;
472 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500473
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300474 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400475 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500476
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400477 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400478
479 return 0;
480}
481
482static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200483shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400484 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500486 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200487 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400488
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500489 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400490 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500491 wd->input_device.pointer_focus != &shsurf->surface->surface)
492 return;
493
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400494 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400495 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500496}
497
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500498struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300499 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500500 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200501 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500502};
503
504static void
Scott Moreau447013d2012-02-18 05:05:29 -0700505resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500506 uint32_t time, int32_t x, int32_t y)
507{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500508 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500509 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500510 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200511 int32_t from_x, from_y;
512 int32_t to_x, to_y;
513
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300514 if (!resize->base.shsurf)
515 return;
516
517 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200518 device->grab_x, device->grab_y,
519 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300520 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200521 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500522
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200523 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200524 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200525 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200526 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500527 } else {
528 width = resize->width;
529 }
530
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200531 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200532 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200533 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200534 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500535 } else {
536 height = resize->height;
537 }
538
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300539 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400540 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500541}
542
543static void
Scott Moreau447013d2012-02-18 05:05:29 -0700544resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400545 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500546{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300547 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500548 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500549
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500550 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300551 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400552 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500553 free(grab);
554 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555}
556
Scott Moreau447013d2012-02-18 05:05:29 -0700557static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500558 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500559 resize_grab_motion,
560 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500561};
562
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400563static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500564weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400565 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500566{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500567 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500568
Alex Wu4539b082012-03-01 12:57:46 +0800569 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
570 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500571
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200572 if (edges == 0 || edges > 15 ||
573 (edges & 3) == 3 || (edges & 12) == 12)
574 return 0;
575
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500576 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400577 if (!resize)
578 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500579
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300580 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
581
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500582 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200583 resize->width = shsurf->surface->geometry.width;
584 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400585
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300586 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500588
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400589 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400590
591 return 0;
592}
593
594static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200595shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400596 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200597 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400598{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500599 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200600 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400601
Alex Wu4539b082012-03-01 12:57:46 +0800602 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
603 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400604
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500605 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400606 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500607 wd->input_device.pointer_focus != &shsurf->surface->surface)
608 return;
609
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400610 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400611 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500612}
613
Juan Zhao96879df2012-02-07 08:45:41 +0800614static struct weston_output *
615get_default_output(struct weston_compositor *compositor)
616{
617 return container_of(compositor->output_list.next,
618 struct weston_output, link);
619}
620
Alex Wu4539b082012-03-01 12:57:46 +0800621static void
622shell_unset_fullscreen(struct shell_surface *shsurf)
623{
624 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800625 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
626 shell_surface_is_top_fullscreen(shsurf)) {
627 weston_output_switch_mode(shsurf->fullscreen_output,
628 shsurf->fullscreen_output->origin);
629 }
Alex Wu4539b082012-03-01 12:57:46 +0800630 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
631 shsurf->fullscreen.framerate = 0;
632 wl_list_remove(&shsurf->fullscreen.transform.link);
633 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800634 if (shsurf->fullscreen.black_surface)
635 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800636 shsurf->fullscreen.black_surface = NULL;
637 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300638 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800639 weston_surface_set_position(shsurf->surface,
640 shsurf->saved_x, shsurf->saved_y);
641}
642
Pekka Paalanen98262232011-12-01 10:42:22 +0200643static int
644reset_shell_surface_type(struct shell_surface *surface)
645{
646 switch (surface->type) {
647 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800648 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200649 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800650 case SHELL_SURFACE_MAXIMIZED:
651 surface->output = get_default_output(surface->surface->compositor);
652 weston_surface_set_position(surface->surface,
653 surface->saved_x,
654 surface->saved_y);
655 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200656 case SHELL_SURFACE_PANEL:
657 case SHELL_SURFACE_BACKGROUND:
658 wl_list_remove(&surface->link);
659 wl_list_init(&surface->link);
660 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200661 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200662 case SHELL_SURFACE_LOCK:
663 wl_resource_post_error(&surface->resource,
664 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200665 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200666 return -1;
667 case SHELL_SURFACE_NONE:
668 case SHELL_SURFACE_TOPLEVEL:
669 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500670 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200671 break;
672 }
673
674 surface->type = SHELL_SURFACE_NONE;
675 return 0;
676}
677
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500678static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200679shell_surface_set_toplevel(struct wl_client *client,
680 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400681
682{
Pekka Paalanen98262232011-12-01 10:42:22 +0200683 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400684
Pekka Paalanen98262232011-12-01 10:42:22 +0200685 if (reset_shell_surface_type(surface))
686 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400687
Pekka Paalanen98262232011-12-01 10:42:22 +0200688 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400689}
690
691static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200692shell_surface_set_transient(struct wl_client *client,
693 struct wl_resource *resource,
694 struct wl_resource *parent_resource,
695 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400696{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200697 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500698 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200699 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500700 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400701
Pekka Paalanen98262232011-12-01 10:42:22 +0200702 if (reset_shell_surface_type(shsurf))
703 return;
704
Alex Wu4539b082012-03-01 12:57:46 +0800705 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800706 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200707 weston_surface_set_position(es, pes->geometry.x + x,
708 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400709
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200710 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400711}
712
Tiago Vignattibe143262012-04-16 17:31:41 +0300713static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800714shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200715{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400716 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800717}
718
719static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300720get_output_panel_height(struct desktop_shell *shell,
721 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800722{
723 struct shell_surface *priv;
724 int panel_height = 0;
725
726 if (!output)
727 return 0;
728
Tiago Vignattibe143262012-04-16 17:31:41 +0300729 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800730 if (priv->output == output) {
731 panel_height = priv->surface->geometry.height;
732 break;
733 }
734 }
735 return panel_height;
736}
737
738static void
739shell_surface_set_maximized(struct wl_client *client,
740 struct wl_resource *resource,
741 struct wl_resource *output_resource )
742{
743 struct shell_surface *shsurf = resource->data;
744 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300745 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800746 uint32_t edges = 0, panel_height = 0;
747
748 /* get the default output, if the client set it as NULL
749 check whether the ouput is available */
750 if (output_resource)
751 shsurf->output = output_resource->data;
752 else
753 shsurf->output = get_default_output(es->compositor);
754
755 if (reset_shell_surface_type(shsurf))
756 return;
757
758 shsurf->saved_x = es->geometry.x;
759 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800760 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800761
Tiago Vignattibe143262012-04-16 17:31:41 +0300762 shell = shell_surface_get_shell(shsurf);
763 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800764 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500765
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400766 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500767 es->output->current->width,
768 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800769
770 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200771}
772
Alex Wu21858432012-04-01 20:13:08 +0800773static void
774black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
775
Alex Wu4539b082012-03-01 12:57:46 +0800776static struct weston_surface *
777create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800778 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800779 GLfloat x, GLfloat y, int w, int h)
780{
781 struct weston_surface *surface = NULL;
782
783 surface = weston_surface_create(ec);
784 if (surface == NULL) {
785 fprintf(stderr, "no memory\n");
786 return NULL;
787 }
788
Alex Wu21858432012-04-01 20:13:08 +0800789 surface->configure = black_surface_configure;
790 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800791 weston_surface_configure(surface, x, y, w, h);
792 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
793 return surface;
794}
795
796/* Create black surface and append it to the associated fullscreen surface.
797 * Handle size dismatch and positioning according to the method. */
798static void
799shell_configure_fullscreen(struct shell_surface *shsurf)
800{
801 struct weston_output *output = shsurf->fullscreen_output;
802 struct weston_surface *surface = shsurf->surface;
803 struct weston_matrix *matrix;
804 float scale;
805
806 center_on_output(surface, output);
807
808 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500809 shsurf->fullscreen.black_surface =
810 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800811 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500812 output->x, output->y,
813 output->current->width,
814 output->current->height);
815
816 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
817 wl_list_insert(&surface->layer_link,
818 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800819 shsurf->fullscreen.black_surface->output = output;
820
821 switch (shsurf->fullscreen.type) {
822 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
823 break;
824 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
825 matrix = &shsurf->fullscreen.transform.matrix;
826 weston_matrix_init(matrix);
827 scale = (float)output->current->width/(float)surface->geometry.width;
828 weston_matrix_scale(matrix, scale, scale, 1);
829 wl_list_remove(&shsurf->fullscreen.transform.link);
830 wl_list_insert(surface->geometry.transformation_list.prev,
831 &shsurf->fullscreen.transform.link);
832 weston_surface_set_position(surface, output->x, output->y);
833 break;
834 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800835 if (shell_surface_is_top_fullscreen(shsurf)) {
836 struct weston_mode mode = {0,
837 surface->geometry.width,
838 surface->geometry.height,
839 shsurf->fullscreen.framerate};
840
841 if (weston_output_switch_mode(output, &mode) == 0) {
842 weston_surface_configure(shsurf->fullscreen.black_surface,
843 output->x, output->y,
844 output->current->width,
845 output->current->height);
846 weston_surface_set_position(surface, output->x, output->y);
847 break;
848 }
849 }
Alex Wu4539b082012-03-01 12:57:46 +0800850 break;
851 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
852 break;
853 default:
854 break;
855 }
856}
857
858/* make the fullscreen and black surface at the top */
859static void
860shell_stack_fullscreen(struct shell_surface *shsurf)
861{
Alex Wubd3354b2012-04-17 17:20:49 +0800862 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800863 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300864 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800865
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500866 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500867 wl_list_insert(&shell->fullscreen_layer.surface_list,
868 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800869 weston_surface_damage(surface);
870
871 if (!shsurf->fullscreen.black_surface)
872 shsurf->fullscreen.black_surface =
873 create_black_surface(surface->compositor,
874 surface,
875 output->x, output->y,
876 output->current->width,
877 output->current->height);
878
879 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500880 wl_list_insert(&surface->layer_link,
881 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500882 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800883}
884
885static void
886shell_map_fullscreen(struct shell_surface *shsurf)
887{
Alex Wu4539b082012-03-01 12:57:46 +0800888 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800889 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800890}
891
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400892static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200893shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500894 struct wl_resource *resource,
895 uint32_t method,
896 uint32_t framerate,
897 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400898{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200899 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500900 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800901
902 if (output_resource)
903 shsurf->output = output_resource->data;
904 else
905 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400906
Pekka Paalanen98262232011-12-01 10:42:22 +0200907 if (reset_shell_surface_type(shsurf))
908 return;
909
Alex Wu4539b082012-03-01 12:57:46 +0800910 shsurf->fullscreen_output = shsurf->output;
911 shsurf->fullscreen.type = method;
912 shsurf->fullscreen.framerate = framerate;
913 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400914
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200915 shsurf->saved_x = es->geometry.x;
916 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800917 shsurf->saved_position_valid = true;
918
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300919 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300920 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500921
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400922 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500923 shsurf->output->current->width,
924 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400925}
926
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500927static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400928popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500929 struct wl_surface *surface, int32_t x, int32_t y)
930{
931 struct wl_input_device *device = grab->input_device;
932 struct shell_surface *priv =
933 container_of(grab, struct shell_surface, popup.grab);
934 struct wl_client *client = priv->surface->surface.resource.client;
935
Pekka Paalanencb108432012-01-19 16:25:40 +0200936 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400937 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500938 grab->focus = surface;
939 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400940 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500941 grab->focus = NULL;
942 }
943}
944
945static void
Scott Moreau447013d2012-02-18 05:05:29 -0700946popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200947 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500948{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500949 struct wl_resource *resource;
950
951 resource = grab->input_device->pointer_focus_resource;
952 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500953 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500954}
955
956static void
Scott Moreau447013d2012-02-18 05:05:29 -0700957popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400958 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500959{
960 struct wl_resource *resource;
961 struct shell_surface *shsurf =
962 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400963 struct wl_display *display;
964 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500965
966 resource = grab->input_device->pointer_focus_resource;
967 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400968 display = wl_client_get_display(resource->client);
969 serial = wl_display_get_serial(display);
970 wl_input_device_send_button(resource, serial,
971 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500972 } else if (state == 0 &&
973 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400974 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500975 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400976 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500977 shsurf->popup.grab.input_device = NULL;
978 }
979
980 if (state == 0)
981 shsurf->popup.initial_up = 1;
982}
983
Scott Moreau447013d2012-02-18 05:05:29 -0700984static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500985 popup_grab_focus,
986 popup_grab_motion,
987 popup_grab_button,
988};
989
990static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400991shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500992{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400993 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500994 struct weston_surface *es = shsurf->surface;
995 struct weston_surface *parent = shsurf->parent->surface;
996
997 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500998 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500999
Pekka Paalanen938269a2012-02-07 14:19:01 +02001000 weston_surface_update_transform(parent);
1001 if (parent->transform.enabled) {
1002 shsurf->popup.parent_transform.matrix =
1003 parent->transform.matrix;
1004 } else {
1005 /* construct x, y translation matrix */
1006 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1007 shsurf->popup.parent_transform.matrix.d[12] =
1008 parent->geometry.x;
1009 shsurf->popup.parent_transform.matrix.d[13] =
1010 parent->geometry.y;
1011 }
1012 wl_list_insert(es->geometry.transformation_list.prev,
1013 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001014 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001015
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001016 shsurf->popup.initial_up = 0;
1017
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001018 /* We don't require the grab to still be active, but if another
1019 * grab has started in the meantime, we end the popup now. */
1020 if (device->grab_serial == shsurf->popup.serial) {
1021 wl_input_device_start_pointer_grab(device,
1022 &shsurf->popup.grab);
1023 } else {
1024 wl_shell_surface_send_popup_done(&shsurf->resource);
1025 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001026}
1027
1028static void
1029shell_surface_set_popup(struct wl_client *client,
1030 struct wl_resource *resource,
1031 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001032 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001033 struct wl_resource *parent_resource,
1034 int32_t x, int32_t y, uint32_t flags)
1035{
1036 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001037
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001038 shsurf->type = SHELL_SURFACE_POPUP;
1039 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001040 shsurf->popup.device = input_device_resource->data;
1041 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001042 shsurf->popup.x = x;
1043 shsurf->popup.y = y;
1044}
1045
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001046static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001047 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001048 shell_surface_move,
1049 shell_surface_resize,
1050 shell_surface_set_toplevel,
1051 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001052 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001053 shell_surface_set_popup,
1054 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001055};
1056
1057static void
1058destroy_shell_surface(struct wl_resource *resource)
1059{
1060 struct shell_surface *shsurf = resource->data;
1061
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001062 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001063 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001064
Alex Wubd3354b2012-04-17 17:20:49 +08001065 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1066 shell_surface_is_top_fullscreen(shsurf)) {
1067 weston_output_switch_mode(shsurf->fullscreen_output,
1068 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001069 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001070
Alex Wuaa08e2d2012-03-05 11:01:40 +08001071 if (shsurf->fullscreen.black_surface)
1072 weston_surface_destroy(shsurf->fullscreen.black_surface);
1073
Alex Wubd3354b2012-04-17 17:20:49 +08001074 /* As destroy_resource() use wl_list_for_each_safe(),
1075 * we can always remove the listener.
1076 */
1077 wl_list_remove(&shsurf->surface_destroy_listener.link);
1078 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001079 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001080
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001081 wl_list_remove(&shsurf->link);
1082 free(shsurf);
1083}
1084
1085static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001086shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087{
1088 struct shell_surface *shsurf = container_of(listener,
1089 struct shell_surface,
1090 surface_destroy_listener);
1091
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001092 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001093}
1094
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001095static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001096get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001097{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001098 struct wl_listener *listener;
1099
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001100 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1101 shell_handle_surface_destroy);
1102 if (listener)
1103 return container_of(listener, struct shell_surface,
1104 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001105
1106 return NULL;
1107}
1108
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001109static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001110shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1111
1112static void
Pekka Paalanen46229672011-11-29 15:49:31 +02001113shell_get_shell_surface(struct wl_client *client,
1114 struct wl_resource *resource,
1115 uint32_t id,
1116 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001117{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001118 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001119 struct shell_surface *shsurf;
1120
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001121 if (get_shell_surface(surface)) {
1122 wl_resource_post_error(surface_resource,
1123 WL_DISPLAY_ERROR_INVALID_OBJECT,
Alex Wubd3354b2012-04-17 17:20:49 +08001124 "desktop_shell::get_shell_surface already requested");
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001125 return;
1126 }
1127
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001128 if (surface->configure) {
1129 wl_resource_post_error(surface_resource,
1130 WL_DISPLAY_ERROR_INVALID_OBJECT,
1131 "surface->configure already set");
1132 return;
1133 }
1134
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001135 shsurf = calloc(1, sizeof *shsurf);
1136 if (!shsurf) {
1137 wl_resource_post_no_memory(resource);
1138 return;
1139 }
1140
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001141 surface->configure = shell_surface_configure;
1142
Scott Moreauff1db4a2012-04-17 19:06:18 -06001143 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001144 shsurf->unresponsive_animation.exists = 0;
1145 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001146 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001147
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001148 shsurf->resource.destroy = destroy_shell_surface;
1149 shsurf->resource.object.id = id;
1150 shsurf->resource.object.interface = &wl_shell_surface_interface;
1151 shsurf->resource.object.implementation =
1152 (void (**)(void)) &shell_surface_implementation;
1153 shsurf->resource.data = shsurf;
1154
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001155 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +08001156 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001157 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001158 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1159 shsurf->fullscreen.framerate = 0;
1160 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001161 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001162 wl_list_init(&shsurf->fullscreen.transform.link);
1163
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001164 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1165 wl_signal_add(&surface->surface.resource.destroy_signal,
1166 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001167
1168 /* init link so its safe to always remove it in destroy_shell_surface */
1169 wl_list_init(&shsurf->link);
1170
Pekka Paalanen460099f2012-01-20 16:48:25 +02001171 /* empty when not in use */
1172 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001173 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001174
Pekka Paalanen98262232011-12-01 10:42:22 +02001175 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001176
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177 wl_client_add_resource(client, &shsurf->resource);
1178}
1179
1180static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001181 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001182};
1183
Kristian Høgsberg07937562011-04-12 17:25:42 -04001184static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001185handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001186{
1187 proc->pid = 0;
1188}
1189
1190static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001191launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001192{
1193 if (shell->screensaver.binding)
1194 return;
1195
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001196 if (!shell->screensaver.path)
1197 return;
1198
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001199 if (shell->screensaver.process.pid != 0) {
1200 fprintf(stderr, "old screensaver still running\n");
1201 return;
1202 }
1203
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001204 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001205 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001206 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001207 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001208}
1209
1210static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001211terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001212{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001213 if (shell->screensaver.process.pid == 0)
1214 return;
1215
1216 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001217}
1218
1219static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001220show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001221{
1222 struct wl_list *list;
1223
1224 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001226 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001227 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001228
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001229 wl_list_remove(&surface->surface->layer_link);
1230 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001231 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001232 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001233}
1234
1235static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001236hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001237{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001238 wl_list_remove(&surface->surface->layer_link);
1239 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001240 surface->surface->output = NULL;
1241}
1242
1243static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001244desktop_shell_set_background(struct wl_client *client,
1245 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001246 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001247 struct wl_resource *surface_resource)
1248{
Tiago Vignattibe143262012-04-16 17:31:41 +03001249 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001250 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001251 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001252 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001253
Pekka Paalanen98262232011-12-01 10:42:22 +02001254 if (reset_shell_surface_type(shsurf))
1255 return;
1256
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001257 wl_list_for_each(priv, &shell->backgrounds, link) {
1258 if (priv->output == output_resource->data) {
1259 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001260 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001261 wl_list_remove(&priv->link);
1262 break;
1263 }
1264 }
1265
Pekka Paalanen068ae942011-11-28 14:11:15 +02001266 shsurf->type = SHELL_SURFACE_BACKGROUND;
1267 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001268
Pekka Paalanen068ae942011-11-28 14:11:15 +02001269 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001270
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001271 weston_surface_set_position(surface, shsurf->output->x,
1272 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001273
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001274 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001275 surface_resource,
1276 shsurf->output->current->width,
1277 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001278}
1279
1280static void
1281desktop_shell_set_panel(struct wl_client *client,
1282 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001283 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001284 struct wl_resource *surface_resource)
1285{
Tiago Vignattibe143262012-04-16 17:31:41 +03001286 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001287 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001288 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001289 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001290
Pekka Paalanen98262232011-12-01 10:42:22 +02001291 if (reset_shell_surface_type(shsurf))
1292 return;
1293
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001294 wl_list_for_each(priv, &shell->panels, link) {
1295 if (priv->output == output_resource->data) {
1296 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001297 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001298 wl_list_remove(&priv->link);
1299 break;
1300 }
1301 }
1302
Pekka Paalanen068ae942011-11-28 14:11:15 +02001303 shsurf->type = SHELL_SURFACE_PANEL;
1304 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001305
Pekka Paalanen068ae942011-11-28 14:11:15 +02001306 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001307
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001308 weston_surface_set_position(surface, shsurf->output->x,
1309 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001310
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001311 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001312 surface_resource,
1313 shsurf->output->current->width,
1314 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001315}
1316
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001317static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001318handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001319{
Tiago Vignattibe143262012-04-16 17:31:41 +03001320 struct desktop_shell *shell =
1321 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001322
1323 fprintf(stderr, "lock surface gone\n");
1324 shell->lock_surface = NULL;
1325}
1326
1327static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001328desktop_shell_set_lock_surface(struct wl_client *client,
1329 struct wl_resource *resource,
1330 struct wl_resource *surface_resource)
1331{
Tiago Vignattibe143262012-04-16 17:31:41 +03001332 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001333 struct shell_surface *surface = surface_resource->data;
1334
1335 if (reset_shell_surface_type(surface))
1336 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001337
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001338 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001339
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001340 if (!shell->locked)
1341 return;
1342
Pekka Paalanen98262232011-12-01 10:42:22 +02001343 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001344
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001345 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1346 wl_signal_add(&surface_resource->destroy_signal,
1347 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001348
Pekka Paalanen068ae942011-11-28 14:11:15 +02001349 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001350}
1351
1352static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001353resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001354{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001355 struct shell_surface *tmp;
1356
1357 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1358 hide_screensaver(shell, tmp);
1359
1360 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001361
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001362 wl_list_remove(&shell->lock_layer.link);
1363 wl_list_insert(&shell->compositor->cursor_layer.link,
1364 &shell->fullscreen_layer.link);
1365 wl_list_insert(&shell->fullscreen_layer.link,
1366 &shell->panel_layer.link);
1367 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001368
1369 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001370 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001371 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001372 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001373}
1374
1375static void
1376desktop_shell_unlock(struct wl_client *client,
1377 struct wl_resource *resource)
1378{
Tiago Vignattibe143262012-04-16 17:31:41 +03001379 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001380
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001381 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001382
1383 if (shell->locked)
1384 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001385}
1386
Kristian Høgsberg75840622011-09-06 13:48:16 -04001387static const struct desktop_shell_interface desktop_shell_implementation = {
1388 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001389 desktop_shell_set_panel,
1390 desktop_shell_set_lock_surface,
1391 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001392};
1393
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001394static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001395get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001396{
1397 struct shell_surface *shsurf;
1398
1399 shsurf = get_shell_surface(surface);
1400 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001401 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001402 return shsurf->type;
1403}
1404
Kristian Høgsberg75840622011-09-06 13:48:16 -04001405static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001406move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001407 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001408{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001409 struct weston_surface *surface =
1410 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001411
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001412 if (surface == NULL)
1413 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001414
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001415 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001416 case SHELL_SURFACE_PANEL:
1417 case SHELL_SURFACE_BACKGROUND:
1418 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001419 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001420 return;
1421 default:
1422 break;
1423 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001424
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001425 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001426}
1427
1428static void
1429resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001430 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001431{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001432 struct weston_surface *surface =
1433 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001434 uint32_t edges = 0;
1435 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001436 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001437
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001438 if (surface == NULL)
1439 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001440
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001441 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001442 if (!shsurf)
1443 return;
1444
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001445 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001446 case SHELL_SURFACE_PANEL:
1447 case SHELL_SURFACE_BACKGROUND:
1448 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001449 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001450 return;
1451 default:
1452 break;
1453 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001454
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001455 weston_surface_from_global(surface,
1456 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001457
Pekka Paalanen60921e52012-01-25 15:55:43 +02001458 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001459 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001460 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001461 edges |= 0;
1462 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001463 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001464
Pekka Paalanen60921e52012-01-25 15:55:43 +02001465 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001466 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001467 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001468 edges |= 0;
1469 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001470 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001471
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001472 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001473 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001474}
1475
1476static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001477surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1478 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1479{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001480 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001481 struct shell_surface *shsurf;
1482 struct weston_surface *surface =
1483 (struct weston_surface *) device->pointer_focus;
1484
1485 if (surface == NULL)
1486 return;
1487
1488 shsurf = get_shell_surface(surface);
1489 if (!shsurf)
1490 return;
1491
1492 switch (shsurf->type) {
1493 case SHELL_SURFACE_BACKGROUND:
1494 case SHELL_SURFACE_SCREENSAVER:
1495 return;
1496 default:
1497 break;
1498 }
1499
1500 surface->alpha += value * step;
1501
1502 if (surface->alpha > 255)
1503 surface->alpha = 255;
1504 if (surface->alpha < step)
1505 surface->alpha = step;
1506
1507 surface->geometry.dirty = 1;
1508 weston_surface_damage(surface);
1509}
1510
1511static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001512zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001513 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001514{
1515 struct weston_input_device *wd = (struct weston_input_device *) device;
1516 struct weston_compositor *compositor = wd->compositor;
1517 struct weston_output *output;
1518
1519 wl_list_for_each(output, &compositor->output_list, link) {
1520 if (pixman_region32_contains_point(&output->region,
1521 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001522 output->zoom.active = 1;
1523 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001524
1525 if (output->zoom.level >= 1.0) {
1526 output->zoom.active = 0;
1527 output->zoom.level = 1.0;
1528 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001529
1530 if (output->zoom.level < output->zoom.increment)
1531 output->zoom.level = output->zoom.increment;
1532
1533 weston_output_update_zoom(output, device->x, device->y);
1534 }
1535 }
1536}
1537
Scott Moreauccbf29d2012-02-22 14:21:41 -07001538static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001539terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001540 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001543
1544 if (state)
1545 wl_display_terminate(compositor->wl_display);
1546}
1547
1548static void
Scott Moreau447013d2012-02-18 05:05:29 -07001549rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001550 uint32_t time, int32_t x, int32_t y)
1551{
1552 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001553 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001554 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001555 struct shell_surface *shsurf = rotate->base.shsurf;
1556 struct weston_surface *surface;
1557 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1558
1559 if (!shsurf)
1560 return;
1561
1562 surface = shsurf->surface;
1563
1564 cx = 0.5f * surface->geometry.width;
1565 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001566
1567 dx = device->x - rotate->center.x;
1568 dy = device->y - rotate->center.y;
1569 r = sqrtf(dx * dx + dy * dy);
1570
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001571 wl_list_remove(&shsurf->rotation.transform.link);
1572 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001573
1574 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001575 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001576 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001577
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001578 weston_matrix_init(&rotate->rotation);
1579 rotate->rotation.d[0] = dx / r;
1580 rotate->rotation.d[4] = -dy / r;
1581 rotate->rotation.d[1] = -rotate->rotation.d[4];
1582 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001583
1584 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001585 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001586 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001587 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001588 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001589
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001590 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001591 &shsurf->surface->geometry.transformation_list,
1592 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001593 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001594 wl_list_init(&shsurf->rotation.transform.link);
1595 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001596 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001597 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001598
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001599 /* We need to adjust the position of the surface
1600 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001601 cposx = surface->geometry.x + cx;
1602 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001603 dposx = rotate->center.x - cposx;
1604 dposy = rotate->center.y - cposy;
1605 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001606 weston_surface_set_position(surface,
1607 surface->geometry.x + dposx,
1608 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001609 }
1610
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001611 /* Repaint implies weston_surface_update_transform(), which
1612 * lazily applies the damage due to rotation update.
1613 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001614 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001615}
1616
1617static void
Scott Moreau447013d2012-02-18 05:05:29 -07001618rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001619 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001620{
1621 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001622 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001623 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001624 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001625
1626 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001627 if (shsurf)
1628 weston_matrix_multiply(&shsurf->rotation.rotation,
1629 &rotate->rotation);
1630 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001631 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001632 free(rotate);
1633 }
1634}
1635
Scott Moreau447013d2012-02-18 05:05:29 -07001636static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001637 noop_grab_focus,
1638 rotate_grab_motion,
1639 rotate_grab_button,
1640};
1641
1642static void
1643rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001644 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001645{
1646 struct weston_surface *base_surface =
1647 (struct weston_surface *) device->pointer_focus;
1648 struct shell_surface *surface;
1649 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001650 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001651 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001652
1653 if (base_surface == NULL)
1654 return;
1655
1656 surface = get_shell_surface(base_surface);
1657 if (!surface)
1658 return;
1659
1660 switch (surface->type) {
1661 case SHELL_SURFACE_PANEL:
1662 case SHELL_SURFACE_BACKGROUND:
1663 case SHELL_SURFACE_FULLSCREEN:
1664 case SHELL_SURFACE_SCREENSAVER:
1665 return;
1666 default:
1667 break;
1668 }
1669
Pekka Paalanen460099f2012-01-20 16:48:25 +02001670 rotate = malloc(sizeof *rotate);
1671 if (!rotate)
1672 return;
1673
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001674 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001675
1676 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001677 surface->surface->geometry.width / 2,
1678 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001679 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001680
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001681 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001682
1683 dx = device->x - rotate->center.x;
1684 dy = device->y - rotate->center.y;
1685 r = sqrtf(dx * dx + dy * dy);
1686 if (r > 20.0f) {
1687 struct weston_matrix inverse;
1688
1689 weston_matrix_init(&inverse);
1690 inverse.d[0] = dx / r;
1691 inverse.d[4] = dy / r;
1692 inverse.d[1] = -inverse.d[4];
1693 inverse.d[5] = inverse.d[0];
1694 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001695
1696 weston_matrix_init(&rotate->rotation);
1697 rotate->rotation.d[0] = dx / r;
1698 rotate->rotation.d[4] = -dy / r;
1699 rotate->rotation.d[1] = -rotate->rotation.d[4];
1700 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001701 } else {
1702 weston_matrix_init(&surface->rotation.rotation);
1703 weston_matrix_init(&rotate->rotation);
1704 }
1705
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001706 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001707}
1708
1709static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001710activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001711 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001712{
Alex Wu21858432012-04-01 20:13:08 +08001713 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001714
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001715 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001716
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001717 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001718 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001719 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001720 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001721 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001722
Pekka Paalanen77346a62011-11-30 16:26:35 +02001723 case SHELL_SURFACE_SCREENSAVER:
1724 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001725 if (shell->lock_surface)
1726 weston_surface_restack(es,
1727 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001728 break;
Alex Wu4539b082012-03-01 12:57:46 +08001729 case SHELL_SURFACE_FULLSCREEN:
1730 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001731 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001732 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001733 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001734 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001735 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001736 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1737 wl_list_for_each_reverse_safe(surf,
1738 prev,
1739 &shell->fullscreen_layer.surface_list,
1740 layer_link)
1741 weston_surface_restack(surf,
1742 &shell->toplevel_layer.surface_list);
1743 }
1744
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001745 weston_surface_restack(es,
1746 &shell->toplevel_layer.surface_list);
1747 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001748 }
1749}
1750
Alex Wu21858432012-04-01 20:13:08 +08001751/* no-op func for checking black surface */
1752static void
1753black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1754{
1755}
1756
1757static bool
1758is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1759{
1760 if (es->configure == black_surface_configure) {
1761 if (fs_surface)
1762 *fs_surface = (struct weston_surface *)es->private;
1763 return true;
1764 }
1765 return false;
1766}
1767
Kristian Høgsberg75840622011-09-06 13:48:16 -04001768static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001769click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001770 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001771 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001774 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001775 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001776 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001777
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001778 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001779 if (!focus)
1780 return;
1781
Alex Wu21858432012-04-01 20:13:08 +08001782 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001783 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001784
Alex Wu9c35e6b2012-03-05 14:13:13 +08001785 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001786 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001787}
1788
1789static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001790lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001791{
Tiago Vignattibe143262012-04-16 17:31:41 +03001792 struct desktop_shell *shell =
1793 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001794 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001795 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001796 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001797
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001798 if (shell->locked) {
1799 wl_list_for_each(output, &shell->compositor->output_list, link)
1800 /* TODO: find a way to jump to other DPMS levels */
1801 if (output->set_dpms)
1802 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001803 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001804 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001805
1806 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001807
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001808 /* Hide all surfaces by removing the fullscreen, panel and
1809 * toplevel layers. This way nothing else can show or receive
1810 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001811
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001812 wl_list_remove(&shell->panel_layer.link);
1813 wl_list_remove(&shell->toplevel_layer.link);
1814 wl_list_remove(&shell->fullscreen_layer.link);
1815 wl_list_insert(&shell->compositor->cursor_layer.link,
1816 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001817
Pekka Paalanen77346a62011-11-30 16:26:35 +02001818 launch_screensaver(shell);
1819
1820 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1821 show_screensaver(shell, shsurf);
1822
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001823 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001824 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001825 weston_compositor_wake(shell->compositor);
1826 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001827 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001828
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001829 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001830 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001831
1832 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001833 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1834 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001835 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001836 }
1837
1838 /* TODO: disable bindings that should not work while locked. */
1839
1840 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001841}
1842
1843static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001844unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001845{
Tiago Vignattibe143262012-04-16 17:31:41 +03001846 struct desktop_shell *shell =
1847 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001848
Pekka Paalanend81c2162011-11-16 13:47:34 +02001849 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001851 return;
1852 }
1853
1854 /* If desktop-shell client has gone away, unlock immediately. */
1855 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001856 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001857 return;
1858 }
1859
1860 if (shell->prepare_event_sent)
1861 return;
1862
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001863 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001864 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001865}
1866
1867static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001868center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001869{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001870 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001871 GLfloat x = (mode->width - surface->geometry.width) / 2;
1872 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001873
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001874 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001875}
1876
1877static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001878map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001879 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001880{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001881 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001882 struct shell_surface *shsurf;
1883 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001884 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001885 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001886
Pekka Paalanen77346a62011-11-30 16:26:35 +02001887 shsurf = get_shell_surface(surface);
1888 if (shsurf)
1889 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001890
Pekka Paalanen60921e52012-01-25 15:55:43 +02001891 surface->geometry.width = width;
1892 surface->geometry.height = height;
1893 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001894
1895 /* initial positioning, see also configure() */
1896 switch (surface_type) {
1897 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001898 weston_surface_set_position(surface, 10 + random() % 400,
1899 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001900 break;
1901 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001902 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001903 break;
Alex Wu4539b082012-03-01 12:57:46 +08001904 case SHELL_SURFACE_FULLSCREEN:
1905 shell_map_fullscreen(shsurf);
1906 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001907 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001908 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001909 panel_height = get_output_panel_height(shell,surface->output);
1910 weston_surface_set_position(surface, surface->output->x,
1911 surface->output->y + panel_height);
1912 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001913 case SHELL_SURFACE_LOCK:
1914 center_on_output(surface, get_default_output(compositor));
1915 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001916 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001917 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001918 case SHELL_SURFACE_NONE:
1919 weston_surface_set_position(surface,
1920 surface->geometry.x + sx,
1921 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001922 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001923 default:
1924 ;
1925 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001926
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001927 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001928 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001929 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001930 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001931 wl_list_insert(&shell->background_layer.surface_list,
1932 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001933 break;
1934 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001935 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001936 wl_list_insert(&shell->panel_layer.surface_list,
1937 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001938 break;
1939 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001940 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001941 wl_list_insert(&shell->lock_layer.surface_list,
1942 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001943 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001944 break;
1945 case SHELL_SURFACE_SCREENSAVER:
1946 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001947 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001948 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001949 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001950 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001951 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001952 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001953 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001954 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001955 case SHELL_SURFACE_POPUP:
1956 case SHELL_SURFACE_TRANSIENT:
1957 parent = shsurf->parent->surface;
1958 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1959 break;
Alex Wu4539b082012-03-01 12:57:46 +08001960 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001961 case SHELL_SURFACE_NONE:
1962 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001963 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001964 wl_list_insert(&shell->toplevel_layer.surface_list,
1965 &surface->layer_link);
1966 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001967 }
1968
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001969 if (surface_type != SHELL_SURFACE_NONE) {
1970 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001971 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1972 surface->output = shsurf->output;
1973 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001974
Juan Zhao7bb92f02011-12-15 11:31:51 -05001975 switch (surface_type) {
1976 case SHELL_SURFACE_TOPLEVEL:
1977 case SHELL_SURFACE_TRANSIENT:
1978 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001979 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001980 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001981 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001982 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001983 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001984 break;
1985 default:
1986 break;
1987 }
1988
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001989 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001990 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001991}
1992
1993static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001994configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001995 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001996{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001997 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1998 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001999
Pekka Paalanen77346a62011-11-30 16:26:35 +02002000 shsurf = get_shell_surface(surface);
2001 if (shsurf)
2002 surface_type = shsurf->type;
2003
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002004 surface->geometry.x = x;
2005 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002006 surface->geometry.width = width;
2007 surface->geometry.height = height;
2008 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002009
2010 switch (surface_type) {
2011 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002012 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002013 break;
Alex Wu4539b082012-03-01 12:57:46 +08002014 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002015 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002016 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002017 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002018 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002019 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002020 surface->geometry.x = surface->output->x;
2021 surface->geometry.y = surface->output->y +
2022 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002023 break;
Alex Wu4539b082012-03-01 12:57:46 +08002024 case SHELL_SURFACE_TOPLEVEL:
2025 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002026 default:
2027 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002028 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002029
Alex Wu4539b082012-03-01 12:57:46 +08002030 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002031 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002032 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002033
2034 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2035 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002036 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2037 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002038 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002039}
2040
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002041static void
2042shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2043{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002044 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002045 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002046
2047 if (!weston_surface_is_mapped(es)) {
2048 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002049 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002050 es->geometry.width != es->buffer->width ||
2051 es->geometry.height != es->buffer->height) {
2052 GLfloat from_x, from_y;
2053 GLfloat to_x, to_y;
2054
2055 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2056 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2057 configure(shell, es,
2058 es->geometry.x + to_x - from_x,
2059 es->geometry.y + to_y - from_y,
2060 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002061 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002062 }
2063}
2064
Tiago Vignattibe143262012-04-16 17:31:41 +03002065static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002066
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002067static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002068desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002069{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002070 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002071 struct desktop_shell *shell =
2072 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002073
2074 shell->child.process.pid = 0;
2075 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002076
2077 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2078 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002079 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002080 shell->child.deathstamp = time;
2081 shell->child.deathcount = 0;
2082 }
2083
2084 shell->child.deathcount++;
2085 if (shell->child.deathcount > 5) {
2086 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2087 return;
2088 }
2089
2090 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2091 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002092}
2093
2094static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002095launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002096{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002097 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002098
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002099 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002100 &shell->child.process,
2101 shell_exe,
2102 desktop_shell_sigchld);
2103
2104 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002105 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002106 return 0;
2107}
2108
2109static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002110bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2111{
Tiago Vignattibe143262012-04-16 17:31:41 +03002112 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002113
2114 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002115 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002116}
2117
Kristian Høgsberg75840622011-09-06 13:48:16 -04002118static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002119unbind_desktop_shell(struct wl_resource *resource)
2120{
Tiago Vignattibe143262012-04-16 17:31:41 +03002121 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002122
2123 if (shell->locked)
2124 resume_desktop(shell);
2125
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002126 shell->child.desktop_shell = NULL;
2127 shell->prepare_event_sent = false;
2128 free(resource);
2129}
2130
2131static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002132bind_desktop_shell(struct wl_client *client,
2133 void *data, uint32_t version, uint32_t id)
2134{
Tiago Vignattibe143262012-04-16 17:31:41 +03002135 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002136 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002137
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002138 resource = wl_client_add_object(client, &desktop_shell_interface,
2139 &desktop_shell_implementation,
2140 id, shell);
2141
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002142 if (client == shell->child.client) {
2143 resource->destroy = unbind_desktop_shell;
2144 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002145 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002146 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002147
2148 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2149 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002150 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002151}
2152
Pekka Paalanen6e168112011-11-24 11:34:05 +02002153static void
2154screensaver_set_surface(struct wl_client *client,
2155 struct wl_resource *resource,
2156 struct wl_resource *shell_surface_resource,
2157 struct wl_resource *output_resource)
2158{
Tiago Vignattibe143262012-04-16 17:31:41 +03002159 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002160 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002161 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002162
Pekka Paalanen98262232011-12-01 10:42:22 +02002163 if (reset_shell_surface_type(surface))
2164 return;
2165
Pekka Paalanen77346a62011-11-30 16:26:35 +02002166 surface->type = SHELL_SURFACE_SCREENSAVER;
2167
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002168 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002169 surface->output = output;
2170 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002171}
2172
2173static const struct screensaver_interface screensaver_implementation = {
2174 screensaver_set_surface
2175};
2176
2177static void
2178unbind_screensaver(struct wl_resource *resource)
2179{
Tiago Vignattibe143262012-04-16 17:31:41 +03002180 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002181
Pekka Paalanen77346a62011-11-30 16:26:35 +02002182 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002183 free(resource);
2184}
2185
2186static void
2187bind_screensaver(struct wl_client *client,
2188 void *data, uint32_t version, uint32_t id)
2189{
Tiago Vignattibe143262012-04-16 17:31:41 +03002190 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002191 struct wl_resource *resource;
2192
2193 resource = wl_client_add_object(client, &screensaver_interface,
2194 &screensaver_implementation,
2195 id, shell);
2196
Pekka Paalanen77346a62011-11-30 16:26:35 +02002197 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002198 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002199 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002200 return;
2201 }
2202
2203 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2204 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002205 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002206}
2207
Kristian Høgsberg07045392012-02-19 18:52:44 -05002208struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002209 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002210 struct weston_surface *current;
2211 struct wl_listener listener;
2212 struct wl_keyboard_grab grab;
2213};
2214
2215static void
2216switcher_next(struct switcher *switcher)
2217{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002218 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002219 struct weston_surface *surface;
2220 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002221 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002222
2223 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002224 switch (get_shell_surface_type(surface)) {
2225 case SHELL_SURFACE_TOPLEVEL:
2226 case SHELL_SURFACE_FULLSCREEN:
2227 case SHELL_SURFACE_MAXIMIZED:
2228 if (first == NULL)
2229 first = surface;
2230 if (prev == switcher->current)
2231 next = surface;
2232 prev = surface;
2233 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002234 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002235 weston_surface_damage(surface);
2236 break;
2237 default:
2238 break;
2239 }
Alex Wu1659daa2012-04-01 20:13:09 +08002240
2241 if (is_black_surface(surface, NULL)) {
2242 surface->alpha = 64;
2243 surface->geometry.dirty = 1;
2244 weston_surface_damage(surface);
2245 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002246 }
2247
2248 if (next == NULL)
2249 next = first;
2250
Alex Wu07b26062012-03-12 16:06:01 +08002251 if (next == NULL)
2252 return;
2253
Kristian Høgsberg07045392012-02-19 18:52:44 -05002254 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002255 wl_signal_add(&next->surface.resource.destroy_signal,
2256 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002257
2258 switcher->current = next;
2259 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002260
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002261 shsurf = get_shell_surface(switcher->current);
2262 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2263 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002264}
2265
2266static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002267switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002268{
2269 struct switcher *switcher =
2270 container_of(listener, struct switcher, listener);
2271
2272 switcher_next(switcher);
2273}
2274
2275static void
2276switcher_destroy(struct switcher *switcher, uint32_t time)
2277{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002278 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002279 struct weston_surface *surface;
2280 struct weston_input_device *device =
2281 (struct weston_input_device *) switcher->grab.input_device;
2282
2283 wl_list_for_each(surface, &compositor->surface_list, link) {
2284 surface->alpha = 255;
2285 weston_surface_damage(surface);
2286 }
2287
Alex Wu07b26062012-03-12 16:06:01 +08002288 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002289 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002290 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002291 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002292 free(switcher);
2293}
2294
2295static void
2296switcher_key(struct wl_keyboard_grab *grab,
2297 uint32_t time, uint32_t key, int32_t state)
2298{
2299 struct switcher *switcher = container_of(grab, struct switcher, grab);
2300 struct weston_input_device *device =
2301 (struct weston_input_device *) grab->input_device;
2302
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002303 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002304 switcher_destroy(switcher, time);
2305 } else if (key == KEY_TAB && state) {
2306 switcher_next(switcher);
2307 }
2308};
2309
2310static const struct wl_keyboard_grab_interface switcher_grab = {
2311 switcher_key
2312};
2313
2314static void
2315switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002316 uint32_t key, uint32_t button, uint32_t axis,
2317 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002318{
Tiago Vignattibe143262012-04-16 17:31:41 +03002319 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002320 struct switcher *switcher;
2321
2322 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002323 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002324 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002325 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002326 wl_list_init(&switcher->listener.link);
2327
2328 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002329 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2330 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002331 switcher_next(switcher);
2332}
2333
Pekka Paalanen3c647232011-12-22 13:43:43 +02002334static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002335backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002336 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002337{
2338 struct weston_compositor *compositor = data;
2339 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002340 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002341
2342 /* TODO: we're limiting to simple use cases, where we assume just
2343 * control on the primary display. We'd have to extend later if we
2344 * ever get support for setting backlights on random desktop LCD
2345 * panels though */
2346 output = get_default_output(compositor);
2347 if (!output)
2348 return;
2349
2350 if (!output->set_backlight)
2351 return;
2352
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002353 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2354 backlight_new = output->backlight_current - 25;
2355 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2356 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002357
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002358 if (backlight_new < 5)
2359 backlight_new = 5;
2360 if (backlight_new > 255)
2361 backlight_new = 255;
2362
2363 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002364 output->set_backlight(output, output->backlight_current);
2365}
2366
2367static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002368debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002369 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002370{
Tiago Vignattibe143262012-04-16 17:31:41 +03002371 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002372 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002373 struct weston_surface *surface;
2374
2375 if (shell->debug_repaint_surface) {
2376 weston_surface_destroy(shell->debug_repaint_surface);
2377 shell->debug_repaint_surface = NULL;
2378 } else {
2379 surface = weston_surface_create(compositor);
2380 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2381 weston_surface_configure(surface, 0, 0, 8192, 8192);
2382 wl_list_insert(&compositor->fade_layer.surface_list,
2383 &surface->layer_link);
2384 weston_surface_assign_output(surface);
2385 pixman_region32_init(&surface->input);
2386
2387 /* Here's the dirty little trick that makes the
2388 * repaint debugging work: we force an
2389 * update_transform first to update dependent state
2390 * and clear the geometry.dirty bit. Then we clear
2391 * the surface damage so it only gets repainted
2392 * piecewise as we repaint other things. */
2393
2394 weston_surface_update_transform(surface);
2395 pixman_region32_fini(&surface->damage);
2396 pixman_region32_init(&surface->damage);
2397 shell->debug_repaint_surface = surface;
2398 }
2399}
2400
2401static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002402shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002403{
Tiago Vignattibe143262012-04-16 17:31:41 +03002404 struct desktop_shell *shell =
2405 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002406
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002407 if (shell->child.client)
2408 wl_client_destroy(shell->child.client);
2409
Pekka Paalanen3c647232011-12-22 13:43:43 +02002410 free(shell->screensaver.path);
2411 free(shell);
2412}
2413
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002414static void
2415shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2416{
2417 uint32_t mod;
2418
2419 /* fixed bindings */
2420 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2421 MODIFIER_CTRL | MODIFIER_ALT,
2422 terminate_binding, ec);
2423 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2424 click_to_activate_binding, shell);
2425 weston_compositor_add_binding(ec, 0, 0,
2426 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2427 MODIFIER_SUPER | MODIFIER_ALT,
2428 surface_opacity_binding, NULL);
2429 weston_compositor_add_binding(ec, 0, 0,
2430 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2431 MODIFIER_SUPER, zoom_binding, NULL);
2432
2433 /* configurable bindings */
2434 mod = shell->binding_modifier;
2435 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2436 move_binding, shell);
2437 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2438 resize_binding, shell);
2439 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2440 rotate_binding, NULL);
2441 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2442 switcher_binding, shell);
2443 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2444 backlight_binding, ec);
2445 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2446 backlight_binding, ec);
2447 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2448 backlight_binding, ec);
2449 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2450 backlight_binding, ec);
2451 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2452 debug_repaint_binding, shell);
2453}
2454
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002455int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002456shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002457
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002458WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002459shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002460{
Tiago Vignattibe143262012-04-16 17:31:41 +03002461 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002462
2463 shell = malloc(sizeof *shell);
2464 if (shell == NULL)
2465 return -1;
2466
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002467 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002468 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002469
2470 shell->destroy_listener.notify = shell_destroy;
2471 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2472 shell->lock_listener.notify = lock;
2473 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2474 shell->unlock_listener.notify = unlock;
2475 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002476 ec->ping_handler = ping_handler;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002477
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002478 wl_list_init(&shell->backgrounds);
2479 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002480 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002481
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002482 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2483 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2484 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2485 weston_layer_init(&shell->background_layer,
2486 &shell->toplevel_layer.link);
2487 wl_list_init(&shell->lock_layer.surface_list);
2488
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002489 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002490
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002491 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2492 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002493 return -1;
2494
Kristian Høgsberg75840622011-09-06 13:48:16 -04002495 if (wl_display_add_global(ec->wl_display,
2496 &desktop_shell_interface,
2497 shell, bind_desktop_shell) == NULL)
2498 return -1;
2499
Pekka Paalanen6e168112011-11-24 11:34:05 +02002500 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2501 shell, bind_screensaver) == NULL)
2502 return -1;
2503
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002504 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002505 if (launch_desktop_shell_process(shell) != 0)
2506 return -1;
2507
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002508 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002509
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002510 return 0;
2511}