blob: ed5ca80a0e955a50a44e567e84c62af5bd382a77 [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
Tiago Vignattibe143262012-04-16 17:31:41 +030039struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050040 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040041
42 struct wl_listener lock_listener;
43 struct wl_listener unlock_listener;
44 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020045
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050046 struct weston_layer fullscreen_layer;
47 struct weston_layer panel_layer;
48 struct weston_layer toplevel_layer;
49 struct weston_layer background_layer;
50 struct weston_layer lock_layer;
51
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050053 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020055 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020056
57 unsigned deathcount;
58 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020059 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020060
61 bool locked;
62 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020063
Pekka Paalanen068ae942011-11-28 14:11:15 +020064 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050065 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010066
67 struct wl_list backgrounds;
68 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020069
Pekka Paalanen77346a62011-11-30 16:26:35 +020070 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020071 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020072 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020073 struct wl_resource *binding;
74 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050075 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020076 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050077
Tiago Vignatti0b52d482012-04-20 18:54:25 +030078 uint32_t binding_modifier;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050079 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040080};
81
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050082enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020083 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050084
Pekka Paalanen57da4a82011-11-23 16:42:16 +020085 SHELL_SURFACE_PANEL,
86 SHELL_SURFACE_BACKGROUND,
87 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020088 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050089
90 SHELL_SURFACE_TOPLEVEL,
91 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050092 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080093 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050094 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020095};
96
Scott Moreauff1db4a2012-04-17 19:06:18 -060097struct ping_timer {
98 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060099 uint32_t serial;
100};
101
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200102struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200103 struct wl_resource resource;
104
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500105 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200106 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500107 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300108 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 enum shell_surface_type type;
111 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800112 bool saved_position_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600113 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100114
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200116 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500117 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 } rotation;
119
120 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700121 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500122 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200123 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500124 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400125 struct wl_input_device *device;
126 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500127 } popup;
128
Alex Wu4539b082012-03-01 12:57:46 +0800129 struct {
130 enum wl_shell_surface_fullscreen_method type;
131 struct weston_transform transform; /* matrix from x, y */
132 uint32_t framerate;
133 struct weston_surface *black_surface;
134 } fullscreen;
135
Scott Moreauff1db4a2012-04-17 19:06:18 -0600136 struct ping_timer *ping_timer;
137
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600138 struct {
139 struct weston_animation current;
140 int exists;
141 int fading_in;
142 uint32_t timestamp;
143 } unresponsive_animation;
144
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500145 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100147 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300148
149 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200150};
151
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300152struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700153 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300154 struct shell_surface *shsurf;
155 struct wl_listener shsurf_destroy_listener;
156};
157
158struct weston_move_grab {
159 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500160 int32_t dx, dy;
161};
162
Pekka Paalanen460099f2012-01-20 16:48:25 +0200163struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300164 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500165 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200166 struct {
167 int32_t x;
168 int32_t y;
169 } center;
170};
171
Alex Wubd3354b2012-04-17 17:20:49 +0800172static struct shell_surface *
173get_shell_surface(struct weston_surface *surface);
174
175static struct desktop_shell *
176shell_surface_get_shell(struct shell_surface *shsurf);
177
178static bool
179shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
180{
181 struct desktop_shell *shell;
182 struct weston_surface *top_fs_es;
183
184 shell = shell_surface_get_shell(shsurf);
185
186 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
187 return false;
188
189 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
190 struct weston_surface,
191 layer_link);
192 return (shsurf == get_shell_surface(top_fs_es));
193}
194
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500195static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400196destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197{
198 struct shell_grab *grab;
199
200 grab = container_of(listener, struct shell_grab,
201 shsurf_destroy_listener);
202
203 grab->shsurf = NULL;
204}
205
206static void
207shell_grab_init(struct shell_grab *grab,
208 const struct wl_pointer_grab_interface *interface,
209 struct shell_surface *shsurf)
210{
211 grab->grab.interface = interface;
212 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400213 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
214 wl_signal_add(&shsurf->resource.destroy_signal,
215 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300216
217}
218
219static void
220shell_grab_finish(struct shell_grab *grab)
221{
222 wl_list_remove(&grab->shsurf_destroy_listener.link);
223}
224
225static void
Alex Wu4539b082012-03-01 12:57:46 +0800226center_on_output(struct weston_surface *surface,
227 struct weston_output *output);
228
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300229static uint32_t
230get_modifier(char *modifier)
231{
232 if (!modifier)
233 return MODIFIER_SUPER;
234
235 if (!strcmp("ctrl", modifier))
236 return MODIFIER_CTRL;
237 else if (!strcmp("alt", modifier))
238 return MODIFIER_ALT;
239 else if (!strcmp("super", modifier))
240 return MODIFIER_SUPER;
241 else
242 return MODIFIER_SUPER;
243}
244
Alex Wu4539b082012-03-01 12:57:46 +0800245static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300246shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200247{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200248 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200249 char *path = NULL;
250 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300251 char *modifier = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200252
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400253 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300254 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200255 };
256
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400257 struct config_key saver_keys[] = {
258 { "path", CONFIG_KEY_STRING, &path },
259 { "duration", CONFIG_KEY_INTEGER, &duration },
260 };
261
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200262 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400263 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200264 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
265 };
266
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200267 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500268 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200269 free(config_file);
270
Pekka Paalanen7296e792011-12-07 16:22:00 +0200271 shell->screensaver.path = path;
272 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300273 shell->binding_modifier = get_modifier(modifier);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200274}
275
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200276static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400277noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500278 struct wl_surface *surface, int32_t x, int32_t y)
279{
280 grab->focus = NULL;
281}
282
283static void
Scott Moreau447013d2012-02-18 05:05:29 -0700284move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500285 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500287 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500288 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289 struct shell_surface *shsurf = move->base.shsurf;
290 struct weston_surface *es;
291
292 if (!shsurf)
293 return;
294
295 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500296
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500297 weston_surface_configure(es,
298 device->x + move->dx,
299 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200300 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500301}
302
303static void
Scott Moreau447013d2012-02-18 05:05:29 -0700304move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400305 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500306{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300307 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
308 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500309 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500311 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300312 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400313 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500314 free(grab);
315 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500316}
317
Scott Moreau447013d2012-02-18 05:05:29 -0700318static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500319 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500320 move_grab_motion,
321 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322};
323
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600324static void
325unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
326{
327 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
328
329 if(!shsurf->unresponsive_animation.exists) {
330 wl_list_insert(&shsurf->surface->compositor->animation_list,
331 &shsurf->unresponsive_animation.current.link);
332 shsurf->unresponsive_animation.exists = 1;
333 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
334 weston_surface_damage(shsurf->surface);
335 }
336}
337
338static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600339unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600340 struct weston_output *output, uint32_t msecs)
341{
342 struct shell_surface *shsurf =
343 container_of(animation, struct shell_surface, unresponsive_animation.current);
344 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600345 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600346
347 if (!surface || !shsurf)
348 return;
349
350 if (shsurf->unresponsive_animation.fading_in) {
351 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
352 if (surface->saturation > 1)
353 surface->saturation -= 5;
354 if (surface->brightness > 200)
355 surface->brightness--;
356
357 shsurf->unresponsive_animation.timestamp += step;
358 }
359
360 if (surface->saturation <= 1 && surface->brightness <= 200) {
361 wl_list_remove(&shsurf->unresponsive_animation.current.link);
362 shsurf->unresponsive_animation.exists = 0;
363 }
364 }
365 else {
366 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
367 if (surface->saturation < 255)
368 surface->saturation += 5;
369 if (surface->brightness < 255)
370 surface->brightness++;
371
372 shsurf->unresponsive_animation.timestamp += step;
373 }
374
375 if (surface->saturation >= 255 && surface->brightness >= 255) {
376 surface->saturation = surface->brightness = 255;
377 wl_list_remove(&shsurf->unresponsive_animation.current.link);
378 shsurf->unresponsive_animation.exists = 0;
379 }
380 }
381
382 surface->geometry.dirty = 1;
383 weston_surface_damage(surface);
384}
385
Scott Moreau9521d5e2012-04-19 13:06:17 -0600386static void
387ping_timer_destroy(struct shell_surface *shsurf)
388{
389 if (!shsurf || !shsurf->ping_timer)
390 return;
391
392 if (shsurf->ping_timer->source)
393 wl_event_source_remove(shsurf->ping_timer->source);
394
395 free(shsurf->ping_timer);
396 shsurf->ping_timer = NULL;
397}
398
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400399static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600400ping_timeout_handler(void *data)
401{
402 struct shell_surface *shsurf = data;
403
Scott Moreau9521d5e2012-04-19 13:06:17 -0600404 /* Client is not responding */
405 shsurf->unresponsive = 1;
406 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600407
408 return 1;
409}
410
411static void
412ping_handler(struct weston_surface *surface, uint32_t serial)
413{
414 struct shell_surface *shsurf;
415 shsurf = get_shell_surface(surface);
416 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600417 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600418
419 if (!shsurf)
420 return;
421
422 if (!shsurf->ping_timer) {
423 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
424 if (!shsurf->ping_timer)
425 return;
426
427 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600428 loop = wl_display_get_event_loop(surface->compositor->wl_display);
429 shsurf->ping_timer->source =
430 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
431 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
432
433 wl_shell_surface_send_ping(&shsurf->resource, serial);
434 }
435}
436
437static void
438shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
439 uint32_t serial)
440{
441 struct shell_surface *shsurf = resource->data;
442
Scott Moreauff1db4a2012-04-17 19:06:18 -0600443 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600444 if (shsurf->unresponsive) {
445 /* Received pong from previously unresponsive client */
446 unresponsive_surface_fade(shsurf, true);
447 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600448 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600449 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600450 }
451}
452
453static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500454weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400455 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500456{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500457 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300458 struct shell_surface *shsurf = get_shell_surface(es);
459
460 if (!shsurf)
461 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500462
463 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400464 if (!move)
465 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500466
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300467 shell_grab_init(&move->base, &move_grab_interface, shsurf);
468
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200469 move->dx = es->geometry.x - wd->input_device.grab_x;
470 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500471
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300472 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400473 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500474
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400475 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400476
477 return 0;
478}
479
480static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200481shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400482 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500484 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200485 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400486
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500487 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400488 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500489 wd->input_device.pointer_focus != &shsurf->surface->surface)
490 return;
491
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400492 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400493 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500494}
495
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500496struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300497 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500498 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200499 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500500};
501
502static void
Scott Moreau447013d2012-02-18 05:05:29 -0700503resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500504 uint32_t time, int32_t x, int32_t y)
505{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500506 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500507 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500508 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200509 int32_t from_x, from_y;
510 int32_t to_x, to_y;
511
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300512 if (!resize->base.shsurf)
513 return;
514
515 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200516 device->grab_x, device->grab_y,
517 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300518 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200519 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500520
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200521 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200522 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200523 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200524 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500525 } else {
526 width = resize->width;
527 }
528
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200529 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200530 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200531 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200532 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500533 } else {
534 height = resize->height;
535 }
536
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300537 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400538 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500539}
540
541static void
Scott Moreau447013d2012-02-18 05:05:29 -0700542resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400543 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500544{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300545 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500546 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500547
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500548 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300549 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400550 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500551 free(grab);
552 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500553}
554
Scott Moreau447013d2012-02-18 05:05:29 -0700555static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500556 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500557 resize_grab_motion,
558 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500559};
560
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400561static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500562weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400563 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500564{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500565 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500566
Alex Wu4539b082012-03-01 12:57:46 +0800567 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
568 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500569
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200570 if (edges == 0 || edges > 15 ||
571 (edges & 3) == 3 || (edges & 12) == 12)
572 return 0;
573
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500574 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400575 if (!resize)
576 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500577
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300578 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
579
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500580 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200581 resize->width = shsurf->surface->geometry.width;
582 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400583
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300584 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500586
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400588
589 return 0;
590}
591
592static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200593shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400594 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200595 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500597 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200598 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400599
Alex Wu4539b082012-03-01 12:57:46 +0800600 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
601 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400602
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500603 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400604 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500605 wd->input_device.pointer_focus != &shsurf->surface->surface)
606 return;
607
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400608 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400609 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500610}
611
Juan Zhao96879df2012-02-07 08:45:41 +0800612static struct weston_output *
613get_default_output(struct weston_compositor *compositor)
614{
615 return container_of(compositor->output_list.next,
616 struct weston_output, link);
617}
618
Alex Wu4539b082012-03-01 12:57:46 +0800619static void
620shell_unset_fullscreen(struct shell_surface *shsurf)
621{
622 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800623 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
624 shell_surface_is_top_fullscreen(shsurf)) {
625 weston_output_switch_mode(shsurf->fullscreen_output,
626 shsurf->fullscreen_output->origin);
627 }
Alex Wu4539b082012-03-01 12:57:46 +0800628 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
629 shsurf->fullscreen.framerate = 0;
630 wl_list_remove(&shsurf->fullscreen.transform.link);
631 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800632 if (shsurf->fullscreen.black_surface)
633 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800634 shsurf->fullscreen.black_surface = NULL;
635 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300636 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800637 weston_surface_set_position(shsurf->surface,
638 shsurf->saved_x, shsurf->saved_y);
639}
640
Pekka Paalanen98262232011-12-01 10:42:22 +0200641static int
642reset_shell_surface_type(struct shell_surface *surface)
643{
644 switch (surface->type) {
645 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800646 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200647 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800648 case SHELL_SURFACE_MAXIMIZED:
649 surface->output = get_default_output(surface->surface->compositor);
650 weston_surface_set_position(surface->surface,
651 surface->saved_x,
652 surface->saved_y);
653 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200654 case SHELL_SURFACE_PANEL:
655 case SHELL_SURFACE_BACKGROUND:
656 wl_list_remove(&surface->link);
657 wl_list_init(&surface->link);
658 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200659 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200660 case SHELL_SURFACE_LOCK:
661 wl_resource_post_error(&surface->resource,
662 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200663 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200664 return -1;
665 case SHELL_SURFACE_NONE:
666 case SHELL_SURFACE_TOPLEVEL:
667 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500668 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200669 break;
670 }
671
672 surface->type = SHELL_SURFACE_NONE;
673 return 0;
674}
675
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500676static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300677set_toplevel(struct shell_surface *shsurf)
678{
679 if (reset_shell_surface_type(shsurf))
680 return;
681
682 shsurf->type = SHELL_SURFACE_TOPLEVEL;
683}
684
685static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200686shell_surface_set_toplevel(struct wl_client *client,
687 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400688
689{
Pekka Paalanen98262232011-12-01 10:42:22 +0200690 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400691
Tiago Vignattibc052c92012-04-19 16:18:18 +0300692 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400693}
694
695static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200696shell_surface_set_transient(struct wl_client *client,
697 struct wl_resource *resource,
698 struct wl_resource *parent_resource,
699 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400700{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200701 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500702 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200703 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500704 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400705
Pekka Paalanen98262232011-12-01 10:42:22 +0200706 if (reset_shell_surface_type(shsurf))
707 return;
708
Alex Wu4539b082012-03-01 12:57:46 +0800709 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800710 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200711 weston_surface_set_position(es, pes->geometry.x + x,
712 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400713
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200714 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400715}
716
Tiago Vignattibe143262012-04-16 17:31:41 +0300717static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800718shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200719{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400720 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800721}
722
723static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300724get_output_panel_height(struct desktop_shell *shell,
725 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800726{
727 struct shell_surface *priv;
728 int panel_height = 0;
729
730 if (!output)
731 return 0;
732
Tiago Vignattibe143262012-04-16 17:31:41 +0300733 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800734 if (priv->output == output) {
735 panel_height = priv->surface->geometry.height;
736 break;
737 }
738 }
739 return panel_height;
740}
741
742static void
743shell_surface_set_maximized(struct wl_client *client,
744 struct wl_resource *resource,
745 struct wl_resource *output_resource )
746{
747 struct shell_surface *shsurf = resource->data;
748 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300749 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800750 uint32_t edges = 0, panel_height = 0;
751
752 /* get the default output, if the client set it as NULL
753 check whether the ouput is available */
754 if (output_resource)
755 shsurf->output = output_resource->data;
756 else
757 shsurf->output = get_default_output(es->compositor);
758
759 if (reset_shell_surface_type(shsurf))
760 return;
761
762 shsurf->saved_x = es->geometry.x;
763 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800764 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800765
Tiago Vignattibe143262012-04-16 17:31:41 +0300766 shell = shell_surface_get_shell(shsurf);
767 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800768 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500769
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400770 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500771 es->output->current->width,
772 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800773
774 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200775}
776
Alex Wu21858432012-04-01 20:13:08 +0800777static void
778black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
779
Alex Wu4539b082012-03-01 12:57:46 +0800780static struct weston_surface *
781create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800782 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800783 GLfloat x, GLfloat y, int w, int h)
784{
785 struct weston_surface *surface = NULL;
786
787 surface = weston_surface_create(ec);
788 if (surface == NULL) {
789 fprintf(stderr, "no memory\n");
790 return NULL;
791 }
792
Alex Wu21858432012-04-01 20:13:08 +0800793 surface->configure = black_surface_configure;
794 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800795 weston_surface_configure(surface, x, y, w, h);
796 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
797 return surface;
798}
799
800/* Create black surface and append it to the associated fullscreen surface.
801 * Handle size dismatch and positioning according to the method. */
802static void
803shell_configure_fullscreen(struct shell_surface *shsurf)
804{
805 struct weston_output *output = shsurf->fullscreen_output;
806 struct weston_surface *surface = shsurf->surface;
807 struct weston_matrix *matrix;
808 float scale;
809
810 center_on_output(surface, output);
811
812 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500813 shsurf->fullscreen.black_surface =
814 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800815 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500816 output->x, output->y,
817 output->current->width,
818 output->current->height);
819
820 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
821 wl_list_insert(&surface->layer_link,
822 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800823 shsurf->fullscreen.black_surface->output = output;
824
825 switch (shsurf->fullscreen.type) {
826 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
827 break;
828 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
829 matrix = &shsurf->fullscreen.transform.matrix;
830 weston_matrix_init(matrix);
831 scale = (float)output->current->width/(float)surface->geometry.width;
832 weston_matrix_scale(matrix, scale, scale, 1);
833 wl_list_remove(&shsurf->fullscreen.transform.link);
834 wl_list_insert(surface->geometry.transformation_list.prev,
835 &shsurf->fullscreen.transform.link);
836 weston_surface_set_position(surface, output->x, output->y);
837 break;
838 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800839 if (shell_surface_is_top_fullscreen(shsurf)) {
840 struct weston_mode mode = {0,
841 surface->geometry.width,
842 surface->geometry.height,
843 shsurf->fullscreen.framerate};
844
845 if (weston_output_switch_mode(output, &mode) == 0) {
846 weston_surface_configure(shsurf->fullscreen.black_surface,
847 output->x, output->y,
848 output->current->width,
849 output->current->height);
850 weston_surface_set_position(surface, output->x, output->y);
851 break;
852 }
853 }
Alex Wu4539b082012-03-01 12:57:46 +0800854 break;
855 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
856 break;
857 default:
858 break;
859 }
860}
861
862/* make the fullscreen and black surface at the top */
863static void
864shell_stack_fullscreen(struct shell_surface *shsurf)
865{
Alex Wubd3354b2012-04-17 17:20:49 +0800866 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800867 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300868 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800869
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500870 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500871 wl_list_insert(&shell->fullscreen_layer.surface_list,
872 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800873 weston_surface_damage(surface);
874
875 if (!shsurf->fullscreen.black_surface)
876 shsurf->fullscreen.black_surface =
877 create_black_surface(surface->compositor,
878 surface,
879 output->x, output->y,
880 output->current->width,
881 output->current->height);
882
883 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500884 wl_list_insert(&surface->layer_link,
885 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500886 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800887}
888
889static void
890shell_map_fullscreen(struct shell_surface *shsurf)
891{
Alex Wu4539b082012-03-01 12:57:46 +0800892 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800893 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800894}
895
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400896static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200897shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500898 struct wl_resource *resource,
899 uint32_t method,
900 uint32_t framerate,
901 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400902{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200903 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500904 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800905
906 if (output_resource)
907 shsurf->output = output_resource->data;
908 else
909 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400910
Pekka Paalanen98262232011-12-01 10:42:22 +0200911 if (reset_shell_surface_type(shsurf))
912 return;
913
Alex Wu4539b082012-03-01 12:57:46 +0800914 shsurf->fullscreen_output = shsurf->output;
915 shsurf->fullscreen.type = method;
916 shsurf->fullscreen.framerate = framerate;
917 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400918
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200919 shsurf->saved_x = es->geometry.x;
920 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800921 shsurf->saved_position_valid = true;
922
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300923 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300924 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500925
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400926 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500927 shsurf->output->current->width,
928 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400929}
930
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500931static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400932popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500933 struct wl_surface *surface, int32_t x, int32_t y)
934{
935 struct wl_input_device *device = grab->input_device;
936 struct shell_surface *priv =
937 container_of(grab, struct shell_surface, popup.grab);
938 struct wl_client *client = priv->surface->surface.resource.client;
939
Pekka Paalanencb108432012-01-19 16:25:40 +0200940 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400941 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500942 grab->focus = surface;
943 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400944 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500945 grab->focus = NULL;
946 }
947}
948
949static void
Scott Moreau447013d2012-02-18 05:05:29 -0700950popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200951 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500952{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500953 struct wl_resource *resource;
954
955 resource = grab->input_device->pointer_focus_resource;
956 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500957 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500958}
959
960static void
Scott Moreau447013d2012-02-18 05:05:29 -0700961popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400962 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500963{
964 struct wl_resource *resource;
965 struct shell_surface *shsurf =
966 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400967 struct wl_display *display;
968 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500969
970 resource = grab->input_device->pointer_focus_resource;
971 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400972 display = wl_client_get_display(resource->client);
973 serial = wl_display_get_serial(display);
974 wl_input_device_send_button(resource, serial,
975 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500976 } else if (state == 0 &&
977 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400978 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500979 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400980 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500981 shsurf->popup.grab.input_device = NULL;
982 }
983
984 if (state == 0)
985 shsurf->popup.initial_up = 1;
986}
987
Scott Moreau447013d2012-02-18 05:05:29 -0700988static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500989 popup_grab_focus,
990 popup_grab_motion,
991 popup_grab_button,
992};
993
994static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400995shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500996{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400997 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500998 struct weston_surface *es = shsurf->surface;
999 struct weston_surface *parent = shsurf->parent->surface;
1000
1001 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001002 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001003
Pekka Paalanen938269a2012-02-07 14:19:01 +02001004 weston_surface_update_transform(parent);
1005 if (parent->transform.enabled) {
1006 shsurf->popup.parent_transform.matrix =
1007 parent->transform.matrix;
1008 } else {
1009 /* construct x, y translation matrix */
1010 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1011 shsurf->popup.parent_transform.matrix.d[12] =
1012 parent->geometry.x;
1013 shsurf->popup.parent_transform.matrix.d[13] =
1014 parent->geometry.y;
1015 }
1016 wl_list_insert(es->geometry.transformation_list.prev,
1017 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001018 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001019
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001020 shsurf->popup.initial_up = 0;
1021
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001022 /* We don't require the grab to still be active, but if another
1023 * grab has started in the meantime, we end the popup now. */
1024 if (device->grab_serial == shsurf->popup.serial) {
1025 wl_input_device_start_pointer_grab(device,
1026 &shsurf->popup.grab);
1027 } else {
1028 wl_shell_surface_send_popup_done(&shsurf->resource);
1029 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001030}
1031
1032static void
1033shell_surface_set_popup(struct wl_client *client,
1034 struct wl_resource *resource,
1035 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001036 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001037 struct wl_resource *parent_resource,
1038 int32_t x, int32_t y, uint32_t flags)
1039{
1040 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001041
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001042 shsurf->type = SHELL_SURFACE_POPUP;
1043 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001044 shsurf->popup.device = input_device_resource->data;
1045 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001046 shsurf->popup.x = x;
1047 shsurf->popup.y = y;
1048}
1049
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001050static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001051 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001052 shell_surface_move,
1053 shell_surface_resize,
1054 shell_surface_set_toplevel,
1055 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001056 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001057 shell_surface_set_popup,
1058 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001059};
1060
1061static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001062destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001063{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001064 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001065 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001066
Alex Wubd3354b2012-04-17 17:20:49 +08001067 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1068 shell_surface_is_top_fullscreen(shsurf)) {
1069 weston_output_switch_mode(shsurf->fullscreen_output,
1070 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001071 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001072
Alex Wuaa08e2d2012-03-05 11:01:40 +08001073 if (shsurf->fullscreen.black_surface)
1074 weston_surface_destroy(shsurf->fullscreen.black_surface);
1075
Alex Wubd3354b2012-04-17 17:20:49 +08001076 /* As destroy_resource() use wl_list_for_each_safe(),
1077 * we can always remove the listener.
1078 */
1079 wl_list_remove(&shsurf->surface_destroy_listener.link);
1080 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001081 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001082
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001083 wl_list_remove(&shsurf->link);
1084 free(shsurf);
1085}
1086
1087static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001088shell_destroy_shell_surface(struct wl_resource *resource)
1089{
1090 struct shell_surface *shsurf = resource->data;
1091
1092 destroy_shell_surface(shsurf);
1093}
1094
1095static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001096shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001097{
1098 struct shell_surface *shsurf = container_of(listener,
1099 struct shell_surface,
1100 surface_destroy_listener);
1101
Tiago Vignattibc052c92012-04-19 16:18:18 +03001102 /* tricky way to check if resource was in fact created */
1103 if (shsurf->resource.object.implementation != 0)
1104 wl_resource_destroy(&shsurf->resource);
1105 else
1106 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001107}
1108
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001109static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001111{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001112 struct wl_listener *listener;
1113
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001114 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1115 shell_handle_surface_destroy);
1116 if (listener)
1117 return container_of(listener, struct shell_surface,
1118 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001119
1120 return NULL;
1121}
1122
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001123static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001124shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1125
1126static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001127create_shell_surface(void *shell, struct weston_surface *surface,
1128 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001129{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001130 struct shell_surface *shsurf;
1131
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001132 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001133 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001134 return;
1135 }
1136
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001137 shsurf = calloc(1, sizeof *shsurf);
1138 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001139 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001140 return;
1141 }
1142
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001143 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001144 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001145
Tiago Vignattibc052c92012-04-19 16:18:18 +03001146 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001147 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001148 shsurf->unresponsive_animation.exists = 0;
1149 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001150 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001151 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001152 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001153 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1154 shsurf->fullscreen.framerate = 0;
1155 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001156 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001157 wl_list_init(&shsurf->fullscreen.transform.link);
1158
Tiago Vignattibc052c92012-04-19 16:18:18 +03001159 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001160 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1161 wl_signal_add(&surface->surface.resource.destroy_signal,
1162 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163
1164 /* init link so its safe to always remove it in destroy_shell_surface */
1165 wl_list_init(&shsurf->link);
1166
Pekka Paalanen460099f2012-01-20 16:48:25 +02001167 /* empty when not in use */
1168 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001169 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001170
Pekka Paalanen98262232011-12-01 10:42:22 +02001171 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001172
Tiago Vignattibc052c92012-04-19 16:18:18 +03001173 *ret = shsurf;
1174}
1175
1176static void
1177shell_get_shell_surface(struct wl_client *client,
1178 struct wl_resource *resource,
1179 uint32_t id,
1180 struct wl_resource *surface_resource)
1181{
1182 struct weston_surface *surface = surface_resource->data;
1183 struct desktop_shell *shell = resource->data;
1184 struct shell_surface *shsurf;
1185
1186 if (get_shell_surface(surface)) {
1187 wl_resource_post_error(surface_resource,
1188 WL_DISPLAY_ERROR_INVALID_OBJECT,
1189 "desktop_shell::get_shell_surface already requested");
1190 return;
1191 }
1192
1193 create_shell_surface(shell, surface, &shsurf);
1194 if (!shsurf) {
1195 wl_resource_post_error(surface_resource,
1196 WL_DISPLAY_ERROR_INVALID_OBJECT,
1197 "surface->configure already set");
1198 return;
1199 }
1200
1201 shsurf->resource.destroy = shell_destroy_shell_surface;
1202 shsurf->resource.object.id = id;
1203 shsurf->resource.object.interface = &wl_shell_surface_interface;
1204 shsurf->resource.object.implementation =
1205 (void (**)(void)) &shell_surface_implementation;
1206 shsurf->resource.data = shsurf;
1207
1208 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001209}
1210
1211static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001212 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001213};
1214
Kristian Høgsberg07937562011-04-12 17:25:42 -04001215static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001216handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001217{
1218 proc->pid = 0;
1219}
1220
1221static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001222launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001223{
1224 if (shell->screensaver.binding)
1225 return;
1226
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001227 if (!shell->screensaver.path)
1228 return;
1229
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001230 if (shell->screensaver.process.pid != 0) {
1231 fprintf(stderr, "old screensaver still running\n");
1232 return;
1233 }
1234
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001236 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001237 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001238 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001239}
1240
1241static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001242terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001243{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001244 if (shell->screensaver.process.pid == 0)
1245 return;
1246
1247 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001248}
1249
1250static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001251show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001252{
1253 struct wl_list *list;
1254
1255 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001256 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001257 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001258 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001259
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001260 wl_list_remove(&surface->surface->layer_link);
1261 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001262 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001263 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001264}
1265
1266static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001267hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001268{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001269 wl_list_remove(&surface->surface->layer_link);
1270 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001271 surface->surface->output = NULL;
1272}
1273
1274static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001275desktop_shell_set_background(struct wl_client *client,
1276 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001277 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001278 struct wl_resource *surface_resource)
1279{
Tiago Vignattibe143262012-04-16 17:31:41 +03001280 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001281 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001282 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001283 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001284
Pekka Paalanen98262232011-12-01 10:42:22 +02001285 if (reset_shell_surface_type(shsurf))
1286 return;
1287
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001288 wl_list_for_each(priv, &shell->backgrounds, link) {
1289 if (priv->output == output_resource->data) {
1290 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001291 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001292 wl_list_remove(&priv->link);
1293 break;
1294 }
1295 }
1296
Pekka Paalanen068ae942011-11-28 14:11:15 +02001297 shsurf->type = SHELL_SURFACE_BACKGROUND;
1298 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001299
Pekka Paalanen068ae942011-11-28 14:11:15 +02001300 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001301
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001302 weston_surface_set_position(surface, shsurf->output->x,
1303 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001304
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001305 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001306 surface_resource,
1307 shsurf->output->current->width,
1308 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001309}
1310
1311static void
1312desktop_shell_set_panel(struct wl_client *client,
1313 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001314 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001315 struct wl_resource *surface_resource)
1316{
Tiago Vignattibe143262012-04-16 17:31:41 +03001317 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001318 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001319 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001320 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001321
Pekka Paalanen98262232011-12-01 10:42:22 +02001322 if (reset_shell_surface_type(shsurf))
1323 return;
1324
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001325 wl_list_for_each(priv, &shell->panels, link) {
1326 if (priv->output == output_resource->data) {
1327 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001328 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001329 wl_list_remove(&priv->link);
1330 break;
1331 }
1332 }
1333
Pekka Paalanen068ae942011-11-28 14:11:15 +02001334 shsurf->type = SHELL_SURFACE_PANEL;
1335 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001336
Pekka Paalanen068ae942011-11-28 14:11:15 +02001337 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001338
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001339 weston_surface_set_position(surface, shsurf->output->x,
1340 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001341
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001342 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001343 surface_resource,
1344 shsurf->output->current->width,
1345 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001346}
1347
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001348static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001349handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001350{
Tiago Vignattibe143262012-04-16 17:31:41 +03001351 struct desktop_shell *shell =
1352 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001353
1354 fprintf(stderr, "lock surface gone\n");
1355 shell->lock_surface = NULL;
1356}
1357
1358static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001359desktop_shell_set_lock_surface(struct wl_client *client,
1360 struct wl_resource *resource,
1361 struct wl_resource *surface_resource)
1362{
Tiago Vignattibe143262012-04-16 17:31:41 +03001363 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001364 struct shell_surface *surface = surface_resource->data;
1365
1366 if (reset_shell_surface_type(surface))
1367 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001368
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001369 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001370
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001371 if (!shell->locked)
1372 return;
1373
Pekka Paalanen98262232011-12-01 10:42:22 +02001374 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001375
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001376 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1377 wl_signal_add(&surface_resource->destroy_signal,
1378 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001379
Pekka Paalanen068ae942011-11-28 14:11:15 +02001380 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001381}
1382
1383static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001384resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001385{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001386 struct shell_surface *tmp;
1387
1388 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1389 hide_screensaver(shell, tmp);
1390
1391 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001392
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001393 wl_list_remove(&shell->lock_layer.link);
1394 wl_list_insert(&shell->compositor->cursor_layer.link,
1395 &shell->fullscreen_layer.link);
1396 wl_list_insert(&shell->fullscreen_layer.link,
1397 &shell->panel_layer.link);
1398 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001399
1400 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001401 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001402 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001403 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001404}
1405
1406static void
1407desktop_shell_unlock(struct wl_client *client,
1408 struct wl_resource *resource)
1409{
Tiago Vignattibe143262012-04-16 17:31:41 +03001410 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001411
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001412 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001413
1414 if (shell->locked)
1415 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001416}
1417
Kristian Høgsberg75840622011-09-06 13:48:16 -04001418static const struct desktop_shell_interface desktop_shell_implementation = {
1419 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001420 desktop_shell_set_panel,
1421 desktop_shell_set_lock_surface,
1422 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001423};
1424
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001425static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001426get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001427{
1428 struct shell_surface *shsurf;
1429
1430 shsurf = get_shell_surface(surface);
1431 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001432 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001433 return shsurf->type;
1434}
1435
Kristian Høgsberg75840622011-09-06 13:48:16 -04001436static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001437move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001438 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001439{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001440 struct weston_surface *surface =
1441 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001442
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001443 if (surface == NULL)
1444 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001445
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001446 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001447 case SHELL_SURFACE_PANEL:
1448 case SHELL_SURFACE_BACKGROUND:
1449 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001450 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001451 return;
1452 default:
1453 break;
1454 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001455
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001456 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001457}
1458
1459static void
1460resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001461 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001462{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001463 struct weston_surface *surface =
1464 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001465 uint32_t edges = 0;
1466 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001467 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001468
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001469 if (surface == NULL)
1470 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001471
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001472 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001473 if (!shsurf)
1474 return;
1475
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001476 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001477 case SHELL_SURFACE_PANEL:
1478 case SHELL_SURFACE_BACKGROUND:
1479 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001480 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001481 return;
1482 default:
1483 break;
1484 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001485
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001486 weston_surface_from_global(surface,
1487 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001488
Pekka Paalanen60921e52012-01-25 15:55:43 +02001489 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001490 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001491 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001492 edges |= 0;
1493 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001494 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001495
Pekka Paalanen60921e52012-01-25 15:55:43 +02001496 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001497 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001498 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001499 edges |= 0;
1500 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001501 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001502
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001503 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001504 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001505}
1506
1507static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001508surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1509 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1510{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001511 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001512 struct shell_surface *shsurf;
1513 struct weston_surface *surface =
1514 (struct weston_surface *) device->pointer_focus;
1515
1516 if (surface == NULL)
1517 return;
1518
1519 shsurf = get_shell_surface(surface);
1520 if (!shsurf)
1521 return;
1522
1523 switch (shsurf->type) {
1524 case SHELL_SURFACE_BACKGROUND:
1525 case SHELL_SURFACE_SCREENSAVER:
1526 return;
1527 default:
1528 break;
1529 }
1530
1531 surface->alpha += value * step;
1532
1533 if (surface->alpha > 255)
1534 surface->alpha = 255;
1535 if (surface->alpha < step)
1536 surface->alpha = step;
1537
1538 surface->geometry.dirty = 1;
1539 weston_surface_damage(surface);
1540}
1541
1542static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001543zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001544 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001545{
1546 struct weston_input_device *wd = (struct weston_input_device *) device;
1547 struct weston_compositor *compositor = wd->compositor;
1548 struct weston_output *output;
1549
1550 wl_list_for_each(output, &compositor->output_list, link) {
1551 if (pixman_region32_contains_point(&output->region,
1552 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001553 output->zoom.active = 1;
1554 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001555
1556 if (output->zoom.level >= 1.0) {
1557 output->zoom.active = 0;
1558 output->zoom.level = 1.0;
1559 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001560
1561 if (output->zoom.level < output->zoom.increment)
1562 output->zoom.level = output->zoom.increment;
1563
1564 weston_output_update_zoom(output, device->x, device->y);
1565 }
1566 }
1567}
1568
Scott Moreauccbf29d2012-02-22 14:21:41 -07001569static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001570terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001571 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001572{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001574
1575 if (state)
1576 wl_display_terminate(compositor->wl_display);
1577}
1578
1579static void
Scott Moreau447013d2012-02-18 05:05:29 -07001580rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001581 uint32_t time, int32_t x, int32_t y)
1582{
1583 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001584 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001585 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001586 struct shell_surface *shsurf = rotate->base.shsurf;
1587 struct weston_surface *surface;
1588 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1589
1590 if (!shsurf)
1591 return;
1592
1593 surface = shsurf->surface;
1594
1595 cx = 0.5f * surface->geometry.width;
1596 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001597
1598 dx = device->x - rotate->center.x;
1599 dy = device->y - rotate->center.y;
1600 r = sqrtf(dx * dx + dy * dy);
1601
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001602 wl_list_remove(&shsurf->rotation.transform.link);
1603 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001604
1605 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001606 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001607 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001608
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001609 weston_matrix_init(&rotate->rotation);
1610 rotate->rotation.d[0] = dx / r;
1611 rotate->rotation.d[4] = -dy / r;
1612 rotate->rotation.d[1] = -rotate->rotation.d[4];
1613 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001614
1615 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001616 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001617 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001618 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001619 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001620
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001621 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001622 &shsurf->surface->geometry.transformation_list,
1623 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001624 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001625 wl_list_init(&shsurf->rotation.transform.link);
1626 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001627 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001628 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001629
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001630 /* We need to adjust the position of the surface
1631 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001632 cposx = surface->geometry.x + cx;
1633 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001634 dposx = rotate->center.x - cposx;
1635 dposy = rotate->center.y - cposy;
1636 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001637 weston_surface_set_position(surface,
1638 surface->geometry.x + dposx,
1639 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001640 }
1641
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001642 /* Repaint implies weston_surface_update_transform(), which
1643 * lazily applies the damage due to rotation update.
1644 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001645 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001646}
1647
1648static void
Scott Moreau447013d2012-02-18 05:05:29 -07001649rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001650 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001651{
1652 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001653 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001654 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001655 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001656
1657 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001658 if (shsurf)
1659 weston_matrix_multiply(&shsurf->rotation.rotation,
1660 &rotate->rotation);
1661 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001662 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001663 free(rotate);
1664 }
1665}
1666
Scott Moreau447013d2012-02-18 05:05:29 -07001667static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001668 noop_grab_focus,
1669 rotate_grab_motion,
1670 rotate_grab_button,
1671};
1672
1673static void
1674rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001675 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001676{
1677 struct weston_surface *base_surface =
1678 (struct weston_surface *) device->pointer_focus;
1679 struct shell_surface *surface;
1680 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001681 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001682 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001683
1684 if (base_surface == NULL)
1685 return;
1686
1687 surface = get_shell_surface(base_surface);
1688 if (!surface)
1689 return;
1690
1691 switch (surface->type) {
1692 case SHELL_SURFACE_PANEL:
1693 case SHELL_SURFACE_BACKGROUND:
1694 case SHELL_SURFACE_FULLSCREEN:
1695 case SHELL_SURFACE_SCREENSAVER:
1696 return;
1697 default:
1698 break;
1699 }
1700
Pekka Paalanen460099f2012-01-20 16:48:25 +02001701 rotate = malloc(sizeof *rotate);
1702 if (!rotate)
1703 return;
1704
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001705 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001706
1707 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001708 surface->surface->geometry.width / 2,
1709 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001710 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001711
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001712 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001713
1714 dx = device->x - rotate->center.x;
1715 dy = device->y - rotate->center.y;
1716 r = sqrtf(dx * dx + dy * dy);
1717 if (r > 20.0f) {
1718 struct weston_matrix inverse;
1719
1720 weston_matrix_init(&inverse);
1721 inverse.d[0] = dx / r;
1722 inverse.d[4] = dy / r;
1723 inverse.d[1] = -inverse.d[4];
1724 inverse.d[5] = inverse.d[0];
1725 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001726
1727 weston_matrix_init(&rotate->rotation);
1728 rotate->rotation.d[0] = dx / r;
1729 rotate->rotation.d[4] = -dy / r;
1730 rotate->rotation.d[1] = -rotate->rotation.d[4];
1731 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001732 } else {
1733 weston_matrix_init(&surface->rotation.rotation);
1734 weston_matrix_init(&rotate->rotation);
1735 }
1736
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001737 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001738}
1739
1740static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001741activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001742 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001743{
Alex Wu21858432012-04-01 20:13:08 +08001744 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001745
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001746 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001747
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001748 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001749 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001750 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001751 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001752 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001753
Pekka Paalanen77346a62011-11-30 16:26:35 +02001754 case SHELL_SURFACE_SCREENSAVER:
1755 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001756 if (shell->lock_surface)
1757 weston_surface_restack(es,
1758 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001759 break;
Alex Wu4539b082012-03-01 12:57:46 +08001760 case SHELL_SURFACE_FULLSCREEN:
1761 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001762 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001763 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001764 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001765 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001766 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001767 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1768 wl_list_for_each_reverse_safe(surf,
1769 prev,
1770 &shell->fullscreen_layer.surface_list,
1771 layer_link)
1772 weston_surface_restack(surf,
1773 &shell->toplevel_layer.surface_list);
1774 }
1775
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001776 weston_surface_restack(es,
1777 &shell->toplevel_layer.surface_list);
1778 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001779 }
1780}
1781
Alex Wu21858432012-04-01 20:13:08 +08001782/* no-op func for checking black surface */
1783static void
1784black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1785{
1786}
1787
1788static bool
1789is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1790{
1791 if (es->configure == black_surface_configure) {
1792 if (fs_surface)
1793 *fs_surface = (struct weston_surface *)es->private;
1794 return true;
1795 }
1796 return false;
1797}
1798
Kristian Høgsberg75840622011-09-06 13:48:16 -04001799static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001800click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001801 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001802 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001803{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001804 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001805 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001806 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001807 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001808
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001809 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001810 if (!focus)
1811 return;
1812
Alex Wu21858432012-04-01 20:13:08 +08001813 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001814 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001815
Alex Wu9c35e6b2012-03-05 14:13:13 +08001816 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001817 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001818}
1819
1820static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001821lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001822{
Tiago Vignattibe143262012-04-16 17:31:41 +03001823 struct desktop_shell *shell =
1824 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001825 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001826 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001827 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001828
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001829 if (shell->locked) {
1830 wl_list_for_each(output, &shell->compositor->output_list, link)
1831 /* TODO: find a way to jump to other DPMS levels */
1832 if (output->set_dpms)
1833 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001834 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001835 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001836
1837 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001838
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001839 /* Hide all surfaces by removing the fullscreen, panel and
1840 * toplevel layers. This way nothing else can show or receive
1841 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001842
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001843 wl_list_remove(&shell->panel_layer.link);
1844 wl_list_remove(&shell->toplevel_layer.link);
1845 wl_list_remove(&shell->fullscreen_layer.link);
1846 wl_list_insert(&shell->compositor->cursor_layer.link,
1847 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001848
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849 launch_screensaver(shell);
1850
1851 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1852 show_screensaver(shell, shsurf);
1853
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001854 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001855 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001856 weston_compositor_wake(shell->compositor);
1857 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001858 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001859
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001860 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001861 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001862
1863 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001864 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1865 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001866 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001867 }
1868
1869 /* TODO: disable bindings that should not work while locked. */
1870
1871 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001872}
1873
1874static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001875unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001876{
Tiago Vignattibe143262012-04-16 17:31:41 +03001877 struct desktop_shell *shell =
1878 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001879
Pekka Paalanend81c2162011-11-16 13:47:34 +02001880 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001881 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001882 return;
1883 }
1884
1885 /* If desktop-shell client has gone away, unlock immediately. */
1886 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001887 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001888 return;
1889 }
1890
1891 if (shell->prepare_event_sent)
1892 return;
1893
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001894 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001895 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001896}
1897
1898static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001899center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001900{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001901 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001902 GLfloat x = (mode->width - surface->geometry.width) / 2;
1903 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001904
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001905 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001906}
1907
1908static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001909map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001910 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001911{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001912 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001913 struct shell_surface *shsurf;
1914 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001915 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001916 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001917
Pekka Paalanen77346a62011-11-30 16:26:35 +02001918 shsurf = get_shell_surface(surface);
1919 if (shsurf)
1920 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001921
Pekka Paalanen60921e52012-01-25 15:55:43 +02001922 surface->geometry.width = width;
1923 surface->geometry.height = height;
1924 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001925
1926 /* initial positioning, see also configure() */
1927 switch (surface_type) {
1928 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001929 weston_surface_set_position(surface, 10 + random() % 400,
1930 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001931 break;
1932 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001933 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001934 break;
Alex Wu4539b082012-03-01 12:57:46 +08001935 case SHELL_SURFACE_FULLSCREEN:
1936 shell_map_fullscreen(shsurf);
1937 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001938 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001939 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001940 panel_height = get_output_panel_height(shell,surface->output);
1941 weston_surface_set_position(surface, surface->output->x,
1942 surface->output->y + panel_height);
1943 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001944 case SHELL_SURFACE_LOCK:
1945 center_on_output(surface, get_default_output(compositor));
1946 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001947 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001948 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001949 case SHELL_SURFACE_NONE:
1950 weston_surface_set_position(surface,
1951 surface->geometry.x + sx,
1952 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001953 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001954 default:
1955 ;
1956 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001957
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001958 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001959 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001960 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001961 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001962 wl_list_insert(&shell->background_layer.surface_list,
1963 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001964 break;
1965 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001966 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001967 wl_list_insert(&shell->panel_layer.surface_list,
1968 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001969 break;
1970 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001971 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001972 wl_list_insert(&shell->lock_layer.surface_list,
1973 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001974 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001975 break;
1976 case SHELL_SURFACE_SCREENSAVER:
1977 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001978 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001979 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001980 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001981 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001982 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001984 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001985 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001986 case SHELL_SURFACE_POPUP:
1987 case SHELL_SURFACE_TRANSIENT:
1988 parent = shsurf->parent->surface;
1989 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1990 break;
Alex Wu4539b082012-03-01 12:57:46 +08001991 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001992 case SHELL_SURFACE_NONE:
1993 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001994 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001995 wl_list_insert(&shell->toplevel_layer.surface_list,
1996 &surface->layer_link);
1997 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001998 }
1999
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002000 if (surface_type != SHELL_SURFACE_NONE) {
2001 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002002 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2003 surface->output = shsurf->output;
2004 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002005
Juan Zhao7bb92f02011-12-15 11:31:51 -05002006 switch (surface_type) {
2007 case SHELL_SURFACE_TOPLEVEL:
2008 case SHELL_SURFACE_TRANSIENT:
2009 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002010 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002011 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002012 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002013 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002014 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002015 break;
2016 default:
2017 break;
2018 }
2019
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002020 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002021 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002022}
2023
2024static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002025configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002026 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002027{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002028 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2029 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002030
Pekka Paalanen77346a62011-11-30 16:26:35 +02002031 shsurf = get_shell_surface(surface);
2032 if (shsurf)
2033 surface_type = shsurf->type;
2034
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002035 surface->geometry.x = x;
2036 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002037 surface->geometry.width = width;
2038 surface->geometry.height = height;
2039 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002040
2041 switch (surface_type) {
2042 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002043 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002044 break;
Alex Wu4539b082012-03-01 12:57:46 +08002045 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002046 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002047 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002048 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002049 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002050 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002051 surface->geometry.x = surface->output->x;
2052 surface->geometry.y = surface->output->y +
2053 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002054 break;
Alex Wu4539b082012-03-01 12:57:46 +08002055 case SHELL_SURFACE_TOPLEVEL:
2056 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002057 default:
2058 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002059 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002060
Alex Wu4539b082012-03-01 12:57:46 +08002061 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002062 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002063 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002064
2065 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2066 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002067 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2068 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002069 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002070}
2071
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002072static void
2073shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2074{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002075 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002076 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002077
2078 if (!weston_surface_is_mapped(es)) {
2079 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002080 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002081 es->geometry.width != es->buffer->width ||
2082 es->geometry.height != es->buffer->height) {
2083 GLfloat from_x, from_y;
2084 GLfloat to_x, to_y;
2085
2086 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2087 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2088 configure(shell, es,
2089 es->geometry.x + to_x - from_x,
2090 es->geometry.y + to_y - from_y,
2091 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002092 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002093 }
2094}
2095
Tiago Vignattibe143262012-04-16 17:31:41 +03002096static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002097
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002098static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002099desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002100{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002101 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002102 struct desktop_shell *shell =
2103 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002104
2105 shell->child.process.pid = 0;
2106 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002107
2108 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2109 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002110 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002111 shell->child.deathstamp = time;
2112 shell->child.deathcount = 0;
2113 }
2114
2115 shell->child.deathcount++;
2116 if (shell->child.deathcount > 5) {
2117 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2118 return;
2119 }
2120
2121 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2122 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002123}
2124
2125static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002126launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002127{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002128 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002129
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002130 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002131 &shell->child.process,
2132 shell_exe,
2133 desktop_shell_sigchld);
2134
2135 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002136 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002137 return 0;
2138}
2139
2140static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002141bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2142{
Tiago Vignattibe143262012-04-16 17:31:41 +03002143 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002144
2145 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002146 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002147}
2148
Kristian Høgsberg75840622011-09-06 13:48:16 -04002149static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002150unbind_desktop_shell(struct wl_resource *resource)
2151{
Tiago Vignattibe143262012-04-16 17:31:41 +03002152 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002153
2154 if (shell->locked)
2155 resume_desktop(shell);
2156
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002157 shell->child.desktop_shell = NULL;
2158 shell->prepare_event_sent = false;
2159 free(resource);
2160}
2161
2162static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002163bind_desktop_shell(struct wl_client *client,
2164 void *data, uint32_t version, uint32_t id)
2165{
Tiago Vignattibe143262012-04-16 17:31:41 +03002166 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002167 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002168
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002169 resource = wl_client_add_object(client, &desktop_shell_interface,
2170 &desktop_shell_implementation,
2171 id, shell);
2172
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002173 if (client == shell->child.client) {
2174 resource->destroy = unbind_desktop_shell;
2175 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002176 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002177 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002178
2179 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2180 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002181 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002182}
2183
Pekka Paalanen6e168112011-11-24 11:34:05 +02002184static void
2185screensaver_set_surface(struct wl_client *client,
2186 struct wl_resource *resource,
2187 struct wl_resource *shell_surface_resource,
2188 struct wl_resource *output_resource)
2189{
Tiago Vignattibe143262012-04-16 17:31:41 +03002190 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002191 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002192 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002193
Pekka Paalanen98262232011-12-01 10:42:22 +02002194 if (reset_shell_surface_type(surface))
2195 return;
2196
Pekka Paalanen77346a62011-11-30 16:26:35 +02002197 surface->type = SHELL_SURFACE_SCREENSAVER;
2198
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002199 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002200 surface->output = output;
2201 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002202}
2203
2204static const struct screensaver_interface screensaver_implementation = {
2205 screensaver_set_surface
2206};
2207
2208static void
2209unbind_screensaver(struct wl_resource *resource)
2210{
Tiago Vignattibe143262012-04-16 17:31:41 +03002211 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002212
Pekka Paalanen77346a62011-11-30 16:26:35 +02002213 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002214 free(resource);
2215}
2216
2217static void
2218bind_screensaver(struct wl_client *client,
2219 void *data, uint32_t version, uint32_t id)
2220{
Tiago Vignattibe143262012-04-16 17:31:41 +03002221 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002222 struct wl_resource *resource;
2223
2224 resource = wl_client_add_object(client, &screensaver_interface,
2225 &screensaver_implementation,
2226 id, shell);
2227
Pekka Paalanen77346a62011-11-30 16:26:35 +02002228 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002229 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002230 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002231 return;
2232 }
2233
2234 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2235 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002236 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002237}
2238
Kristian Høgsberg07045392012-02-19 18:52:44 -05002239struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002240 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002241 struct weston_surface *current;
2242 struct wl_listener listener;
2243 struct wl_keyboard_grab grab;
2244};
2245
2246static void
2247switcher_next(struct switcher *switcher)
2248{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002249 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002250 struct weston_surface *surface;
2251 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002252 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002253
2254 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002255 switch (get_shell_surface_type(surface)) {
2256 case SHELL_SURFACE_TOPLEVEL:
2257 case SHELL_SURFACE_FULLSCREEN:
2258 case SHELL_SURFACE_MAXIMIZED:
2259 if (first == NULL)
2260 first = surface;
2261 if (prev == switcher->current)
2262 next = surface;
2263 prev = surface;
2264 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002265 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002266 weston_surface_damage(surface);
2267 break;
2268 default:
2269 break;
2270 }
Alex Wu1659daa2012-04-01 20:13:09 +08002271
2272 if (is_black_surface(surface, NULL)) {
2273 surface->alpha = 64;
2274 surface->geometry.dirty = 1;
2275 weston_surface_damage(surface);
2276 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002277 }
2278
2279 if (next == NULL)
2280 next = first;
2281
Alex Wu07b26062012-03-12 16:06:01 +08002282 if (next == NULL)
2283 return;
2284
Kristian Høgsberg07045392012-02-19 18:52:44 -05002285 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002286 wl_signal_add(&next->surface.resource.destroy_signal,
2287 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002288
2289 switcher->current = next;
2290 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002291
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002292 shsurf = get_shell_surface(switcher->current);
2293 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2294 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002295}
2296
2297static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002298switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002299{
2300 struct switcher *switcher =
2301 container_of(listener, struct switcher, listener);
2302
2303 switcher_next(switcher);
2304}
2305
2306static void
2307switcher_destroy(struct switcher *switcher, uint32_t time)
2308{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002309 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002310 struct weston_surface *surface;
2311 struct weston_input_device *device =
2312 (struct weston_input_device *) switcher->grab.input_device;
2313
2314 wl_list_for_each(surface, &compositor->surface_list, link) {
2315 surface->alpha = 255;
2316 weston_surface_damage(surface);
2317 }
2318
Alex Wu07b26062012-03-12 16:06:01 +08002319 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002320 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002321 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002322 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002323 free(switcher);
2324}
2325
2326static void
2327switcher_key(struct wl_keyboard_grab *grab,
2328 uint32_t time, uint32_t key, int32_t state)
2329{
2330 struct switcher *switcher = container_of(grab, struct switcher, grab);
2331 struct weston_input_device *device =
2332 (struct weston_input_device *) grab->input_device;
2333
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002334 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002335 switcher_destroy(switcher, time);
2336 } else if (key == KEY_TAB && state) {
2337 switcher_next(switcher);
2338 }
2339};
2340
2341static const struct wl_keyboard_grab_interface switcher_grab = {
2342 switcher_key
2343};
2344
2345static void
2346switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002347 uint32_t key, uint32_t button, uint32_t axis,
2348 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002349{
Tiago Vignattibe143262012-04-16 17:31:41 +03002350 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002351 struct switcher *switcher;
2352
2353 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002354 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002355 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002356 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002357 wl_list_init(&switcher->listener.link);
2358
2359 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002360 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2361 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002362 switcher_next(switcher);
2363}
2364
Pekka Paalanen3c647232011-12-22 13:43:43 +02002365static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002366backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002367 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002368{
2369 struct weston_compositor *compositor = data;
2370 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002371 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002372
2373 /* TODO: we're limiting to simple use cases, where we assume just
2374 * control on the primary display. We'd have to extend later if we
2375 * ever get support for setting backlights on random desktop LCD
2376 * panels though */
2377 output = get_default_output(compositor);
2378 if (!output)
2379 return;
2380
2381 if (!output->set_backlight)
2382 return;
2383
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002384 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2385 backlight_new = output->backlight_current - 25;
2386 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2387 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002388
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002389 if (backlight_new < 5)
2390 backlight_new = 5;
2391 if (backlight_new > 255)
2392 backlight_new = 255;
2393
2394 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002395 output->set_backlight(output, output->backlight_current);
2396}
2397
2398static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002399debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002400 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002401{
Tiago Vignattibe143262012-04-16 17:31:41 +03002402 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002403 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002404 struct weston_surface *surface;
2405
2406 if (shell->debug_repaint_surface) {
2407 weston_surface_destroy(shell->debug_repaint_surface);
2408 shell->debug_repaint_surface = NULL;
2409 } else {
2410 surface = weston_surface_create(compositor);
2411 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2412 weston_surface_configure(surface, 0, 0, 8192, 8192);
2413 wl_list_insert(&compositor->fade_layer.surface_list,
2414 &surface->layer_link);
2415 weston_surface_assign_output(surface);
2416 pixman_region32_init(&surface->input);
2417
2418 /* Here's the dirty little trick that makes the
2419 * repaint debugging work: we force an
2420 * update_transform first to update dependent state
2421 * and clear the geometry.dirty bit. Then we clear
2422 * the surface damage so it only gets repainted
2423 * piecewise as we repaint other things. */
2424
2425 weston_surface_update_transform(surface);
2426 pixman_region32_fini(&surface->damage);
2427 pixman_region32_init(&surface->damage);
2428 shell->debug_repaint_surface = surface;
2429 }
2430}
2431
2432static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002433shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002434{
Tiago Vignattibe143262012-04-16 17:31:41 +03002435 struct desktop_shell *shell =
2436 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002437
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002438 if (shell->child.client)
2439 wl_client_destroy(shell->child.client);
2440
Pekka Paalanen3c647232011-12-22 13:43:43 +02002441 free(shell->screensaver.path);
2442 free(shell);
2443}
2444
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002445static void
2446shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2447{
2448 uint32_t mod;
2449
2450 /* fixed bindings */
2451 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2452 MODIFIER_CTRL | MODIFIER_ALT,
2453 terminate_binding, ec);
2454 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2455 click_to_activate_binding, shell);
2456 weston_compositor_add_binding(ec, 0, 0,
2457 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2458 MODIFIER_SUPER | MODIFIER_ALT,
2459 surface_opacity_binding, NULL);
2460 weston_compositor_add_binding(ec, 0, 0,
2461 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2462 MODIFIER_SUPER, zoom_binding, NULL);
2463
2464 /* configurable bindings */
2465 mod = shell->binding_modifier;
2466 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2467 move_binding, shell);
2468 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2469 resize_binding, shell);
2470 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2471 rotate_binding, NULL);
2472 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2473 switcher_binding, shell);
2474 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2475 backlight_binding, ec);
2476 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2477 backlight_binding, ec);
2478 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2479 backlight_binding, ec);
2480 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2481 backlight_binding, ec);
2482 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2483 debug_repaint_binding, shell);
2484}
2485
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002486int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002487shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002488
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002489WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002490shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002491{
Tiago Vignattibe143262012-04-16 17:31:41 +03002492 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002493
2494 shell = malloc(sizeof *shell);
2495 if (shell == NULL)
2496 return -1;
2497
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002498 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002499 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002500
2501 shell->destroy_listener.notify = shell_destroy;
2502 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2503 shell->lock_listener.notify = lock;
2504 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2505 shell->unlock_listener.notify = unlock;
2506 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002507 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002508 ec->shell_interface.create_shell_surface = create_shell_surface;
2509 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002510
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002511 wl_list_init(&shell->backgrounds);
2512 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002513 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002514
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002515 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2516 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2517 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2518 weston_layer_init(&shell->background_layer,
2519 &shell->toplevel_layer.link);
2520 wl_list_init(&shell->lock_layer.surface_list);
2521
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002522 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002523
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002524 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2525 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002526 return -1;
2527
Kristian Høgsberg75840622011-09-06 13:48:16 -04002528 if (wl_display_add_global(ec->wl_display,
2529 &desktop_shell_interface,
2530 shell, bind_desktop_shell) == NULL)
2531 return -1;
2532
Pekka Paalanen6e168112011-11-24 11:34:05 +02002533 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2534 shell, bind_screensaver) == NULL)
2535 return -1;
2536
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002537 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002538 if (launch_desktop_shell_process(shell) != 0)
2539 return -1;
2540
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002541 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002542
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002543 return 0;
2544}