blob: 48c66db7a3439837432a79b7d173b431afc585be [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Juan Zhaoe10d2792012-04-25 19:09:52 +080039enum animation_type {
40 ANIMATION_NONE,
41
42 ANIMATION_ZOOM,
43 ANIMATION_FADE
44};
45
Tiago Vignattibe143262012-04-16 17:31:41 +030046struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050047 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040048
49 struct wl_listener lock_listener;
50 struct wl_listener unlock_listener;
51 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050053 struct weston_layer fullscreen_layer;
54 struct weston_layer panel_layer;
55 struct weston_layer toplevel_layer;
56 struct weston_layer background_layer;
57 struct weston_layer lock_layer;
58
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020059 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050060 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020063
64 unsigned deathcount;
65 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020066 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020067
68 bool locked;
69 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020070
Pekka Paalanen068ae942011-11-28 14:11:15 +020071 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050072 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073
74 struct wl_list backgrounds;
75 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020076
Pekka Paalanen77346a62011-11-30 16:26:35 +020077 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020078 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020079 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020080 struct wl_resource *binding;
81 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020083 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050084
Tiago Vignatti0b52d482012-04-20 18:54:25 +030085 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080086 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050087 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040088};
89
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020091 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050092
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093 SHELL_SURFACE_PANEL,
94 SHELL_SURFACE_BACKGROUND,
95 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020096 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050097
98 SHELL_SURFACE_TOPLEVEL,
99 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800101 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200103};
104
Scott Moreauff1db4a2012-04-17 19:06:18 -0600105struct ping_timer {
106 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600107 uint32_t serial;
108};
109
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_resource resource;
112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300116 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200117
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500118 enum shell_surface_type type;
119 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800120 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800121 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600122 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100123
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500124 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200125 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500126 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200127 } rotation;
128
129 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700130 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500131 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200132 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400134 struct wl_input_device *device;
135 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 } popup;
137
Alex Wu4539b082012-03-01 12:57:46 +0800138 struct {
139 enum wl_shell_surface_fullscreen_method type;
140 struct weston_transform transform; /* matrix from x, y */
141 uint32_t framerate;
142 struct weston_surface *black_surface;
143 } fullscreen;
144
Scott Moreauff1db4a2012-04-17 19:06:18 -0600145 struct ping_timer *ping_timer;
146
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600147 struct {
148 struct weston_animation current;
149 int exists;
150 int fading_in;
151 uint32_t timestamp;
152 } unresponsive_animation;
153
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500154 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500155 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100156 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300157
158 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200159};
160
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300161struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700162 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300163 struct shell_surface *shsurf;
164 struct wl_listener shsurf_destroy_listener;
165};
166
167struct weston_move_grab {
168 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500169 int32_t dx, dy;
170};
171
Pekka Paalanen460099f2012-01-20 16:48:25 +0200172struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300173 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500174 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200175 struct {
176 int32_t x;
177 int32_t y;
178 } center;
179};
180
Alex Wubd3354b2012-04-17 17:20:49 +0800181static struct shell_surface *
182get_shell_surface(struct weston_surface *surface);
183
184static struct desktop_shell *
185shell_surface_get_shell(struct shell_surface *shsurf);
186
187static bool
188shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
189{
190 struct desktop_shell *shell;
191 struct weston_surface *top_fs_es;
192
193 shell = shell_surface_get_shell(shsurf);
194
195 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
196 return false;
197
198 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
199 struct weston_surface,
200 layer_link);
201 return (shsurf == get_shell_surface(top_fs_es));
202}
203
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500204static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400205destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206{
207 struct shell_grab *grab;
208
209 grab = container_of(listener, struct shell_grab,
210 shsurf_destroy_listener);
211
212 grab->shsurf = NULL;
213}
214
215static void
216shell_grab_init(struct shell_grab *grab,
217 const struct wl_pointer_grab_interface *interface,
218 struct shell_surface *shsurf)
219{
220 grab->grab.interface = interface;
221 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400222 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
223 wl_signal_add(&shsurf->resource.destroy_signal,
224 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300225
226}
227
228static void
229shell_grab_finish(struct shell_grab *grab)
230{
231 wl_list_remove(&grab->shsurf_destroy_listener.link);
232}
233
234static void
Alex Wu4539b082012-03-01 12:57:46 +0800235center_on_output(struct weston_surface *surface,
236 struct weston_output *output);
237
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300238static uint32_t
239get_modifier(char *modifier)
240{
241 if (!modifier)
242 return MODIFIER_SUPER;
243
244 if (!strcmp("ctrl", modifier))
245 return MODIFIER_CTRL;
246 else if (!strcmp("alt", modifier))
247 return MODIFIER_ALT;
248 else if (!strcmp("super", modifier))
249 return MODIFIER_SUPER;
250 else
251 return MODIFIER_SUPER;
252}
253
Juan Zhaoe10d2792012-04-25 19:09:52 +0800254static enum animation_type
255get_animation_type(char *animation)
256{
257 if (!animation)
258 return ANIMATION_NONE;
259
260 if (!strcmp("zoom", animation))
261 return ANIMATION_ZOOM;
262 else if (!strcmp("fade", animation))
263 return ANIMATION_FADE;
264 else
265 return ANIMATION_NONE;
266}
267
Alex Wu4539b082012-03-01 12:57:46 +0800268static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300269shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200270{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200271 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200272 char *path = NULL;
273 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300274 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800275 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200276
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400277 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300278 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800279 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200280 };
281
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400282 struct config_key saver_keys[] = {
283 { "path", CONFIG_KEY_STRING, &path },
284 { "duration", CONFIG_KEY_INTEGER, &duration },
285 };
286
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200287 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400288 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200289 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
290 };
291
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200292 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500293 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200294 free(config_file);
295
Pekka Paalanen7296e792011-12-07 16:22:00 +0200296 shell->screensaver.path = path;
297 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300298 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800299 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200300}
301
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200302static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400303noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500304 struct wl_surface *surface, int32_t x, int32_t y)
305{
306 grab->focus = NULL;
307}
308
309static void
Scott Moreau447013d2012-02-18 05:05:29 -0700310move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500311 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500313 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500314 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315 struct shell_surface *shsurf = move->base.shsurf;
316 struct weston_surface *es;
317
318 if (!shsurf)
319 return;
320
321 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500323 weston_surface_configure(es,
324 device->x + move->dx,
325 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200326 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500327}
328
329static void
Scott Moreau447013d2012-02-18 05:05:29 -0700330move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400331 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500332{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300333 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
334 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500335 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500336
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500337 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400339 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500340 free(grab);
341 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342}
343
Scott Moreau447013d2012-02-18 05:05:29 -0700344static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500345 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500346 move_grab_motion,
347 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500348};
349
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600350static void
351unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
352{
353 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
354
355 if(!shsurf->unresponsive_animation.exists) {
356 wl_list_insert(&shsurf->surface->compositor->animation_list,
357 &shsurf->unresponsive_animation.current.link);
358 shsurf->unresponsive_animation.exists = 1;
359 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
360 weston_surface_damage(shsurf->surface);
361 }
362}
363
364static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600365unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600366 struct weston_output *output, uint32_t msecs)
367{
368 struct shell_surface *shsurf =
369 container_of(animation, struct shell_surface, unresponsive_animation.current);
370 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600371 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600372
373 if (!surface || !shsurf)
374 return;
375
376 if (shsurf->unresponsive_animation.fading_in) {
377 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
378 if (surface->saturation > 1)
379 surface->saturation -= 5;
380 if (surface->brightness > 200)
381 surface->brightness--;
382
383 shsurf->unresponsive_animation.timestamp += step;
384 }
385
386 if (surface->saturation <= 1 && surface->brightness <= 200) {
387 wl_list_remove(&shsurf->unresponsive_animation.current.link);
388 shsurf->unresponsive_animation.exists = 0;
389 }
390 }
391 else {
392 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
393 if (surface->saturation < 255)
394 surface->saturation += 5;
395 if (surface->brightness < 255)
396 surface->brightness++;
397
398 shsurf->unresponsive_animation.timestamp += step;
399 }
400
401 if (surface->saturation >= 255 && surface->brightness >= 255) {
402 surface->saturation = surface->brightness = 255;
403 wl_list_remove(&shsurf->unresponsive_animation.current.link);
404 shsurf->unresponsive_animation.exists = 0;
405 }
406 }
407
408 surface->geometry.dirty = 1;
409 weston_surface_damage(surface);
410}
411
Scott Moreau9521d5e2012-04-19 13:06:17 -0600412static void
413ping_timer_destroy(struct shell_surface *shsurf)
414{
415 if (!shsurf || !shsurf->ping_timer)
416 return;
417
418 if (shsurf->ping_timer->source)
419 wl_event_source_remove(shsurf->ping_timer->source);
420
421 free(shsurf->ping_timer);
422 shsurf->ping_timer = NULL;
423}
424
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400425static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600426ping_timeout_handler(void *data)
427{
428 struct shell_surface *shsurf = data;
429
Scott Moreau9521d5e2012-04-19 13:06:17 -0600430 /* Client is not responding */
431 shsurf->unresponsive = 1;
432 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600433
434 return 1;
435}
436
437static void
438ping_handler(struct weston_surface *surface, uint32_t serial)
439{
440 struct shell_surface *shsurf;
441 shsurf = get_shell_surface(surface);
442 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600443 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600444
445 if (!shsurf)
446 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400447 if (!shsurf->resource.client)
448 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600449
450 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300451 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600452 if (!shsurf->ping_timer)
453 return;
454
455 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600456 loop = wl_display_get_event_loop(surface->compositor->wl_display);
457 shsurf->ping_timer->source =
458 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
459 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
460
461 wl_shell_surface_send_ping(&shsurf->resource, serial);
462 }
463}
464
465static void
466shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
467 uint32_t serial)
468{
469 struct shell_surface *shsurf = resource->data;
470
Scott Moreauff1db4a2012-04-17 19:06:18 -0600471 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600472 if (shsurf->unresponsive) {
473 /* Received pong from previously unresponsive client */
474 unresponsive_surface_fade(shsurf, true);
475 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600476 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600477 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600478 }
479}
480
481static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500482weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400483 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500485 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300486 struct shell_surface *shsurf = get_shell_surface(es);
487
488 if (!shsurf)
489 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500490
491 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400492 if (!move)
493 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500494
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300495 shell_grab_init(&move->base, &move_grab_interface, shsurf);
496
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200497 move->dx = es->geometry.x - wd->input_device.grab_x;
498 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500499
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300500 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400501 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500502
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400503 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400504
505 return 0;
506}
507
508static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200509shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400510 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400511{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500512 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200513 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400514
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500515 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400516 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500517 wd->input_device.pointer_focus != &shsurf->surface->surface)
518 return;
519
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400520 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400521 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500522}
523
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500524struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300525 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500526 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200527 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500528};
529
530static void
Scott Moreau447013d2012-02-18 05:05:29 -0700531resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500532 uint32_t time, int32_t x, int32_t y)
533{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500534 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500535 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500536 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200537 int32_t from_x, from_y;
538 int32_t to_x, to_y;
539
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300540 if (!resize->base.shsurf)
541 return;
542
543 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200544 device->grab_x, device->grab_y,
545 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300546 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200547 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500548
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200549 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200550 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200551 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200552 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500553 } else {
554 width = resize->width;
555 }
556
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200557 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200558 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200559 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200560 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500561 } else {
562 height = resize->height;
563 }
564
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300565 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400566 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500567}
568
569static void
Scott Moreau447013d2012-02-18 05:05:29 -0700570resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400571 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500572{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300573 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500574 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500575
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500576 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300577 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400578 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500579 free(grab);
580 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500581}
582
Scott Moreau447013d2012-02-18 05:05:29 -0700583static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500584 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500585 resize_grab_motion,
586 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500587};
588
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400589static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500590weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400591 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500592{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500594
Alex Wu4539b082012-03-01 12:57:46 +0800595 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
596 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500597
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200598 if (edges == 0 || edges > 15 ||
599 (edges & 3) == 3 || (edges & 12) == 12)
600 return 0;
601
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500602 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400603 if (!resize)
604 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500605
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300606 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
607
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500608 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200609 resize->width = shsurf->surface->geometry.width;
610 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400611
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300612 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400613 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500614
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400615 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400616
617 return 0;
618}
619
620static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200621shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400622 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200623 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400624{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500625 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200626 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400627
Alex Wu4539b082012-03-01 12:57:46 +0800628 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
629 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400630
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500631 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400632 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500633 wd->input_device.pointer_focus != &shsurf->surface->surface)
634 return;
635
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400636 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400637 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500638}
639
Juan Zhao96879df2012-02-07 08:45:41 +0800640static struct weston_output *
641get_default_output(struct weston_compositor *compositor)
642{
643 return container_of(compositor->output_list.next,
644 struct weston_output, link);
645}
646
Alex Wu4539b082012-03-01 12:57:46 +0800647static void
648shell_unset_fullscreen(struct shell_surface *shsurf)
649{
650 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800651 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
652 shell_surface_is_top_fullscreen(shsurf)) {
653 weston_output_switch_mode(shsurf->fullscreen_output,
654 shsurf->fullscreen_output->origin);
655 }
Alex Wu4539b082012-03-01 12:57:46 +0800656 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
657 shsurf->fullscreen.framerate = 0;
658 wl_list_remove(&shsurf->fullscreen.transform.link);
659 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800660 if (shsurf->fullscreen.black_surface)
661 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800662 shsurf->fullscreen.black_surface = NULL;
663 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300664 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800665 weston_surface_set_position(shsurf->surface,
666 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800667 if (shsurf->saved_rotation_valid) {
668 wl_list_insert(&shsurf->surface->geometry.transformation_list,
669 &shsurf->rotation.transform.link);
670 shsurf->saved_rotation_valid = false;
671 }
Alex Wu4539b082012-03-01 12:57:46 +0800672}
673
Pekka Paalanen98262232011-12-01 10:42:22 +0200674static int
675reset_shell_surface_type(struct shell_surface *surface)
676{
677 switch (surface->type) {
678 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800679 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200680 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800681 case SHELL_SURFACE_MAXIMIZED:
682 surface->output = get_default_output(surface->surface->compositor);
683 weston_surface_set_position(surface->surface,
684 surface->saved_x,
685 surface->saved_y);
686 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200687 case SHELL_SURFACE_PANEL:
688 case SHELL_SURFACE_BACKGROUND:
689 wl_list_remove(&surface->link);
690 wl_list_init(&surface->link);
691 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200692 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200693 case SHELL_SURFACE_LOCK:
694 wl_resource_post_error(&surface->resource,
695 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200696 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200697 return -1;
698 case SHELL_SURFACE_NONE:
699 case SHELL_SURFACE_TOPLEVEL:
700 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500701 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200702 break;
703 }
704
705 surface->type = SHELL_SURFACE_NONE;
706 return 0;
707}
708
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500709static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300710set_toplevel(struct shell_surface *shsurf)
711{
712 if (reset_shell_surface_type(shsurf))
713 return;
714
715 shsurf->type = SHELL_SURFACE_TOPLEVEL;
716}
717
718static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200719shell_surface_set_toplevel(struct wl_client *client,
720 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400721
722{
Pekka Paalanen98262232011-12-01 10:42:22 +0200723 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400724
Tiago Vignattibc052c92012-04-19 16:18:18 +0300725 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400726}
727
728static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200729shell_surface_set_transient(struct wl_client *client,
730 struct wl_resource *resource,
731 struct wl_resource *parent_resource,
732 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400733{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200734 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500735 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200736 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500737 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400738
Pekka Paalanen98262232011-12-01 10:42:22 +0200739 if (reset_shell_surface_type(shsurf))
740 return;
741
Alex Wu4539b082012-03-01 12:57:46 +0800742 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800743 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200744 weston_surface_set_position(es, pes->geometry.x + x,
745 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400746
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200747 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400748}
749
Tiago Vignattibe143262012-04-16 17:31:41 +0300750static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800751shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200752{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400753 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800754}
755
756static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300757get_output_panel_height(struct desktop_shell *shell,
758 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800759{
760 struct shell_surface *priv;
761 int panel_height = 0;
762
763 if (!output)
764 return 0;
765
Tiago Vignattibe143262012-04-16 17:31:41 +0300766 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800767 if (priv->output == output) {
768 panel_height = priv->surface->geometry.height;
769 break;
770 }
771 }
772 return panel_height;
773}
774
775static void
776shell_surface_set_maximized(struct wl_client *client,
777 struct wl_resource *resource,
778 struct wl_resource *output_resource )
779{
780 struct shell_surface *shsurf = resource->data;
781 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300782 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800783 uint32_t edges = 0, panel_height = 0;
784
785 /* get the default output, if the client set it as NULL
786 check whether the ouput is available */
787 if (output_resource)
788 shsurf->output = output_resource->data;
789 else
790 shsurf->output = get_default_output(es->compositor);
791
792 if (reset_shell_surface_type(shsurf))
793 return;
794
795 shsurf->saved_x = es->geometry.x;
796 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800797 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800798
Tiago Vignattibe143262012-04-16 17:31:41 +0300799 shell = shell_surface_get_shell(shsurf);
800 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800801 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500802
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400803 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500804 es->output->current->width,
805 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800806
807 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200808}
809
Alex Wu21858432012-04-01 20:13:08 +0800810static void
811black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
812
Alex Wu4539b082012-03-01 12:57:46 +0800813static struct weston_surface *
814create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800815 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800816 GLfloat x, GLfloat y, int w, int h)
817{
818 struct weston_surface *surface = NULL;
819
820 surface = weston_surface_create(ec);
821 if (surface == NULL) {
822 fprintf(stderr, "no memory\n");
823 return NULL;
824 }
825
Alex Wu21858432012-04-01 20:13:08 +0800826 surface->configure = black_surface_configure;
827 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800828 weston_surface_configure(surface, x, y, w, h);
829 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
830 return surface;
831}
832
833/* Create black surface and append it to the associated fullscreen surface.
834 * Handle size dismatch and positioning according to the method. */
835static void
836shell_configure_fullscreen(struct shell_surface *shsurf)
837{
838 struct weston_output *output = shsurf->fullscreen_output;
839 struct weston_surface *surface = shsurf->surface;
840 struct weston_matrix *matrix;
841 float scale;
842
843 center_on_output(surface, output);
844
845 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500846 shsurf->fullscreen.black_surface =
847 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800848 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500849 output->x, output->y,
850 output->current->width,
851 output->current->height);
852
853 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
854 wl_list_insert(&surface->layer_link,
855 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800856 shsurf->fullscreen.black_surface->output = output;
857
858 switch (shsurf->fullscreen.type) {
859 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
860 break;
861 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
862 matrix = &shsurf->fullscreen.transform.matrix;
863 weston_matrix_init(matrix);
864 scale = (float)output->current->width/(float)surface->geometry.width;
865 weston_matrix_scale(matrix, scale, scale, 1);
866 wl_list_remove(&shsurf->fullscreen.transform.link);
867 wl_list_insert(surface->geometry.transformation_list.prev,
868 &shsurf->fullscreen.transform.link);
869 weston_surface_set_position(surface, output->x, output->y);
870 break;
871 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800872 if (shell_surface_is_top_fullscreen(shsurf)) {
873 struct weston_mode mode = {0,
874 surface->geometry.width,
875 surface->geometry.height,
876 shsurf->fullscreen.framerate};
877
878 if (weston_output_switch_mode(output, &mode) == 0) {
879 weston_surface_configure(shsurf->fullscreen.black_surface,
880 output->x, output->y,
881 output->current->width,
882 output->current->height);
883 weston_surface_set_position(surface, output->x, output->y);
884 break;
885 }
886 }
Alex Wu4539b082012-03-01 12:57:46 +0800887 break;
888 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
889 break;
890 default:
891 break;
892 }
893}
894
895/* make the fullscreen and black surface at the top */
896static void
897shell_stack_fullscreen(struct shell_surface *shsurf)
898{
Alex Wubd3354b2012-04-17 17:20:49 +0800899 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800900 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300901 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800902
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500903 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500904 wl_list_insert(&shell->fullscreen_layer.surface_list,
905 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800906 weston_surface_damage(surface);
907
908 if (!shsurf->fullscreen.black_surface)
909 shsurf->fullscreen.black_surface =
910 create_black_surface(surface->compositor,
911 surface,
912 output->x, output->y,
913 output->current->width,
914 output->current->height);
915
916 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500917 wl_list_insert(&surface->layer_link,
918 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500919 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800920}
921
922static void
923shell_map_fullscreen(struct shell_surface *shsurf)
924{
Alex Wu4539b082012-03-01 12:57:46 +0800925 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800926 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800927}
928
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400929static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200930shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500931 struct wl_resource *resource,
932 uint32_t method,
933 uint32_t framerate,
934 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400935{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200936 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500937 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800938
939 if (output_resource)
940 shsurf->output = output_resource->data;
941 else
942 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400943
Pekka Paalanen98262232011-12-01 10:42:22 +0200944 if (reset_shell_surface_type(shsurf))
945 return;
946
Alex Wu4539b082012-03-01 12:57:46 +0800947 shsurf->fullscreen_output = shsurf->output;
948 shsurf->fullscreen.type = method;
949 shsurf->fullscreen.framerate = framerate;
950 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400951
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200952 shsurf->saved_x = es->geometry.x;
953 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800954 shsurf->saved_position_valid = true;
955
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300956 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300957 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500958
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800959 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
960 wl_list_remove(&shsurf->rotation.transform.link);
961 wl_list_init(&shsurf->rotation.transform.link);
962 shsurf->surface->geometry.dirty = 1;
963 shsurf->saved_rotation_valid = true;
964 }
965
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400966 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500967 shsurf->output->current->width,
968 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400969}
970
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500971static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400972popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500973 struct wl_surface *surface, int32_t x, int32_t y)
974{
975 struct wl_input_device *device = grab->input_device;
976 struct shell_surface *priv =
977 container_of(grab, struct shell_surface, popup.grab);
978 struct wl_client *client = priv->surface->surface.resource.client;
979
Pekka Paalanencb108432012-01-19 16:25:40 +0200980 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400981 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500982 grab->focus = surface;
983 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400984 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500985 grab->focus = NULL;
986 }
987}
988
989static void
Scott Moreau447013d2012-02-18 05:05:29 -0700990popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200991 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500992{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500993 struct wl_resource *resource;
994
995 resource = grab->input_device->pointer_focus_resource;
996 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500997 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500998}
999
1000static void
Scott Moreau447013d2012-02-18 05:05:29 -07001001popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001002 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001003{
1004 struct wl_resource *resource;
1005 struct shell_surface *shsurf =
1006 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001007 struct wl_display *display;
1008 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001009
1010 resource = grab->input_device->pointer_focus_resource;
1011 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001012 display = wl_client_get_display(resource->client);
1013 serial = wl_display_get_serial(display);
1014 wl_input_device_send_button(resource, serial,
1015 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001016 } else if (state == 0 &&
1017 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001018 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001019 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001020 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001021 shsurf->popup.grab.input_device = NULL;
1022 }
1023
1024 if (state == 0)
1025 shsurf->popup.initial_up = 1;
1026}
1027
Scott Moreau447013d2012-02-18 05:05:29 -07001028static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001029 popup_grab_focus,
1030 popup_grab_motion,
1031 popup_grab_button,
1032};
1033
1034static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001035shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001036{
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001037 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001038 struct weston_surface *es = shsurf->surface;
1039 struct weston_surface *parent = shsurf->parent->surface;
1040
1041 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001042 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001043
Pekka Paalanen938269a2012-02-07 14:19:01 +02001044 weston_surface_update_transform(parent);
1045 if (parent->transform.enabled) {
1046 shsurf->popup.parent_transform.matrix =
1047 parent->transform.matrix;
1048 } else {
1049 /* construct x, y translation matrix */
1050 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1051 shsurf->popup.parent_transform.matrix.d[12] =
1052 parent->geometry.x;
1053 shsurf->popup.parent_transform.matrix.d[13] =
1054 parent->geometry.y;
1055 }
1056 wl_list_insert(es->geometry.transformation_list.prev,
1057 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001058 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001059
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001060 shsurf->popup.initial_up = 0;
1061
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001062 /* We don't require the grab to still be active, but if another
1063 * grab has started in the meantime, we end the popup now. */
1064 if (device->grab_serial == shsurf->popup.serial) {
1065 wl_input_device_start_pointer_grab(device,
1066 &shsurf->popup.grab);
1067 } else {
1068 wl_shell_surface_send_popup_done(&shsurf->resource);
1069 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001070}
1071
1072static void
1073shell_surface_set_popup(struct wl_client *client,
1074 struct wl_resource *resource,
1075 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001076 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001077 struct wl_resource *parent_resource,
1078 int32_t x, int32_t y, uint32_t flags)
1079{
1080 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001081
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001082 shsurf->type = SHELL_SURFACE_POPUP;
1083 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001084 shsurf->popup.device = input_device_resource->data;
1085 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001086 shsurf->popup.x = x;
1087 shsurf->popup.y = y;
1088}
1089
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001090static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001091 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001092 shell_surface_move,
1093 shell_surface_resize,
1094 shell_surface_set_toplevel,
1095 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001096 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001097 shell_surface_set_popup,
1098 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001099};
1100
1101static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001102destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001103{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001104 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001105 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001106
Alex Wubd3354b2012-04-17 17:20:49 +08001107 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1108 shell_surface_is_top_fullscreen(shsurf)) {
1109 weston_output_switch_mode(shsurf->fullscreen_output,
1110 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001111 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001112
Alex Wuaa08e2d2012-03-05 11:01:40 +08001113 if (shsurf->fullscreen.black_surface)
1114 weston_surface_destroy(shsurf->fullscreen.black_surface);
1115
Alex Wubd3354b2012-04-17 17:20:49 +08001116 /* As destroy_resource() use wl_list_for_each_safe(),
1117 * we can always remove the listener.
1118 */
1119 wl_list_remove(&shsurf->surface_destroy_listener.link);
1120 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001121 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001122
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001123 wl_list_remove(&shsurf->link);
1124 free(shsurf);
1125}
1126
1127static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001128shell_destroy_shell_surface(struct wl_resource *resource)
1129{
1130 struct shell_surface *shsurf = resource->data;
1131
1132 destroy_shell_surface(shsurf);
1133}
1134
1135static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001136shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001137{
1138 struct shell_surface *shsurf = container_of(listener,
1139 struct shell_surface,
1140 surface_destroy_listener);
1141
Tiago Vignattibc052c92012-04-19 16:18:18 +03001142 /* tricky way to check if resource was in fact created */
1143 if (shsurf->resource.object.implementation != 0)
1144 wl_resource_destroy(&shsurf->resource);
1145 else
1146 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001147}
1148
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001149static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001150get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001151{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001152 struct wl_listener *listener;
1153
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001154 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1155 shell_handle_surface_destroy);
1156 if (listener)
1157 return container_of(listener, struct shell_surface,
1158 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001159
1160 return NULL;
1161}
1162
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001164shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1165
1166static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001167create_shell_surface(void *shell, struct weston_surface *surface,
1168 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001169{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001170 struct shell_surface *shsurf;
1171
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001172 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001173 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001174 return;
1175 }
1176
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177 shsurf = calloc(1, sizeof *shsurf);
1178 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001179 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001180 return;
1181 }
1182
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001183 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001184 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001185
Tiago Vignattibc052c92012-04-19 16:18:18 +03001186 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001187 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001188 shsurf->unresponsive_animation.exists = 0;
1189 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001190 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001191 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001192 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001193 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001194 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1195 shsurf->fullscreen.framerate = 0;
1196 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001197 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001198 wl_list_init(&shsurf->fullscreen.transform.link);
1199
Tiago Vignattibc052c92012-04-19 16:18:18 +03001200 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001201 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1202 wl_signal_add(&surface->surface.resource.destroy_signal,
1203 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001204
1205 /* init link so its safe to always remove it in destroy_shell_surface */
1206 wl_list_init(&shsurf->link);
1207
Pekka Paalanen460099f2012-01-20 16:48:25 +02001208 /* empty when not in use */
1209 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001210 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001211
Pekka Paalanen98262232011-12-01 10:42:22 +02001212 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001213
Tiago Vignattibc052c92012-04-19 16:18:18 +03001214 *ret = shsurf;
1215}
1216
1217static void
1218shell_get_shell_surface(struct wl_client *client,
1219 struct wl_resource *resource,
1220 uint32_t id,
1221 struct wl_resource *surface_resource)
1222{
1223 struct weston_surface *surface = surface_resource->data;
1224 struct desktop_shell *shell = resource->data;
1225 struct shell_surface *shsurf;
1226
1227 if (get_shell_surface(surface)) {
1228 wl_resource_post_error(surface_resource,
1229 WL_DISPLAY_ERROR_INVALID_OBJECT,
1230 "desktop_shell::get_shell_surface already requested");
1231 return;
1232 }
1233
1234 create_shell_surface(shell, surface, &shsurf);
1235 if (!shsurf) {
1236 wl_resource_post_error(surface_resource,
1237 WL_DISPLAY_ERROR_INVALID_OBJECT,
1238 "surface->configure already set");
1239 return;
1240 }
1241
1242 shsurf->resource.destroy = shell_destroy_shell_surface;
1243 shsurf->resource.object.id = id;
1244 shsurf->resource.object.interface = &wl_shell_surface_interface;
1245 shsurf->resource.object.implementation =
1246 (void (**)(void)) &shell_surface_implementation;
1247 shsurf->resource.data = shsurf;
1248
1249 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001250}
1251
1252static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001253 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001254};
1255
Kristian Høgsberg07937562011-04-12 17:25:42 -04001256static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001257handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001258{
1259 proc->pid = 0;
1260}
1261
1262static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001263launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001264{
1265 if (shell->screensaver.binding)
1266 return;
1267
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001268 if (!shell->screensaver.path)
1269 return;
1270
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001271 if (shell->screensaver.process.pid != 0) {
1272 fprintf(stderr, "old screensaver still running\n");
1273 return;
1274 }
1275
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001276 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001277 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001278 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001279 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001280}
1281
1282static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001283terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001284{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001285 if (shell->screensaver.process.pid == 0)
1286 return;
1287
1288 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001289}
1290
1291static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001292show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001293{
1294 struct wl_list *list;
1295
1296 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001297 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001298 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001299 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001300
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001301 wl_list_remove(&surface->surface->layer_link);
1302 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001303 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001304 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001305}
1306
1307static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001308hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001309{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001310 wl_list_remove(&surface->surface->layer_link);
1311 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001312 surface->surface->output = NULL;
1313}
1314
1315static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001316desktop_shell_set_background(struct wl_client *client,
1317 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001318 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001319 struct wl_resource *surface_resource)
1320{
Tiago Vignattibe143262012-04-16 17:31:41 +03001321 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001322 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001323 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001324 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001325
Pekka Paalanen98262232011-12-01 10:42:22 +02001326 if (reset_shell_surface_type(shsurf))
1327 return;
1328
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001329 wl_list_for_each(priv, &shell->backgrounds, link) {
1330 if (priv->output == output_resource->data) {
1331 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001332 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001333 wl_list_remove(&priv->link);
1334 break;
1335 }
1336 }
1337
Pekka Paalanen068ae942011-11-28 14:11:15 +02001338 shsurf->type = SHELL_SURFACE_BACKGROUND;
1339 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001340
Pekka Paalanen068ae942011-11-28 14:11:15 +02001341 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001342
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001343 weston_surface_set_position(surface, shsurf->output->x,
1344 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001345
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001346 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001347 surface_resource,
1348 shsurf->output->current->width,
1349 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001350}
1351
1352static void
1353desktop_shell_set_panel(struct wl_client *client,
1354 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001355 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001356 struct wl_resource *surface_resource)
1357{
Tiago Vignattibe143262012-04-16 17:31:41 +03001358 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001359 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001360 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001361 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001362
Pekka Paalanen98262232011-12-01 10:42:22 +02001363 if (reset_shell_surface_type(shsurf))
1364 return;
1365
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001366 wl_list_for_each(priv, &shell->panels, link) {
1367 if (priv->output == output_resource->data) {
1368 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001369 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001370 wl_list_remove(&priv->link);
1371 break;
1372 }
1373 }
1374
Pekka Paalanen068ae942011-11-28 14:11:15 +02001375 shsurf->type = SHELL_SURFACE_PANEL;
1376 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001377
Pekka Paalanen068ae942011-11-28 14:11:15 +02001378 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001379
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001380 weston_surface_set_position(surface, shsurf->output->x,
1381 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001382
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001383 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001384 surface_resource,
1385 shsurf->output->current->width,
1386 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001387}
1388
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001389static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001390handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001391{
Tiago Vignattibe143262012-04-16 17:31:41 +03001392 struct desktop_shell *shell =
1393 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001394
1395 fprintf(stderr, "lock surface gone\n");
1396 shell->lock_surface = NULL;
1397}
1398
1399static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001400desktop_shell_set_lock_surface(struct wl_client *client,
1401 struct wl_resource *resource,
1402 struct wl_resource *surface_resource)
1403{
Tiago Vignattibe143262012-04-16 17:31:41 +03001404 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001405 struct shell_surface *surface = surface_resource->data;
1406
1407 if (reset_shell_surface_type(surface))
1408 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001409
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001410 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001411
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001412 if (!shell->locked)
1413 return;
1414
Pekka Paalanen98262232011-12-01 10:42:22 +02001415 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001416
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001417 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1418 wl_signal_add(&surface_resource->destroy_signal,
1419 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001420
Pekka Paalanen068ae942011-11-28 14:11:15 +02001421 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001422}
1423
1424static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001425resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001426{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001427 struct shell_surface *tmp;
1428
1429 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1430 hide_screensaver(shell, tmp);
1431
1432 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001433
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001434 wl_list_remove(&shell->lock_layer.link);
1435 wl_list_insert(&shell->compositor->cursor_layer.link,
1436 &shell->fullscreen_layer.link);
1437 wl_list_insert(&shell->fullscreen_layer.link,
1438 &shell->panel_layer.link);
1439 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001440
1441 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001442 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001443 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001444 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001445}
1446
1447static void
1448desktop_shell_unlock(struct wl_client *client,
1449 struct wl_resource *resource)
1450{
Tiago Vignattibe143262012-04-16 17:31:41 +03001451 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001452
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001453 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001454
1455 if (shell->locked)
1456 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001457}
1458
Kristian Høgsberg75840622011-09-06 13:48:16 -04001459static const struct desktop_shell_interface desktop_shell_implementation = {
1460 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001461 desktop_shell_set_panel,
1462 desktop_shell_set_lock_surface,
1463 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001464};
1465
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001466static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001467get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001468{
1469 struct shell_surface *shsurf;
1470
1471 shsurf = get_shell_surface(surface);
1472 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001473 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001474 return shsurf->type;
1475}
1476
Kristian Høgsberg75840622011-09-06 13:48:16 -04001477static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001478move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001479 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001480{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001481 struct weston_surface *surface =
1482 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001483
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001484 if (surface == NULL)
1485 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001486
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001487 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001488 case SHELL_SURFACE_PANEL:
1489 case SHELL_SURFACE_BACKGROUND:
1490 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001491 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001492 return;
1493 default:
1494 break;
1495 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001496
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001497 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001498}
1499
1500static void
1501resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001502 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 struct weston_surface *surface =
1505 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001506 uint32_t edges = 0;
1507 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001508 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001509
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001510 if (surface == NULL)
1511 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001512
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001513 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001514 if (!shsurf)
1515 return;
1516
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001517 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001518 case SHELL_SURFACE_PANEL:
1519 case SHELL_SURFACE_BACKGROUND:
1520 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001521 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001522 return;
1523 default:
1524 break;
1525 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001526
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001527 weston_surface_from_global(surface,
1528 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001529
Pekka Paalanen60921e52012-01-25 15:55:43 +02001530 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001531 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001532 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001533 edges |= 0;
1534 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001535 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001536
Pekka Paalanen60921e52012-01-25 15:55:43 +02001537 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001538 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001539 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001540 edges |= 0;
1541 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001542 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001543
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001544 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001545 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001546}
1547
1548static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001549surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1550 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1551{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001552 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001553 struct shell_surface *shsurf;
1554 struct weston_surface *surface =
1555 (struct weston_surface *) device->pointer_focus;
1556
1557 if (surface == NULL)
1558 return;
1559
1560 shsurf = get_shell_surface(surface);
1561 if (!shsurf)
1562 return;
1563
1564 switch (shsurf->type) {
1565 case SHELL_SURFACE_BACKGROUND:
1566 case SHELL_SURFACE_SCREENSAVER:
1567 return;
1568 default:
1569 break;
1570 }
1571
1572 surface->alpha += value * step;
1573
1574 if (surface->alpha > 255)
1575 surface->alpha = 255;
1576 if (surface->alpha < step)
1577 surface->alpha = step;
1578
1579 surface->geometry.dirty = 1;
1580 weston_surface_damage(surface);
1581}
1582
1583static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001584zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001585 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001586{
1587 struct weston_input_device *wd = (struct weston_input_device *) device;
1588 struct weston_compositor *compositor = wd->compositor;
1589 struct weston_output *output;
1590
1591 wl_list_for_each(output, &compositor->output_list, link) {
1592 if (pixman_region32_contains_point(&output->region,
1593 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001594 output->zoom.active = 1;
1595 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001596
1597 if (output->zoom.level >= 1.0) {
1598 output->zoom.active = 0;
1599 output->zoom.level = 1.0;
1600 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001601
1602 if (output->zoom.level < output->zoom.increment)
1603 output->zoom.level = output->zoom.increment;
1604
1605 weston_output_update_zoom(output, device->x, device->y);
1606 }
1607 }
1608}
1609
Scott Moreauccbf29d2012-02-22 14:21:41 -07001610static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001611terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001612 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001613{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001614 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001615
1616 if (state)
1617 wl_display_terminate(compositor->wl_display);
1618}
1619
1620static void
Scott Moreau447013d2012-02-18 05:05:29 -07001621rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001622 uint32_t time, int32_t x, int32_t y)
1623{
1624 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001625 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001626 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001627 struct shell_surface *shsurf = rotate->base.shsurf;
1628 struct weston_surface *surface;
1629 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1630
1631 if (!shsurf)
1632 return;
1633
1634 surface = shsurf->surface;
1635
1636 cx = 0.5f * surface->geometry.width;
1637 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001638
1639 dx = device->x - rotate->center.x;
1640 dy = device->y - rotate->center.y;
1641 r = sqrtf(dx * dx + dy * dy);
1642
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001643 wl_list_remove(&shsurf->rotation.transform.link);
1644 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001645
1646 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001647 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001648 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001649
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001650 weston_matrix_init(&rotate->rotation);
1651 rotate->rotation.d[0] = dx / r;
1652 rotate->rotation.d[4] = -dy / r;
1653 rotate->rotation.d[1] = -rotate->rotation.d[4];
1654 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001655
1656 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001657 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001658 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001659 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001660 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001661
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001662 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001663 &shsurf->surface->geometry.transformation_list,
1664 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001665 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001666 wl_list_init(&shsurf->rotation.transform.link);
1667 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001668 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001669 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001670
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001671 /* We need to adjust the position of the surface
1672 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001673 cposx = surface->geometry.x + cx;
1674 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001675 dposx = rotate->center.x - cposx;
1676 dposy = rotate->center.y - cposy;
1677 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001678 weston_surface_set_position(surface,
1679 surface->geometry.x + dposx,
1680 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001681 }
1682
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001683 /* Repaint implies weston_surface_update_transform(), which
1684 * lazily applies the damage due to rotation update.
1685 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001686 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001687}
1688
1689static void
Scott Moreau447013d2012-02-18 05:05:29 -07001690rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001691 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001692{
1693 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001694 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001695 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001696 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001697
1698 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001699 if (shsurf)
1700 weston_matrix_multiply(&shsurf->rotation.rotation,
1701 &rotate->rotation);
1702 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001703 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001704 free(rotate);
1705 }
1706}
1707
Scott Moreau447013d2012-02-18 05:05:29 -07001708static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001709 noop_grab_focus,
1710 rotate_grab_motion,
1711 rotate_grab_button,
1712};
1713
1714static void
1715rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001716 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001717{
1718 struct weston_surface *base_surface =
1719 (struct weston_surface *) device->pointer_focus;
1720 struct shell_surface *surface;
1721 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001722 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001723 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001724
1725 if (base_surface == NULL)
1726 return;
1727
1728 surface = get_shell_surface(base_surface);
1729 if (!surface)
1730 return;
1731
1732 switch (surface->type) {
1733 case SHELL_SURFACE_PANEL:
1734 case SHELL_SURFACE_BACKGROUND:
1735 case SHELL_SURFACE_FULLSCREEN:
1736 case SHELL_SURFACE_SCREENSAVER:
1737 return;
1738 default:
1739 break;
1740 }
1741
Pekka Paalanen460099f2012-01-20 16:48:25 +02001742 rotate = malloc(sizeof *rotate);
1743 if (!rotate)
1744 return;
1745
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001746 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001747
1748 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001749 surface->surface->geometry.width / 2,
1750 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001751 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001752
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001753 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001754
1755 dx = device->x - rotate->center.x;
1756 dy = device->y - rotate->center.y;
1757 r = sqrtf(dx * dx + dy * dy);
1758 if (r > 20.0f) {
1759 struct weston_matrix inverse;
1760
1761 weston_matrix_init(&inverse);
1762 inverse.d[0] = dx / r;
1763 inverse.d[4] = dy / r;
1764 inverse.d[1] = -inverse.d[4];
1765 inverse.d[5] = inverse.d[0];
1766 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001767
1768 weston_matrix_init(&rotate->rotation);
1769 rotate->rotation.d[0] = dx / r;
1770 rotate->rotation.d[4] = -dy / r;
1771 rotate->rotation.d[1] = -rotate->rotation.d[4];
1772 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001773 } else {
1774 weston_matrix_init(&surface->rotation.rotation);
1775 weston_matrix_init(&rotate->rotation);
1776 }
1777
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001778 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001779}
1780
1781static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001782activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001783 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001784{
Alex Wu21858432012-04-01 20:13:08 +08001785 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001786
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001787 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001788
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001789 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001790 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001791 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001792 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001793 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001794
Pekka Paalanen77346a62011-11-30 16:26:35 +02001795 case SHELL_SURFACE_SCREENSAVER:
1796 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001797 if (shell->lock_surface)
1798 weston_surface_restack(es,
1799 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001800 break;
Alex Wu4539b082012-03-01 12:57:46 +08001801 case SHELL_SURFACE_FULLSCREEN:
1802 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001803 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001804 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001805 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001806 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001807 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001808 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1809 wl_list_for_each_reverse_safe(surf,
1810 prev,
1811 &shell->fullscreen_layer.surface_list,
1812 layer_link)
1813 weston_surface_restack(surf,
1814 &shell->toplevel_layer.surface_list);
1815 }
1816
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001817 weston_surface_restack(es,
1818 &shell->toplevel_layer.surface_list);
1819 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001820 }
1821}
1822
Alex Wu21858432012-04-01 20:13:08 +08001823/* no-op func for checking black surface */
1824static void
1825black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1826{
1827}
1828
1829static bool
1830is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1831{
1832 if (es->configure == black_surface_configure) {
1833 if (fs_surface)
1834 *fs_surface = (struct weston_surface *)es->private;
1835 return true;
1836 }
1837 return false;
1838}
1839
Kristian Høgsberg75840622011-09-06 13:48:16 -04001840static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001841click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001842 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001843 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001844{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001845 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001846 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001847 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001848 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001849
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001851 if (!focus)
1852 return;
1853
Alex Wu21858432012-04-01 20:13:08 +08001854 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001855 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001856
Alex Wu9c35e6b2012-03-05 14:13:13 +08001857 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001858 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001859}
1860
1861static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001862lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001863{
Tiago Vignattibe143262012-04-16 17:31:41 +03001864 struct desktop_shell *shell =
1865 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001866 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001867 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001868 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001869
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001870 if (shell->locked) {
1871 wl_list_for_each(output, &shell->compositor->output_list, link)
1872 /* TODO: find a way to jump to other DPMS levels */
1873 if (output->set_dpms)
1874 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001875 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001876 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001877
1878 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001879
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001880 /* Hide all surfaces by removing the fullscreen, panel and
1881 * toplevel layers. This way nothing else can show or receive
1882 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001883
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001884 wl_list_remove(&shell->panel_layer.link);
1885 wl_list_remove(&shell->toplevel_layer.link);
1886 wl_list_remove(&shell->fullscreen_layer.link);
1887 wl_list_insert(&shell->compositor->cursor_layer.link,
1888 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001889
Pekka Paalanen77346a62011-11-30 16:26:35 +02001890 launch_screensaver(shell);
1891
1892 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1893 show_screensaver(shell, shsurf);
1894
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001895 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001896 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001897 weston_compositor_wake(shell->compositor);
1898 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001899 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001900
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001901 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001902 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001903
1904 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001905 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1906 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001907 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001908 }
1909
1910 /* TODO: disable bindings that should not work while locked. */
1911
1912 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001913}
1914
1915static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001916unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001917{
Tiago Vignattibe143262012-04-16 17:31:41 +03001918 struct desktop_shell *shell =
1919 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001920
Pekka Paalanend81c2162011-11-16 13:47:34 +02001921 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001922 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001923 return;
1924 }
1925
1926 /* If desktop-shell client has gone away, unlock immediately. */
1927 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001928 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001929 return;
1930 }
1931
1932 if (shell->prepare_event_sent)
1933 return;
1934
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001935 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001936 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001937}
1938
1939static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001940center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001941{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001942 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001943 GLfloat x = (mode->width - surface->geometry.width) / 2;
1944 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001945
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001946 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001947}
1948
1949static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001950map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001951 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001952{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001953 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001954 struct shell_surface *shsurf;
1955 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001956 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001957 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001958
Pekka Paalanen77346a62011-11-30 16:26:35 +02001959 shsurf = get_shell_surface(surface);
1960 if (shsurf)
1961 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001962
Pekka Paalanen60921e52012-01-25 15:55:43 +02001963 surface->geometry.width = width;
1964 surface->geometry.height = height;
1965 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001966
1967 /* initial positioning, see also configure() */
1968 switch (surface_type) {
1969 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001970 weston_surface_set_position(surface, 10 + random() % 400,
1971 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001972 break;
1973 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001974 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001975 break;
Alex Wu4539b082012-03-01 12:57:46 +08001976 case SHELL_SURFACE_FULLSCREEN:
1977 shell_map_fullscreen(shsurf);
1978 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001979 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001980 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001981 panel_height = get_output_panel_height(shell,surface->output);
1982 weston_surface_set_position(surface, surface->output->x,
1983 surface->output->y + panel_height);
1984 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001985 case SHELL_SURFACE_LOCK:
1986 center_on_output(surface, get_default_output(compositor));
1987 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001988 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001989 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001990 case SHELL_SURFACE_NONE:
1991 weston_surface_set_position(surface,
1992 surface->geometry.x + sx,
1993 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001994 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001995 default:
1996 ;
1997 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001998
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001999 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002000 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002001 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002002 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002003 wl_list_insert(&shell->background_layer.surface_list,
2004 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002005 break;
2006 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002007 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002008 wl_list_insert(&shell->panel_layer.surface_list,
2009 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002010 break;
2011 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002012 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002013 wl_list_insert(&shell->lock_layer.surface_list,
2014 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002015 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002016 break;
2017 case SHELL_SURFACE_SCREENSAVER:
2018 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002019 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002020 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002021 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002023 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002024 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002025 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002026 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002027 case SHELL_SURFACE_POPUP:
2028 case SHELL_SURFACE_TRANSIENT:
2029 parent = shsurf->parent->surface;
2030 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2031 break;
Alex Wu4539b082012-03-01 12:57:46 +08002032 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002033 case SHELL_SURFACE_NONE:
2034 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002035 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002036 wl_list_insert(&shell->toplevel_layer.surface_list,
2037 &surface->layer_link);
2038 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002039 }
2040
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002041 if (surface_type != SHELL_SURFACE_NONE) {
2042 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002043 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2044 surface->output = shsurf->output;
2045 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002046
Juan Zhao7bb92f02011-12-15 11:31:51 -05002047 switch (surface_type) {
2048 case SHELL_SURFACE_TOPLEVEL:
2049 case SHELL_SURFACE_TRANSIENT:
2050 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002051 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002052 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002053 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002054 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002055 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002056 break;
2057 default:
2058 break;
2059 }
2060
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002061 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002062 {
2063 switch (shell->win_animation_type) {
2064 case ANIMATION_FADE:
2065 weston_fade_run(surface, NULL, NULL);
2066 break;
2067 case ANIMATION_ZOOM:
2068 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2069 break;
2070 default:
2071 break;
2072 }
2073 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002074}
2075
2076static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002077configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002078 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002079{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002080 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2081 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002082
Pekka Paalanen77346a62011-11-30 16:26:35 +02002083 shsurf = get_shell_surface(surface);
2084 if (shsurf)
2085 surface_type = shsurf->type;
2086
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002087 surface->geometry.x = x;
2088 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002089 surface->geometry.width = width;
2090 surface->geometry.height = height;
2091 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002092
2093 switch (surface_type) {
2094 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002095 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002096 break;
Alex Wu4539b082012-03-01 12:57:46 +08002097 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002098 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002099 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002100 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002101 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002102 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002103 surface->geometry.x = surface->output->x;
2104 surface->geometry.y = surface->output->y +
2105 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002106 break;
Alex Wu4539b082012-03-01 12:57:46 +08002107 case SHELL_SURFACE_TOPLEVEL:
2108 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002109 default:
2110 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002111 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002112
Alex Wu4539b082012-03-01 12:57:46 +08002113 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002114 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002115 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002116
2117 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2118 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002119 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2120 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002121 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002122}
2123
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002124static void
2125shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2126{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002127 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002128 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002129
2130 if (!weston_surface_is_mapped(es)) {
2131 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002132 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002133 es->geometry.width != es->buffer->width ||
2134 es->geometry.height != es->buffer->height) {
2135 GLfloat from_x, from_y;
2136 GLfloat to_x, to_y;
2137
2138 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2139 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2140 configure(shell, es,
2141 es->geometry.x + to_x - from_x,
2142 es->geometry.y + to_y - from_y,
2143 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002144 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002145 }
2146}
2147
Tiago Vignattibe143262012-04-16 17:31:41 +03002148static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002149
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002150static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002151desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002152{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002153 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002154 struct desktop_shell *shell =
2155 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002156
2157 shell->child.process.pid = 0;
2158 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002159
2160 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2161 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002162 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002163 shell->child.deathstamp = time;
2164 shell->child.deathcount = 0;
2165 }
2166
2167 shell->child.deathcount++;
2168 if (shell->child.deathcount > 5) {
2169 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2170 return;
2171 }
2172
2173 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2174 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002175}
2176
2177static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002178launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002179{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002180 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002181
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002182 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002183 &shell->child.process,
2184 shell_exe,
2185 desktop_shell_sigchld);
2186
2187 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002188 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002189 return 0;
2190}
2191
2192static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002193bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2194{
Tiago Vignattibe143262012-04-16 17:31:41 +03002195 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002196
2197 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002198 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002199}
2200
Kristian Høgsberg75840622011-09-06 13:48:16 -04002201static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002202unbind_desktop_shell(struct wl_resource *resource)
2203{
Tiago Vignattibe143262012-04-16 17:31:41 +03002204 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002205
2206 if (shell->locked)
2207 resume_desktop(shell);
2208
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002209 shell->child.desktop_shell = NULL;
2210 shell->prepare_event_sent = false;
2211 free(resource);
2212}
2213
2214static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002215bind_desktop_shell(struct wl_client *client,
2216 void *data, uint32_t version, uint32_t id)
2217{
Tiago Vignattibe143262012-04-16 17:31:41 +03002218 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002219 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002220
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002221 resource = wl_client_add_object(client, &desktop_shell_interface,
2222 &desktop_shell_implementation,
2223 id, shell);
2224
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002225 if (client == shell->child.client) {
2226 resource->destroy = unbind_desktop_shell;
2227 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002228 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002229 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002230
2231 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2232 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002233 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002234}
2235
Pekka Paalanen6e168112011-11-24 11:34:05 +02002236static void
2237screensaver_set_surface(struct wl_client *client,
2238 struct wl_resource *resource,
2239 struct wl_resource *shell_surface_resource,
2240 struct wl_resource *output_resource)
2241{
Tiago Vignattibe143262012-04-16 17:31:41 +03002242 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002243 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002244 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002245
Pekka Paalanen98262232011-12-01 10:42:22 +02002246 if (reset_shell_surface_type(surface))
2247 return;
2248
Pekka Paalanen77346a62011-11-30 16:26:35 +02002249 surface->type = SHELL_SURFACE_SCREENSAVER;
2250
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002251 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002252 surface->output = output;
2253 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002254}
2255
2256static const struct screensaver_interface screensaver_implementation = {
2257 screensaver_set_surface
2258};
2259
2260static void
2261unbind_screensaver(struct wl_resource *resource)
2262{
Tiago Vignattibe143262012-04-16 17:31:41 +03002263 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002264
Pekka Paalanen77346a62011-11-30 16:26:35 +02002265 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002266 free(resource);
2267}
2268
2269static void
2270bind_screensaver(struct wl_client *client,
2271 void *data, uint32_t version, uint32_t id)
2272{
Tiago Vignattibe143262012-04-16 17:31:41 +03002273 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002274 struct wl_resource *resource;
2275
2276 resource = wl_client_add_object(client, &screensaver_interface,
2277 &screensaver_implementation,
2278 id, shell);
2279
Pekka Paalanen77346a62011-11-30 16:26:35 +02002280 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002281 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002282 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002283 return;
2284 }
2285
2286 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2287 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002288 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002289}
2290
Kristian Høgsberg07045392012-02-19 18:52:44 -05002291struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002292 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002293 struct weston_surface *current;
2294 struct wl_listener listener;
2295 struct wl_keyboard_grab grab;
2296};
2297
2298static void
2299switcher_next(struct switcher *switcher)
2300{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002301 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002302 struct weston_surface *surface;
2303 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002304 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002305
2306 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002307 switch (get_shell_surface_type(surface)) {
2308 case SHELL_SURFACE_TOPLEVEL:
2309 case SHELL_SURFACE_FULLSCREEN:
2310 case SHELL_SURFACE_MAXIMIZED:
2311 if (first == NULL)
2312 first = surface;
2313 if (prev == switcher->current)
2314 next = surface;
2315 prev = surface;
2316 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002317 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002318 weston_surface_damage(surface);
2319 break;
2320 default:
2321 break;
2322 }
Alex Wu1659daa2012-04-01 20:13:09 +08002323
2324 if (is_black_surface(surface, NULL)) {
2325 surface->alpha = 64;
2326 surface->geometry.dirty = 1;
2327 weston_surface_damage(surface);
2328 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002329 }
2330
2331 if (next == NULL)
2332 next = first;
2333
Alex Wu07b26062012-03-12 16:06:01 +08002334 if (next == NULL)
2335 return;
2336
Kristian Høgsberg07045392012-02-19 18:52:44 -05002337 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002338 wl_signal_add(&next->surface.resource.destroy_signal,
2339 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002340
2341 switcher->current = next;
2342 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002343
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002344 shsurf = get_shell_surface(switcher->current);
2345 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2346 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002347}
2348
2349static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002350switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002351{
2352 struct switcher *switcher =
2353 container_of(listener, struct switcher, listener);
2354
2355 switcher_next(switcher);
2356}
2357
2358static void
2359switcher_destroy(struct switcher *switcher, uint32_t time)
2360{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002361 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002362 struct weston_surface *surface;
2363 struct weston_input_device *device =
2364 (struct weston_input_device *) switcher->grab.input_device;
2365
2366 wl_list_for_each(surface, &compositor->surface_list, link) {
2367 surface->alpha = 255;
2368 weston_surface_damage(surface);
2369 }
2370
Alex Wu07b26062012-03-12 16:06:01 +08002371 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002372 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002373 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002374 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002375 free(switcher);
2376}
2377
2378static void
2379switcher_key(struct wl_keyboard_grab *grab,
2380 uint32_t time, uint32_t key, int32_t state)
2381{
2382 struct switcher *switcher = container_of(grab, struct switcher, grab);
2383 struct weston_input_device *device =
2384 (struct weston_input_device *) grab->input_device;
2385
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002386 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002387 switcher_destroy(switcher, time);
2388 } else if (key == KEY_TAB && state) {
2389 switcher_next(switcher);
2390 }
2391};
2392
2393static const struct wl_keyboard_grab_interface switcher_grab = {
2394 switcher_key
2395};
2396
2397static void
2398switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002399 uint32_t key, uint32_t button, uint32_t axis,
2400 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002401{
Tiago Vignattibe143262012-04-16 17:31:41 +03002402 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002403 struct switcher *switcher;
2404
2405 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002406 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002407 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002408 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002409 wl_list_init(&switcher->listener.link);
2410
2411 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002412 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2413 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002414 switcher_next(switcher);
2415}
2416
Pekka Paalanen3c647232011-12-22 13:43:43 +02002417static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002418backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002419 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002420{
2421 struct weston_compositor *compositor = data;
2422 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002423 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002424
2425 /* TODO: we're limiting to simple use cases, where we assume just
2426 * control on the primary display. We'd have to extend later if we
2427 * ever get support for setting backlights on random desktop LCD
2428 * panels though */
2429 output = get_default_output(compositor);
2430 if (!output)
2431 return;
2432
2433 if (!output->set_backlight)
2434 return;
2435
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002436 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2437 backlight_new = output->backlight_current - 25;
2438 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2439 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002440
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002441 if (backlight_new < 5)
2442 backlight_new = 5;
2443 if (backlight_new > 255)
2444 backlight_new = 255;
2445
2446 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002447 output->set_backlight(output, output->backlight_current);
2448}
2449
2450static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002451debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002452 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002453{
Tiago Vignattibe143262012-04-16 17:31:41 +03002454 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002455 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002456 struct weston_surface *surface;
2457
2458 if (shell->debug_repaint_surface) {
2459 weston_surface_destroy(shell->debug_repaint_surface);
2460 shell->debug_repaint_surface = NULL;
2461 } else {
2462 surface = weston_surface_create(compositor);
2463 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2464 weston_surface_configure(surface, 0, 0, 8192, 8192);
2465 wl_list_insert(&compositor->fade_layer.surface_list,
2466 &surface->layer_link);
2467 weston_surface_assign_output(surface);
2468 pixman_region32_init(&surface->input);
2469
2470 /* Here's the dirty little trick that makes the
2471 * repaint debugging work: we force an
2472 * update_transform first to update dependent state
2473 * and clear the geometry.dirty bit. Then we clear
2474 * the surface damage so it only gets repainted
2475 * piecewise as we repaint other things. */
2476
2477 weston_surface_update_transform(surface);
2478 pixman_region32_fini(&surface->damage);
2479 pixman_region32_init(&surface->damage);
2480 shell->debug_repaint_surface = surface;
2481 }
2482}
2483
2484static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002485shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002486{
Tiago Vignattibe143262012-04-16 17:31:41 +03002487 struct desktop_shell *shell =
2488 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002489
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002490 if (shell->child.client)
2491 wl_client_destroy(shell->child.client);
2492
Pekka Paalanen3c647232011-12-22 13:43:43 +02002493 free(shell->screensaver.path);
2494 free(shell);
2495}
2496
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002497static void
2498shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2499{
2500 uint32_t mod;
2501
2502 /* fixed bindings */
2503 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2504 MODIFIER_CTRL | MODIFIER_ALT,
2505 terminate_binding, ec);
2506 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2507 click_to_activate_binding, shell);
2508 weston_compositor_add_binding(ec, 0, 0,
2509 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2510 MODIFIER_SUPER | MODIFIER_ALT,
2511 surface_opacity_binding, NULL);
2512 weston_compositor_add_binding(ec, 0, 0,
2513 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2514 MODIFIER_SUPER, zoom_binding, NULL);
2515
2516 /* configurable bindings */
2517 mod = shell->binding_modifier;
2518 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2519 move_binding, shell);
2520 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2521 resize_binding, shell);
2522 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2523 rotate_binding, NULL);
2524 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2525 switcher_binding, shell);
2526 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2527 backlight_binding, ec);
2528 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2529 backlight_binding, ec);
2530 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2531 backlight_binding, ec);
2532 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2533 backlight_binding, ec);
2534 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2535 debug_repaint_binding, shell);
2536}
2537
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002538int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002539shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002540
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002541WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002542shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002543{
Tiago Vignattibe143262012-04-16 17:31:41 +03002544 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002545
2546 shell = malloc(sizeof *shell);
2547 if (shell == NULL)
2548 return -1;
2549
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002550 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002551 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002552
2553 shell->destroy_listener.notify = shell_destroy;
2554 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2555 shell->lock_listener.notify = lock;
2556 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2557 shell->unlock_listener.notify = unlock;
2558 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002559 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002560 ec->shell_interface.create_shell_surface = create_shell_surface;
2561 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002562
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002563 wl_list_init(&shell->backgrounds);
2564 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002565 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002566
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002567 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2568 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2569 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2570 weston_layer_init(&shell->background_layer,
2571 &shell->toplevel_layer.link);
2572 wl_list_init(&shell->lock_layer.surface_list);
2573
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002574 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002575
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002576 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2577 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002578 return -1;
2579
Kristian Høgsberg75840622011-09-06 13:48:16 -04002580 if (wl_display_add_global(ec->wl_display,
2581 &desktop_shell_interface,
2582 shell, bind_desktop_shell) == NULL)
2583 return -1;
2584
Pekka Paalanen6e168112011-11-24 11:34:05 +02002585 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2586 shell, bind_screensaver) == NULL)
2587 return -1;
2588
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002589 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002590 if (launch_desktop_shell_process(shell) != 0)
2591 return -1;
2592
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002593 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002594
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002595 return 0;
2596}