blob: fd664628767acb70c4d1771a79e0578158ff34ae [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;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400421 if (!shsurf->resource.client)
422 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600423
424 if (!shsurf->ping_timer) {
425 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
426 if (!shsurf->ping_timer)
427 return;
428
429 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600430 loop = wl_display_get_event_loop(surface->compositor->wl_display);
431 shsurf->ping_timer->source =
432 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
433 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
434
435 wl_shell_surface_send_ping(&shsurf->resource, serial);
436 }
437}
438
439static void
440shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
441 uint32_t serial)
442{
443 struct shell_surface *shsurf = resource->data;
444
Scott Moreauff1db4a2012-04-17 19:06:18 -0600445 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600446 if (shsurf->unresponsive) {
447 /* Received pong from previously unresponsive client */
448 unresponsive_surface_fade(shsurf, true);
449 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600450 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600451 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600452 }
453}
454
455static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400457 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500458{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500459 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300460 struct shell_surface *shsurf = get_shell_surface(es);
461
462 if (!shsurf)
463 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500464
465 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400466 if (!move)
467 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500468
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300469 shell_grab_init(&move->base, &move_grab_interface, shsurf);
470
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200471 move->dx = es->geometry.x - wd->input_device.grab_x;
472 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500473
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300474 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400475 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500476
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400477 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400478
479 return 0;
480}
481
482static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200483shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400484 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500486 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200487 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400488
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500489 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400490 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500491 wd->input_device.pointer_focus != &shsurf->surface->surface)
492 return;
493
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400494 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400495 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500496}
497
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500498struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300499 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500500 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200501 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500502};
503
504static void
Scott Moreau447013d2012-02-18 05:05:29 -0700505resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500506 uint32_t time, int32_t x, int32_t y)
507{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500508 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500509 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500510 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200511 int32_t from_x, from_y;
512 int32_t to_x, to_y;
513
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300514 if (!resize->base.shsurf)
515 return;
516
517 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200518 device->grab_x, device->grab_y,
519 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300520 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200521 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500522
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200523 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200524 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200525 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200526 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500527 } else {
528 width = resize->width;
529 }
530
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200531 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200532 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200533 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200534 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500535 } else {
536 height = resize->height;
537 }
538
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300539 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400540 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500541}
542
543static void
Scott Moreau447013d2012-02-18 05:05:29 -0700544resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400545 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500546{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300547 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500548 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500549
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500550 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300551 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400552 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500553 free(grab);
554 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555}
556
Scott Moreau447013d2012-02-18 05:05:29 -0700557static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500558 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500559 resize_grab_motion,
560 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500561};
562
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400563static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500564weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400565 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500566{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500567 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500568
Alex Wu4539b082012-03-01 12:57:46 +0800569 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
570 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500571
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200572 if (edges == 0 || edges > 15 ||
573 (edges & 3) == 3 || (edges & 12) == 12)
574 return 0;
575
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500576 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400577 if (!resize)
578 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500579
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300580 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
581
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500582 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200583 resize->width = shsurf->surface->geometry.width;
584 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400585
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300586 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500588
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400589 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400590
591 return 0;
592}
593
594static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200595shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400596 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200597 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400598{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500599 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200600 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400601
Alex Wu4539b082012-03-01 12:57:46 +0800602 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
603 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400604
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500605 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400606 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500607 wd->input_device.pointer_focus != &shsurf->surface->surface)
608 return;
609
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400610 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400611 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500612}
613
Juan Zhao96879df2012-02-07 08:45:41 +0800614static struct weston_output *
615get_default_output(struct weston_compositor *compositor)
616{
617 return container_of(compositor->output_list.next,
618 struct weston_output, link);
619}
620
Alex Wu4539b082012-03-01 12:57:46 +0800621static void
622shell_unset_fullscreen(struct shell_surface *shsurf)
623{
624 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800625 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
626 shell_surface_is_top_fullscreen(shsurf)) {
627 weston_output_switch_mode(shsurf->fullscreen_output,
628 shsurf->fullscreen_output->origin);
629 }
Alex Wu4539b082012-03-01 12:57:46 +0800630 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
631 shsurf->fullscreen.framerate = 0;
632 wl_list_remove(&shsurf->fullscreen.transform.link);
633 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800634 if (shsurf->fullscreen.black_surface)
635 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800636 shsurf->fullscreen.black_surface = NULL;
637 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300638 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800639 weston_surface_set_position(shsurf->surface,
640 shsurf->saved_x, shsurf->saved_y);
641}
642
Pekka Paalanen98262232011-12-01 10:42:22 +0200643static int
644reset_shell_surface_type(struct shell_surface *surface)
645{
646 switch (surface->type) {
647 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800648 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200649 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800650 case SHELL_SURFACE_MAXIMIZED:
651 surface->output = get_default_output(surface->surface->compositor);
652 weston_surface_set_position(surface->surface,
653 surface->saved_x,
654 surface->saved_y);
655 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200656 case SHELL_SURFACE_PANEL:
657 case SHELL_SURFACE_BACKGROUND:
658 wl_list_remove(&surface->link);
659 wl_list_init(&surface->link);
660 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200661 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200662 case SHELL_SURFACE_LOCK:
663 wl_resource_post_error(&surface->resource,
664 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200665 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200666 return -1;
667 case SHELL_SURFACE_NONE:
668 case SHELL_SURFACE_TOPLEVEL:
669 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500670 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200671 break;
672 }
673
674 surface->type = SHELL_SURFACE_NONE;
675 return 0;
676}
677
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500678static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300679set_toplevel(struct shell_surface *shsurf)
680{
681 if (reset_shell_surface_type(shsurf))
682 return;
683
684 shsurf->type = SHELL_SURFACE_TOPLEVEL;
685}
686
687static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200688shell_surface_set_toplevel(struct wl_client *client,
689 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400690
691{
Pekka Paalanen98262232011-12-01 10:42:22 +0200692 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400693
Tiago Vignattibc052c92012-04-19 16:18:18 +0300694 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400695}
696
697static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200698shell_surface_set_transient(struct wl_client *client,
699 struct wl_resource *resource,
700 struct wl_resource *parent_resource,
701 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400702{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200703 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500704 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200705 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500706 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400707
Pekka Paalanen98262232011-12-01 10:42:22 +0200708 if (reset_shell_surface_type(shsurf))
709 return;
710
Alex Wu4539b082012-03-01 12:57:46 +0800711 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800712 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200713 weston_surface_set_position(es, pes->geometry.x + x,
714 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400715
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200716 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400717}
718
Tiago Vignattibe143262012-04-16 17:31:41 +0300719static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800720shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200721{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400722 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800723}
724
725static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300726get_output_panel_height(struct desktop_shell *shell,
727 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800728{
729 struct shell_surface *priv;
730 int panel_height = 0;
731
732 if (!output)
733 return 0;
734
Tiago Vignattibe143262012-04-16 17:31:41 +0300735 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800736 if (priv->output == output) {
737 panel_height = priv->surface->geometry.height;
738 break;
739 }
740 }
741 return panel_height;
742}
743
744static void
745shell_surface_set_maximized(struct wl_client *client,
746 struct wl_resource *resource,
747 struct wl_resource *output_resource )
748{
749 struct shell_surface *shsurf = resource->data;
750 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300751 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800752 uint32_t edges = 0, panel_height = 0;
753
754 /* get the default output, if the client set it as NULL
755 check whether the ouput is available */
756 if (output_resource)
757 shsurf->output = output_resource->data;
758 else
759 shsurf->output = get_default_output(es->compositor);
760
761 if (reset_shell_surface_type(shsurf))
762 return;
763
764 shsurf->saved_x = es->geometry.x;
765 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800766 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800767
Tiago Vignattibe143262012-04-16 17:31:41 +0300768 shell = shell_surface_get_shell(shsurf);
769 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800770 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500771
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400772 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500773 es->output->current->width,
774 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800775
776 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200777}
778
Alex Wu21858432012-04-01 20:13:08 +0800779static void
780black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
781
Alex Wu4539b082012-03-01 12:57:46 +0800782static struct weston_surface *
783create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800784 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800785 GLfloat x, GLfloat y, int w, int h)
786{
787 struct weston_surface *surface = NULL;
788
789 surface = weston_surface_create(ec);
790 if (surface == NULL) {
791 fprintf(stderr, "no memory\n");
792 return NULL;
793 }
794
Alex Wu21858432012-04-01 20:13:08 +0800795 surface->configure = black_surface_configure;
796 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800797 weston_surface_configure(surface, x, y, w, h);
798 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
799 return surface;
800}
801
802/* Create black surface and append it to the associated fullscreen surface.
803 * Handle size dismatch and positioning according to the method. */
804static void
805shell_configure_fullscreen(struct shell_surface *shsurf)
806{
807 struct weston_output *output = shsurf->fullscreen_output;
808 struct weston_surface *surface = shsurf->surface;
809 struct weston_matrix *matrix;
810 float scale;
811
812 center_on_output(surface, output);
813
814 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500815 shsurf->fullscreen.black_surface =
816 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800817 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500818 output->x, output->y,
819 output->current->width,
820 output->current->height);
821
822 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
823 wl_list_insert(&surface->layer_link,
824 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800825 shsurf->fullscreen.black_surface->output = output;
826
827 switch (shsurf->fullscreen.type) {
828 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
829 break;
830 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
831 matrix = &shsurf->fullscreen.transform.matrix;
832 weston_matrix_init(matrix);
833 scale = (float)output->current->width/(float)surface->geometry.width;
834 weston_matrix_scale(matrix, scale, scale, 1);
835 wl_list_remove(&shsurf->fullscreen.transform.link);
836 wl_list_insert(surface->geometry.transformation_list.prev,
837 &shsurf->fullscreen.transform.link);
838 weston_surface_set_position(surface, output->x, output->y);
839 break;
840 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800841 if (shell_surface_is_top_fullscreen(shsurf)) {
842 struct weston_mode mode = {0,
843 surface->geometry.width,
844 surface->geometry.height,
845 shsurf->fullscreen.framerate};
846
847 if (weston_output_switch_mode(output, &mode) == 0) {
848 weston_surface_configure(shsurf->fullscreen.black_surface,
849 output->x, output->y,
850 output->current->width,
851 output->current->height);
852 weston_surface_set_position(surface, output->x, output->y);
853 break;
854 }
855 }
Alex Wu4539b082012-03-01 12:57:46 +0800856 break;
857 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
858 break;
859 default:
860 break;
861 }
862}
863
864/* make the fullscreen and black surface at the top */
865static void
866shell_stack_fullscreen(struct shell_surface *shsurf)
867{
Alex Wubd3354b2012-04-17 17:20:49 +0800868 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800869 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300870 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800871
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500872 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500873 wl_list_insert(&shell->fullscreen_layer.surface_list,
874 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800875 weston_surface_damage(surface);
876
877 if (!shsurf->fullscreen.black_surface)
878 shsurf->fullscreen.black_surface =
879 create_black_surface(surface->compositor,
880 surface,
881 output->x, output->y,
882 output->current->width,
883 output->current->height);
884
885 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500886 wl_list_insert(&surface->layer_link,
887 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500888 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800889}
890
891static void
892shell_map_fullscreen(struct shell_surface *shsurf)
893{
Alex Wu4539b082012-03-01 12:57:46 +0800894 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800895 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800896}
897
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400898static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200899shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500900 struct wl_resource *resource,
901 uint32_t method,
902 uint32_t framerate,
903 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400904{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200905 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500906 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800907
908 if (output_resource)
909 shsurf->output = output_resource->data;
910 else
911 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400912
Pekka Paalanen98262232011-12-01 10:42:22 +0200913 if (reset_shell_surface_type(shsurf))
914 return;
915
Alex Wu4539b082012-03-01 12:57:46 +0800916 shsurf->fullscreen_output = shsurf->output;
917 shsurf->fullscreen.type = method;
918 shsurf->fullscreen.framerate = framerate;
919 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400920
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200921 shsurf->saved_x = es->geometry.x;
922 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800923 shsurf->saved_position_valid = true;
924
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300925 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300926 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500927
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400928 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500929 shsurf->output->current->width,
930 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400931}
932
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500933static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400934popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500935 struct wl_surface *surface, int32_t x, int32_t y)
936{
937 struct wl_input_device *device = grab->input_device;
938 struct shell_surface *priv =
939 container_of(grab, struct shell_surface, popup.grab);
940 struct wl_client *client = priv->surface->surface.resource.client;
941
Pekka Paalanencb108432012-01-19 16:25:40 +0200942 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400943 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500944 grab->focus = surface;
945 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400946 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500947 grab->focus = NULL;
948 }
949}
950
951static void
Scott Moreau447013d2012-02-18 05:05:29 -0700952popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200953 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500954{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500955 struct wl_resource *resource;
956
957 resource = grab->input_device->pointer_focus_resource;
958 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500959 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500960}
961
962static void
Scott Moreau447013d2012-02-18 05:05:29 -0700963popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400964 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500965{
966 struct wl_resource *resource;
967 struct shell_surface *shsurf =
968 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400969 struct wl_display *display;
970 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500971
972 resource = grab->input_device->pointer_focus_resource;
973 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400974 display = wl_client_get_display(resource->client);
975 serial = wl_display_get_serial(display);
976 wl_input_device_send_button(resource, serial,
977 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500978 } else if (state == 0 &&
979 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400980 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500981 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400982 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500983 shsurf->popup.grab.input_device = NULL;
984 }
985
986 if (state == 0)
987 shsurf->popup.initial_up = 1;
988}
989
Scott Moreau447013d2012-02-18 05:05:29 -0700990static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500991 popup_grab_focus,
992 popup_grab_motion,
993 popup_grab_button,
994};
995
996static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400997shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500998{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400999 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001000 struct weston_surface *es = shsurf->surface;
1001 struct weston_surface *parent = shsurf->parent->surface;
1002
1003 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001004 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001005
Pekka Paalanen938269a2012-02-07 14:19:01 +02001006 weston_surface_update_transform(parent);
1007 if (parent->transform.enabled) {
1008 shsurf->popup.parent_transform.matrix =
1009 parent->transform.matrix;
1010 } else {
1011 /* construct x, y translation matrix */
1012 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1013 shsurf->popup.parent_transform.matrix.d[12] =
1014 parent->geometry.x;
1015 shsurf->popup.parent_transform.matrix.d[13] =
1016 parent->geometry.y;
1017 }
1018 wl_list_insert(es->geometry.transformation_list.prev,
1019 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001020 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001021
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001022 shsurf->popup.initial_up = 0;
1023
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001024 /* We don't require the grab to still be active, but if another
1025 * grab has started in the meantime, we end the popup now. */
1026 if (device->grab_serial == shsurf->popup.serial) {
1027 wl_input_device_start_pointer_grab(device,
1028 &shsurf->popup.grab);
1029 } else {
1030 wl_shell_surface_send_popup_done(&shsurf->resource);
1031 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001032}
1033
1034static void
1035shell_surface_set_popup(struct wl_client *client,
1036 struct wl_resource *resource,
1037 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001038 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001039 struct wl_resource *parent_resource,
1040 int32_t x, int32_t y, uint32_t flags)
1041{
1042 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001043
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001044 shsurf->type = SHELL_SURFACE_POPUP;
1045 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001046 shsurf->popup.device = input_device_resource->data;
1047 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001048 shsurf->popup.x = x;
1049 shsurf->popup.y = y;
1050}
1051
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001052static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001053 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001054 shell_surface_move,
1055 shell_surface_resize,
1056 shell_surface_set_toplevel,
1057 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001058 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001059 shell_surface_set_popup,
1060 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001061};
1062
1063static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001064destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001065{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001066 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001067 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001068
Alex Wubd3354b2012-04-17 17:20:49 +08001069 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1070 shell_surface_is_top_fullscreen(shsurf)) {
1071 weston_output_switch_mode(shsurf->fullscreen_output,
1072 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001073 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001074
Alex Wuaa08e2d2012-03-05 11:01:40 +08001075 if (shsurf->fullscreen.black_surface)
1076 weston_surface_destroy(shsurf->fullscreen.black_surface);
1077
Alex Wubd3354b2012-04-17 17:20:49 +08001078 /* As destroy_resource() use wl_list_for_each_safe(),
1079 * we can always remove the listener.
1080 */
1081 wl_list_remove(&shsurf->surface_destroy_listener.link);
1082 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001083 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001084
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001085 wl_list_remove(&shsurf->link);
1086 free(shsurf);
1087}
1088
1089static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001090shell_destroy_shell_surface(struct wl_resource *resource)
1091{
1092 struct shell_surface *shsurf = resource->data;
1093
1094 destroy_shell_surface(shsurf);
1095}
1096
1097static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001098shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001099{
1100 struct shell_surface *shsurf = container_of(listener,
1101 struct shell_surface,
1102 surface_destroy_listener);
1103
Tiago Vignattibc052c92012-04-19 16:18:18 +03001104 /* tricky way to check if resource was in fact created */
1105 if (shsurf->resource.object.implementation != 0)
1106 wl_resource_destroy(&shsurf->resource);
1107 else
1108 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001109}
1110
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001111static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001112get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001113{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001114 struct wl_listener *listener;
1115
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001116 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1117 shell_handle_surface_destroy);
1118 if (listener)
1119 return container_of(listener, struct shell_surface,
1120 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001121
1122 return NULL;
1123}
1124
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001125static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001126shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1127
1128static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001129create_shell_surface(void *shell, struct weston_surface *surface,
1130 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001131{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001132 struct shell_surface *shsurf;
1133
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001134 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001135 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001136 return;
1137 }
1138
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001139 shsurf = calloc(1, sizeof *shsurf);
1140 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001141 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001142 return;
1143 }
1144
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001145 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001146 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001147
Tiago Vignattibc052c92012-04-19 16:18:18 +03001148 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001149 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001150 shsurf->unresponsive_animation.exists = 0;
1151 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001152 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001153 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001154 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001155 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1156 shsurf->fullscreen.framerate = 0;
1157 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001158 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001159 wl_list_init(&shsurf->fullscreen.transform.link);
1160
Tiago Vignattibc052c92012-04-19 16:18:18 +03001161 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001162 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1163 wl_signal_add(&surface->surface.resource.destroy_signal,
1164 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001165
1166 /* init link so its safe to always remove it in destroy_shell_surface */
1167 wl_list_init(&shsurf->link);
1168
Pekka Paalanen460099f2012-01-20 16:48:25 +02001169 /* empty when not in use */
1170 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001171 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001172
Pekka Paalanen98262232011-12-01 10:42:22 +02001173 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001174
Tiago Vignattibc052c92012-04-19 16:18:18 +03001175 *ret = shsurf;
1176}
1177
1178static void
1179shell_get_shell_surface(struct wl_client *client,
1180 struct wl_resource *resource,
1181 uint32_t id,
1182 struct wl_resource *surface_resource)
1183{
1184 struct weston_surface *surface = surface_resource->data;
1185 struct desktop_shell *shell = resource->data;
1186 struct shell_surface *shsurf;
1187
1188 if (get_shell_surface(surface)) {
1189 wl_resource_post_error(surface_resource,
1190 WL_DISPLAY_ERROR_INVALID_OBJECT,
1191 "desktop_shell::get_shell_surface already requested");
1192 return;
1193 }
1194
1195 create_shell_surface(shell, surface, &shsurf);
1196 if (!shsurf) {
1197 wl_resource_post_error(surface_resource,
1198 WL_DISPLAY_ERROR_INVALID_OBJECT,
1199 "surface->configure already set");
1200 return;
1201 }
1202
1203 shsurf->resource.destroy = shell_destroy_shell_surface;
1204 shsurf->resource.object.id = id;
1205 shsurf->resource.object.interface = &wl_shell_surface_interface;
1206 shsurf->resource.object.implementation =
1207 (void (**)(void)) &shell_surface_implementation;
1208 shsurf->resource.data = shsurf;
1209
1210 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001211}
1212
1213static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001214 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001215};
1216
Kristian Høgsberg07937562011-04-12 17:25:42 -04001217static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001218handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001219{
1220 proc->pid = 0;
1221}
1222
1223static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001224launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001225{
1226 if (shell->screensaver.binding)
1227 return;
1228
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001229 if (!shell->screensaver.path)
1230 return;
1231
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001232 if (shell->screensaver.process.pid != 0) {
1233 fprintf(stderr, "old screensaver still running\n");
1234 return;
1235 }
1236
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001237 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001238 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001239 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001240 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001241}
1242
1243static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001244terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001245{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001246 if (shell->screensaver.process.pid == 0)
1247 return;
1248
1249 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001250}
1251
1252static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001253show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001254{
1255 struct wl_list *list;
1256
1257 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001258 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001259 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001260 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001261
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001262 wl_list_remove(&surface->surface->layer_link);
1263 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001264 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001265 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001266}
1267
1268static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001269hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001270{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001271 wl_list_remove(&surface->surface->layer_link);
1272 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001273 surface->surface->output = NULL;
1274}
1275
1276static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001277desktop_shell_set_background(struct wl_client *client,
1278 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001279 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001280 struct wl_resource *surface_resource)
1281{
Tiago Vignattibe143262012-04-16 17:31:41 +03001282 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001283 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001284 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001285 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001286
Pekka Paalanen98262232011-12-01 10:42:22 +02001287 if (reset_shell_surface_type(shsurf))
1288 return;
1289
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001290 wl_list_for_each(priv, &shell->backgrounds, link) {
1291 if (priv->output == output_resource->data) {
1292 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001293 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001294 wl_list_remove(&priv->link);
1295 break;
1296 }
1297 }
1298
Pekka Paalanen068ae942011-11-28 14:11:15 +02001299 shsurf->type = SHELL_SURFACE_BACKGROUND;
1300 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001301
Pekka Paalanen068ae942011-11-28 14:11:15 +02001302 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001303
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001304 weston_surface_set_position(surface, shsurf->output->x,
1305 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001306
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001307 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001308 surface_resource,
1309 shsurf->output->current->width,
1310 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001311}
1312
1313static void
1314desktop_shell_set_panel(struct wl_client *client,
1315 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001316 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001317 struct wl_resource *surface_resource)
1318{
Tiago Vignattibe143262012-04-16 17:31:41 +03001319 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001320 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001321 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001322 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001323
Pekka Paalanen98262232011-12-01 10:42:22 +02001324 if (reset_shell_surface_type(shsurf))
1325 return;
1326
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001327 wl_list_for_each(priv, &shell->panels, link) {
1328 if (priv->output == output_resource->data) {
1329 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001330 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001331 wl_list_remove(&priv->link);
1332 break;
1333 }
1334 }
1335
Pekka Paalanen068ae942011-11-28 14:11:15 +02001336 shsurf->type = SHELL_SURFACE_PANEL;
1337 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001338
Pekka Paalanen068ae942011-11-28 14:11:15 +02001339 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001340
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001341 weston_surface_set_position(surface, shsurf->output->x,
1342 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001343
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001344 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001345 surface_resource,
1346 shsurf->output->current->width,
1347 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001348}
1349
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001350static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001351handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001352{
Tiago Vignattibe143262012-04-16 17:31:41 +03001353 struct desktop_shell *shell =
1354 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001355
1356 fprintf(stderr, "lock surface gone\n");
1357 shell->lock_surface = NULL;
1358}
1359
1360static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001361desktop_shell_set_lock_surface(struct wl_client *client,
1362 struct wl_resource *resource,
1363 struct wl_resource *surface_resource)
1364{
Tiago Vignattibe143262012-04-16 17:31:41 +03001365 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001366 struct shell_surface *surface = surface_resource->data;
1367
1368 if (reset_shell_surface_type(surface))
1369 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001370
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001371 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001372
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001373 if (!shell->locked)
1374 return;
1375
Pekka Paalanen98262232011-12-01 10:42:22 +02001376 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001377
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001378 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1379 wl_signal_add(&surface_resource->destroy_signal,
1380 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001381
Pekka Paalanen068ae942011-11-28 14:11:15 +02001382 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001383}
1384
1385static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001386resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001387{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001388 struct shell_surface *tmp;
1389
1390 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1391 hide_screensaver(shell, tmp);
1392
1393 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001394
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001395 wl_list_remove(&shell->lock_layer.link);
1396 wl_list_insert(&shell->compositor->cursor_layer.link,
1397 &shell->fullscreen_layer.link);
1398 wl_list_insert(&shell->fullscreen_layer.link,
1399 &shell->panel_layer.link);
1400 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001401
1402 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001403 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001405 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001406}
1407
1408static void
1409desktop_shell_unlock(struct wl_client *client,
1410 struct wl_resource *resource)
1411{
Tiago Vignattibe143262012-04-16 17:31:41 +03001412 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001413
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001414 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001415
1416 if (shell->locked)
1417 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001418}
1419
Kristian Høgsberg75840622011-09-06 13:48:16 -04001420static const struct desktop_shell_interface desktop_shell_implementation = {
1421 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001422 desktop_shell_set_panel,
1423 desktop_shell_set_lock_surface,
1424 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001425};
1426
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001427static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001428get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001429{
1430 struct shell_surface *shsurf;
1431
1432 shsurf = get_shell_surface(surface);
1433 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001434 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001435 return shsurf->type;
1436}
1437
Kristian Høgsberg75840622011-09-06 13:48:16 -04001438static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001439move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001440 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001441{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442 struct weston_surface *surface =
1443 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001444
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001445 if (surface == NULL)
1446 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001447
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001448 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001449 case SHELL_SURFACE_PANEL:
1450 case SHELL_SURFACE_BACKGROUND:
1451 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001452 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001453 return;
1454 default:
1455 break;
1456 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001457
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001458 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001459}
1460
1461static void
1462resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001463 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001464{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001465 struct weston_surface *surface =
1466 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001467 uint32_t edges = 0;
1468 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001469 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001470
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001471 if (surface == NULL)
1472 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001473
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001474 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001475 if (!shsurf)
1476 return;
1477
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001478 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001479 case SHELL_SURFACE_PANEL:
1480 case SHELL_SURFACE_BACKGROUND:
1481 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001482 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001483 return;
1484 default:
1485 break;
1486 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001487
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001488 weston_surface_from_global(surface,
1489 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001490
Pekka Paalanen60921e52012-01-25 15:55:43 +02001491 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001492 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001493 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001494 edges |= 0;
1495 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001496 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001497
Pekka Paalanen60921e52012-01-25 15:55:43 +02001498 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001499 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001500 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001501 edges |= 0;
1502 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001503 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001504
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001505 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001506 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001507}
1508
1509static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001510surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1511 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1512{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001513 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001514 struct shell_surface *shsurf;
1515 struct weston_surface *surface =
1516 (struct weston_surface *) device->pointer_focus;
1517
1518 if (surface == NULL)
1519 return;
1520
1521 shsurf = get_shell_surface(surface);
1522 if (!shsurf)
1523 return;
1524
1525 switch (shsurf->type) {
1526 case SHELL_SURFACE_BACKGROUND:
1527 case SHELL_SURFACE_SCREENSAVER:
1528 return;
1529 default:
1530 break;
1531 }
1532
1533 surface->alpha += value * step;
1534
1535 if (surface->alpha > 255)
1536 surface->alpha = 255;
1537 if (surface->alpha < step)
1538 surface->alpha = step;
1539
1540 surface->geometry.dirty = 1;
1541 weston_surface_damage(surface);
1542}
1543
1544static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001545zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001546 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001547{
1548 struct weston_input_device *wd = (struct weston_input_device *) device;
1549 struct weston_compositor *compositor = wd->compositor;
1550 struct weston_output *output;
1551
1552 wl_list_for_each(output, &compositor->output_list, link) {
1553 if (pixman_region32_contains_point(&output->region,
1554 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001555 output->zoom.active = 1;
1556 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001557
1558 if (output->zoom.level >= 1.0) {
1559 output->zoom.active = 0;
1560 output->zoom.level = 1.0;
1561 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001562
1563 if (output->zoom.level < output->zoom.increment)
1564 output->zoom.level = output->zoom.increment;
1565
1566 weston_output_update_zoom(output, device->x, device->y);
1567 }
1568 }
1569}
1570
Scott Moreauccbf29d2012-02-22 14:21:41 -07001571static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001572terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001573 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001574{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001575 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001576
1577 if (state)
1578 wl_display_terminate(compositor->wl_display);
1579}
1580
1581static void
Scott Moreau447013d2012-02-18 05:05:29 -07001582rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001583 uint32_t time, int32_t x, int32_t y)
1584{
1585 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001586 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001587 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001588 struct shell_surface *shsurf = rotate->base.shsurf;
1589 struct weston_surface *surface;
1590 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1591
1592 if (!shsurf)
1593 return;
1594
1595 surface = shsurf->surface;
1596
1597 cx = 0.5f * surface->geometry.width;
1598 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001599
1600 dx = device->x - rotate->center.x;
1601 dy = device->y - rotate->center.y;
1602 r = sqrtf(dx * dx + dy * dy);
1603
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001604 wl_list_remove(&shsurf->rotation.transform.link);
1605 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001606
1607 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001608 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001609 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001610
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001611 weston_matrix_init(&rotate->rotation);
1612 rotate->rotation.d[0] = dx / r;
1613 rotate->rotation.d[4] = -dy / r;
1614 rotate->rotation.d[1] = -rotate->rotation.d[4];
1615 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001616
1617 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001618 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001619 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001620 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001621 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001622
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001623 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001624 &shsurf->surface->geometry.transformation_list,
1625 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001626 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001627 wl_list_init(&shsurf->rotation.transform.link);
1628 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001629 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001630 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001631
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001632 /* We need to adjust the position of the surface
1633 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001634 cposx = surface->geometry.x + cx;
1635 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001636 dposx = rotate->center.x - cposx;
1637 dposy = rotate->center.y - cposy;
1638 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001639 weston_surface_set_position(surface,
1640 surface->geometry.x + dposx,
1641 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001642 }
1643
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001644 /* Repaint implies weston_surface_update_transform(), which
1645 * lazily applies the damage due to rotation update.
1646 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001647 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001648}
1649
1650static void
Scott Moreau447013d2012-02-18 05:05:29 -07001651rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001652 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001653{
1654 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001655 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001656 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001657 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001658
1659 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001660 if (shsurf)
1661 weston_matrix_multiply(&shsurf->rotation.rotation,
1662 &rotate->rotation);
1663 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001664 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001665 free(rotate);
1666 }
1667}
1668
Scott Moreau447013d2012-02-18 05:05:29 -07001669static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001670 noop_grab_focus,
1671 rotate_grab_motion,
1672 rotate_grab_button,
1673};
1674
1675static void
1676rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001677 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001678{
1679 struct weston_surface *base_surface =
1680 (struct weston_surface *) device->pointer_focus;
1681 struct shell_surface *surface;
1682 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001683 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001684 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001685
1686 if (base_surface == NULL)
1687 return;
1688
1689 surface = get_shell_surface(base_surface);
1690 if (!surface)
1691 return;
1692
1693 switch (surface->type) {
1694 case SHELL_SURFACE_PANEL:
1695 case SHELL_SURFACE_BACKGROUND:
1696 case SHELL_SURFACE_FULLSCREEN:
1697 case SHELL_SURFACE_SCREENSAVER:
1698 return;
1699 default:
1700 break;
1701 }
1702
Pekka Paalanen460099f2012-01-20 16:48:25 +02001703 rotate = malloc(sizeof *rotate);
1704 if (!rotate)
1705 return;
1706
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001707 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001708
1709 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001710 surface->surface->geometry.width / 2,
1711 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001712 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001713
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001714 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001715
1716 dx = device->x - rotate->center.x;
1717 dy = device->y - rotate->center.y;
1718 r = sqrtf(dx * dx + dy * dy);
1719 if (r > 20.0f) {
1720 struct weston_matrix inverse;
1721
1722 weston_matrix_init(&inverse);
1723 inverse.d[0] = dx / r;
1724 inverse.d[4] = dy / r;
1725 inverse.d[1] = -inverse.d[4];
1726 inverse.d[5] = inverse.d[0];
1727 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001728
1729 weston_matrix_init(&rotate->rotation);
1730 rotate->rotation.d[0] = dx / r;
1731 rotate->rotation.d[4] = -dy / r;
1732 rotate->rotation.d[1] = -rotate->rotation.d[4];
1733 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001734 } else {
1735 weston_matrix_init(&surface->rotation.rotation);
1736 weston_matrix_init(&rotate->rotation);
1737 }
1738
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001739 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001740}
1741
1742static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001743activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001744 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001745{
Alex Wu21858432012-04-01 20:13:08 +08001746 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001747
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001749
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001750 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001751 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001752 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001753 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001754 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001755
Pekka Paalanen77346a62011-11-30 16:26:35 +02001756 case SHELL_SURFACE_SCREENSAVER:
1757 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001758 if (shell->lock_surface)
1759 weston_surface_restack(es,
1760 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001761 break;
Alex Wu4539b082012-03-01 12:57:46 +08001762 case SHELL_SURFACE_FULLSCREEN:
1763 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001764 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001765 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001766 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001767 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001768 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001769 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1770 wl_list_for_each_reverse_safe(surf,
1771 prev,
1772 &shell->fullscreen_layer.surface_list,
1773 layer_link)
1774 weston_surface_restack(surf,
1775 &shell->toplevel_layer.surface_list);
1776 }
1777
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001778 weston_surface_restack(es,
1779 &shell->toplevel_layer.surface_list);
1780 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001781 }
1782}
1783
Alex Wu21858432012-04-01 20:13:08 +08001784/* no-op func for checking black surface */
1785static void
1786black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1787{
1788}
1789
1790static bool
1791is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1792{
1793 if (es->configure == black_surface_configure) {
1794 if (fs_surface)
1795 *fs_surface = (struct weston_surface *)es->private;
1796 return true;
1797 }
1798 return false;
1799}
1800
Kristian Høgsberg75840622011-09-06 13:48:16 -04001801static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001802click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001803 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001804 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001805{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001806 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001807 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001808 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001809 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001810
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001811 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001812 if (!focus)
1813 return;
1814
Alex Wu21858432012-04-01 20:13:08 +08001815 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001816 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001817
Alex Wu9c35e6b2012-03-05 14:13:13 +08001818 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001819 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001820}
1821
1822static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001823lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001824{
Tiago Vignattibe143262012-04-16 17:31:41 +03001825 struct desktop_shell *shell =
1826 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001827 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001828 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001829 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001830
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001831 if (shell->locked) {
1832 wl_list_for_each(output, &shell->compositor->output_list, link)
1833 /* TODO: find a way to jump to other DPMS levels */
1834 if (output->set_dpms)
1835 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001836 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001837 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001838
1839 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001840
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001841 /* Hide all surfaces by removing the fullscreen, panel and
1842 * toplevel layers. This way nothing else can show or receive
1843 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001844
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001845 wl_list_remove(&shell->panel_layer.link);
1846 wl_list_remove(&shell->toplevel_layer.link);
1847 wl_list_remove(&shell->fullscreen_layer.link);
1848 wl_list_insert(&shell->compositor->cursor_layer.link,
1849 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001850
Pekka Paalanen77346a62011-11-30 16:26:35 +02001851 launch_screensaver(shell);
1852
1853 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1854 show_screensaver(shell, shsurf);
1855
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001856 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001857 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001858 weston_compositor_wake(shell->compositor);
1859 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001860 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001861
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001862 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001863 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001864
1865 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001866 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1867 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001868 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001869 }
1870
1871 /* TODO: disable bindings that should not work while locked. */
1872
1873 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001874}
1875
1876static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001877unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001878{
Tiago Vignattibe143262012-04-16 17:31:41 +03001879 struct desktop_shell *shell =
1880 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001881
Pekka Paalanend81c2162011-11-16 13:47:34 +02001882 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001883 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001884 return;
1885 }
1886
1887 /* If desktop-shell client has gone away, unlock immediately. */
1888 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001889 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001890 return;
1891 }
1892
1893 if (shell->prepare_event_sent)
1894 return;
1895
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001896 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001897 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001898}
1899
1900static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001901center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001902{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001903 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001904 GLfloat x = (mode->width - surface->geometry.width) / 2;
1905 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001906
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001907 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001908}
1909
1910static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001911map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001912 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001913{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001914 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001915 struct shell_surface *shsurf;
1916 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001917 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001918 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001919
Pekka Paalanen77346a62011-11-30 16:26:35 +02001920 shsurf = get_shell_surface(surface);
1921 if (shsurf)
1922 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001923
Pekka Paalanen60921e52012-01-25 15:55:43 +02001924 surface->geometry.width = width;
1925 surface->geometry.height = height;
1926 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001927
1928 /* initial positioning, see also configure() */
1929 switch (surface_type) {
1930 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001931 weston_surface_set_position(surface, 10 + random() % 400,
1932 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001933 break;
1934 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001935 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001936 break;
Alex Wu4539b082012-03-01 12:57:46 +08001937 case SHELL_SURFACE_FULLSCREEN:
1938 shell_map_fullscreen(shsurf);
1939 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001940 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001941 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001942 panel_height = get_output_panel_height(shell,surface->output);
1943 weston_surface_set_position(surface, surface->output->x,
1944 surface->output->y + panel_height);
1945 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001946 case SHELL_SURFACE_LOCK:
1947 center_on_output(surface, get_default_output(compositor));
1948 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001949 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001950 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001951 case SHELL_SURFACE_NONE:
1952 weston_surface_set_position(surface,
1953 surface->geometry.x + sx,
1954 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001955 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001956 default:
1957 ;
1958 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001959
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001960 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001961 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001962 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001963 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001964 wl_list_insert(&shell->background_layer.surface_list,
1965 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001966 break;
1967 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001968 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001969 wl_list_insert(&shell->panel_layer.surface_list,
1970 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001971 break;
1972 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001973 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001974 wl_list_insert(&shell->lock_layer.surface_list,
1975 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001976 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001977 break;
1978 case SHELL_SURFACE_SCREENSAVER:
1979 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001980 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001981 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001982 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001984 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001985 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001986 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001987 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001988 case SHELL_SURFACE_POPUP:
1989 case SHELL_SURFACE_TRANSIENT:
1990 parent = shsurf->parent->surface;
1991 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1992 break;
Alex Wu4539b082012-03-01 12:57:46 +08001993 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001994 case SHELL_SURFACE_NONE:
1995 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001996 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001997 wl_list_insert(&shell->toplevel_layer.surface_list,
1998 &surface->layer_link);
1999 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002000 }
2001
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002002 if (surface_type != SHELL_SURFACE_NONE) {
2003 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002004 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2005 surface->output = shsurf->output;
2006 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002007
Juan Zhao7bb92f02011-12-15 11:31:51 -05002008 switch (surface_type) {
2009 case SHELL_SURFACE_TOPLEVEL:
2010 case SHELL_SURFACE_TRANSIENT:
2011 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002012 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002013 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002014 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002015 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002016 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002017 break;
2018 default:
2019 break;
2020 }
2021
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002022 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002023 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002024}
2025
2026static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002027configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002028 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002029{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002030 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2031 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002032
Pekka Paalanen77346a62011-11-30 16:26:35 +02002033 shsurf = get_shell_surface(surface);
2034 if (shsurf)
2035 surface_type = shsurf->type;
2036
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002037 surface->geometry.x = x;
2038 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002039 surface->geometry.width = width;
2040 surface->geometry.height = height;
2041 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002042
2043 switch (surface_type) {
2044 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002045 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002046 break;
Alex Wu4539b082012-03-01 12:57:46 +08002047 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002048 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002049 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002050 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002051 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002052 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002053 surface->geometry.x = surface->output->x;
2054 surface->geometry.y = surface->output->y +
2055 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002056 break;
Alex Wu4539b082012-03-01 12:57:46 +08002057 case SHELL_SURFACE_TOPLEVEL:
2058 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002059 default:
2060 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002061 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002062
Alex Wu4539b082012-03-01 12:57:46 +08002063 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002064 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002065 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002066
2067 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2068 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002069 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2070 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002071 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002072}
2073
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002074static void
2075shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2076{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002077 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002078 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002079
2080 if (!weston_surface_is_mapped(es)) {
2081 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002082 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002083 es->geometry.width != es->buffer->width ||
2084 es->geometry.height != es->buffer->height) {
2085 GLfloat from_x, from_y;
2086 GLfloat to_x, to_y;
2087
2088 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2089 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2090 configure(shell, es,
2091 es->geometry.x + to_x - from_x,
2092 es->geometry.y + to_y - from_y,
2093 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002094 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002095 }
2096}
2097
Tiago Vignattibe143262012-04-16 17:31:41 +03002098static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002099
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002100static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002101desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002102{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002103 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002104 struct desktop_shell *shell =
2105 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002106
2107 shell->child.process.pid = 0;
2108 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002109
2110 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2111 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002112 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002113 shell->child.deathstamp = time;
2114 shell->child.deathcount = 0;
2115 }
2116
2117 shell->child.deathcount++;
2118 if (shell->child.deathcount > 5) {
2119 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2120 return;
2121 }
2122
2123 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2124 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002125}
2126
2127static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002128launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002129{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002130 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002131
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002132 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002133 &shell->child.process,
2134 shell_exe,
2135 desktop_shell_sigchld);
2136
2137 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002138 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002139 return 0;
2140}
2141
2142static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002143bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2144{
Tiago Vignattibe143262012-04-16 17:31:41 +03002145 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002146
2147 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002148 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002149}
2150
Kristian Høgsberg75840622011-09-06 13:48:16 -04002151static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002152unbind_desktop_shell(struct wl_resource *resource)
2153{
Tiago Vignattibe143262012-04-16 17:31:41 +03002154 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002155
2156 if (shell->locked)
2157 resume_desktop(shell);
2158
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002159 shell->child.desktop_shell = NULL;
2160 shell->prepare_event_sent = false;
2161 free(resource);
2162}
2163
2164static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002165bind_desktop_shell(struct wl_client *client,
2166 void *data, uint32_t version, uint32_t id)
2167{
Tiago Vignattibe143262012-04-16 17:31:41 +03002168 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002169 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002170
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002171 resource = wl_client_add_object(client, &desktop_shell_interface,
2172 &desktop_shell_implementation,
2173 id, shell);
2174
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002175 if (client == shell->child.client) {
2176 resource->destroy = unbind_desktop_shell;
2177 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002178 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002179 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002180
2181 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2182 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002183 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002184}
2185
Pekka Paalanen6e168112011-11-24 11:34:05 +02002186static void
2187screensaver_set_surface(struct wl_client *client,
2188 struct wl_resource *resource,
2189 struct wl_resource *shell_surface_resource,
2190 struct wl_resource *output_resource)
2191{
Tiago Vignattibe143262012-04-16 17:31:41 +03002192 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002193 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002194 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002195
Pekka Paalanen98262232011-12-01 10:42:22 +02002196 if (reset_shell_surface_type(surface))
2197 return;
2198
Pekka Paalanen77346a62011-11-30 16:26:35 +02002199 surface->type = SHELL_SURFACE_SCREENSAVER;
2200
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002201 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002202 surface->output = output;
2203 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002204}
2205
2206static const struct screensaver_interface screensaver_implementation = {
2207 screensaver_set_surface
2208};
2209
2210static void
2211unbind_screensaver(struct wl_resource *resource)
2212{
Tiago Vignattibe143262012-04-16 17:31:41 +03002213 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002214
Pekka Paalanen77346a62011-11-30 16:26:35 +02002215 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002216 free(resource);
2217}
2218
2219static void
2220bind_screensaver(struct wl_client *client,
2221 void *data, uint32_t version, uint32_t id)
2222{
Tiago Vignattibe143262012-04-16 17:31:41 +03002223 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002224 struct wl_resource *resource;
2225
2226 resource = wl_client_add_object(client, &screensaver_interface,
2227 &screensaver_implementation,
2228 id, shell);
2229
Pekka Paalanen77346a62011-11-30 16:26:35 +02002230 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002231 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002232 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002233 return;
2234 }
2235
2236 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2237 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002238 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002239}
2240
Kristian Høgsberg07045392012-02-19 18:52:44 -05002241struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002242 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002243 struct weston_surface *current;
2244 struct wl_listener listener;
2245 struct wl_keyboard_grab grab;
2246};
2247
2248static void
2249switcher_next(struct switcher *switcher)
2250{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002251 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002252 struct weston_surface *surface;
2253 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002254 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002255
2256 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002257 switch (get_shell_surface_type(surface)) {
2258 case SHELL_SURFACE_TOPLEVEL:
2259 case SHELL_SURFACE_FULLSCREEN:
2260 case SHELL_SURFACE_MAXIMIZED:
2261 if (first == NULL)
2262 first = surface;
2263 if (prev == switcher->current)
2264 next = surface;
2265 prev = surface;
2266 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002267 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002268 weston_surface_damage(surface);
2269 break;
2270 default:
2271 break;
2272 }
Alex Wu1659daa2012-04-01 20:13:09 +08002273
2274 if (is_black_surface(surface, NULL)) {
2275 surface->alpha = 64;
2276 surface->geometry.dirty = 1;
2277 weston_surface_damage(surface);
2278 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002279 }
2280
2281 if (next == NULL)
2282 next = first;
2283
Alex Wu07b26062012-03-12 16:06:01 +08002284 if (next == NULL)
2285 return;
2286
Kristian Høgsberg07045392012-02-19 18:52:44 -05002287 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002288 wl_signal_add(&next->surface.resource.destroy_signal,
2289 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002290
2291 switcher->current = next;
2292 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002293
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002294 shsurf = get_shell_surface(switcher->current);
2295 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2296 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002297}
2298
2299static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002300switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002301{
2302 struct switcher *switcher =
2303 container_of(listener, struct switcher, listener);
2304
2305 switcher_next(switcher);
2306}
2307
2308static void
2309switcher_destroy(struct switcher *switcher, uint32_t time)
2310{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002311 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002312 struct weston_surface *surface;
2313 struct weston_input_device *device =
2314 (struct weston_input_device *) switcher->grab.input_device;
2315
2316 wl_list_for_each(surface, &compositor->surface_list, link) {
2317 surface->alpha = 255;
2318 weston_surface_damage(surface);
2319 }
2320
Alex Wu07b26062012-03-12 16:06:01 +08002321 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002322 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002323 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002324 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002325 free(switcher);
2326}
2327
2328static void
2329switcher_key(struct wl_keyboard_grab *grab,
2330 uint32_t time, uint32_t key, int32_t state)
2331{
2332 struct switcher *switcher = container_of(grab, struct switcher, grab);
2333 struct weston_input_device *device =
2334 (struct weston_input_device *) grab->input_device;
2335
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002336 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002337 switcher_destroy(switcher, time);
2338 } else if (key == KEY_TAB && state) {
2339 switcher_next(switcher);
2340 }
2341};
2342
2343static const struct wl_keyboard_grab_interface switcher_grab = {
2344 switcher_key
2345};
2346
2347static void
2348switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002349 uint32_t key, uint32_t button, uint32_t axis,
2350 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002351{
Tiago Vignattibe143262012-04-16 17:31:41 +03002352 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002353 struct switcher *switcher;
2354
2355 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002356 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002357 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002358 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002359 wl_list_init(&switcher->listener.link);
2360
2361 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002362 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2363 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002364 switcher_next(switcher);
2365}
2366
Pekka Paalanen3c647232011-12-22 13:43:43 +02002367static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002368backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002369 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002370{
2371 struct weston_compositor *compositor = data;
2372 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002373 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002374
2375 /* TODO: we're limiting to simple use cases, where we assume just
2376 * control on the primary display. We'd have to extend later if we
2377 * ever get support for setting backlights on random desktop LCD
2378 * panels though */
2379 output = get_default_output(compositor);
2380 if (!output)
2381 return;
2382
2383 if (!output->set_backlight)
2384 return;
2385
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002386 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2387 backlight_new = output->backlight_current - 25;
2388 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2389 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002390
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002391 if (backlight_new < 5)
2392 backlight_new = 5;
2393 if (backlight_new > 255)
2394 backlight_new = 255;
2395
2396 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002397 output->set_backlight(output, output->backlight_current);
2398}
2399
2400static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002401debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002402 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002403{
Tiago Vignattibe143262012-04-16 17:31:41 +03002404 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002405 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002406 struct weston_surface *surface;
2407
2408 if (shell->debug_repaint_surface) {
2409 weston_surface_destroy(shell->debug_repaint_surface);
2410 shell->debug_repaint_surface = NULL;
2411 } else {
2412 surface = weston_surface_create(compositor);
2413 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2414 weston_surface_configure(surface, 0, 0, 8192, 8192);
2415 wl_list_insert(&compositor->fade_layer.surface_list,
2416 &surface->layer_link);
2417 weston_surface_assign_output(surface);
2418 pixman_region32_init(&surface->input);
2419
2420 /* Here's the dirty little trick that makes the
2421 * repaint debugging work: we force an
2422 * update_transform first to update dependent state
2423 * and clear the geometry.dirty bit. Then we clear
2424 * the surface damage so it only gets repainted
2425 * piecewise as we repaint other things. */
2426
2427 weston_surface_update_transform(surface);
2428 pixman_region32_fini(&surface->damage);
2429 pixman_region32_init(&surface->damage);
2430 shell->debug_repaint_surface = surface;
2431 }
2432}
2433
2434static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002435shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002436{
Tiago Vignattibe143262012-04-16 17:31:41 +03002437 struct desktop_shell *shell =
2438 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002439
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002440 if (shell->child.client)
2441 wl_client_destroy(shell->child.client);
2442
Pekka Paalanen3c647232011-12-22 13:43:43 +02002443 free(shell->screensaver.path);
2444 free(shell);
2445}
2446
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002447static void
2448shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2449{
2450 uint32_t mod;
2451
2452 /* fixed bindings */
2453 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2454 MODIFIER_CTRL | MODIFIER_ALT,
2455 terminate_binding, ec);
2456 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2457 click_to_activate_binding, shell);
2458 weston_compositor_add_binding(ec, 0, 0,
2459 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2460 MODIFIER_SUPER | MODIFIER_ALT,
2461 surface_opacity_binding, NULL);
2462 weston_compositor_add_binding(ec, 0, 0,
2463 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2464 MODIFIER_SUPER, zoom_binding, NULL);
2465
2466 /* configurable bindings */
2467 mod = shell->binding_modifier;
2468 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2469 move_binding, shell);
2470 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2471 resize_binding, shell);
2472 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2473 rotate_binding, NULL);
2474 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2475 switcher_binding, shell);
2476 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2477 backlight_binding, ec);
2478 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2479 backlight_binding, ec);
2480 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2481 backlight_binding, ec);
2482 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2483 backlight_binding, ec);
2484 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2485 debug_repaint_binding, shell);
2486}
2487
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002488int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002489shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002490
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002491WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002492shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002493{
Tiago Vignattibe143262012-04-16 17:31:41 +03002494 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002495
2496 shell = malloc(sizeof *shell);
2497 if (shell == NULL)
2498 return -1;
2499
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002500 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002501 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002502
2503 shell->destroy_listener.notify = shell_destroy;
2504 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2505 shell->lock_listener.notify = lock;
2506 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2507 shell->unlock_listener.notify = unlock;
2508 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002509 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002510 ec->shell_interface.create_shell_surface = create_shell_surface;
2511 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002512
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002513 wl_list_init(&shell->backgrounds);
2514 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002515 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002516
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002517 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2518 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2519 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2520 weston_layer_init(&shell->background_layer,
2521 &shell->toplevel_layer.link);
2522 wl_list_init(&shell->lock_layer.surface_list);
2523
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002524 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002525
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002526 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2527 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002528 return -1;
2529
Kristian Høgsberg75840622011-09-06 13:48:16 -04002530 if (wl_display_add_global(ec->wl_display,
2531 &desktop_shell_interface,
2532 shell, bind_desktop_shell) == NULL)
2533 return -1;
2534
Pekka Paalanen6e168112011-11-24 11:34:05 +02002535 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2536 shell, bind_screensaver) == NULL)
2537 return -1;
2538
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002539 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002540 if (launch_desktop_shell_process(shell) != 0)
2541 return -1;
2542
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002543 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002544
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002545 return 0;
2546}