blob: e59558c393f21afbe00a57108985e5fea4604e76 [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øgsberg7f366e72012-04-27 17:20:01 -0400118 enum shell_surface_type type, next_type;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500119 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
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400710set_surface_type(struct shell_surface *shsurf)
711{
712 struct weston_surface *surface = shsurf->surface;
713 struct shell_surface *pshsurf = shsurf->parent;
714 struct weston_surface *pes;
715 struct shell_surface *priv;
716 struct desktop_shell *shell = shsurf->shell;
717
718 reset_shell_surface_type(shsurf);
719
720 shsurf->type = shsurf->next_type;
721 shsurf->next_type = SHELL_SURFACE_NONE;
722
723 switch (shsurf->type) {
724 case SHELL_SURFACE_TOPLEVEL:
725 break;
726 case SHELL_SURFACE_TRANSIENT:
727 pes = pshsurf->surface;
728 weston_surface_set_position(surface,
729 pes->geometry.x + shsurf->popup.x,
730 pes->geometry.y + shsurf->popup.y);
731 break;
732
733 case SHELL_SURFACE_MAXIMIZED:
734 shsurf->saved_x = surface->geometry.x;
735 shsurf->saved_y = surface->geometry.y;
736 shsurf->saved_position_valid = true;
737 break;
738
739 case SHELL_SURFACE_FULLSCREEN:
740 shsurf->saved_x = surface->geometry.x;
741 shsurf->saved_y = surface->geometry.y;
742 shsurf->saved_position_valid = true;
743
744 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
745 wl_list_remove(&shsurf->rotation.transform.link);
746 wl_list_init(&shsurf->rotation.transform.link);
747 shsurf->surface->geometry.dirty = 1;
748 shsurf->saved_rotation_valid = true;
749 }
750 break;
751
752 case SHELL_SURFACE_BACKGROUND:
753 wl_list_for_each(priv, &shell->backgrounds, link) {
754 if (priv->output == shsurf->output) {
755 priv->surface->output = NULL;
756 wl_list_remove(&priv->surface->layer_link);
757 wl_list_remove(&priv->link);
758 break;
759 }
760 }
761
762 wl_list_insert(&shell->backgrounds, &shsurf->link);
763
764 weston_surface_set_position(surface, shsurf->output->x,
765 shsurf->output->y);
766 break;
767
768 case SHELL_SURFACE_PANEL:
769 wl_list_for_each(priv, &shell->panels, link) {
770 if (priv->output == shsurf->output) {
771 priv->surface->output = NULL;
772 wl_list_remove(&priv->surface->layer_link);
773 wl_list_remove(&priv->link);
774 break;
775 }
776 }
777
778 wl_list_insert(&shell->panels, &shsurf->link);
779
780 weston_surface_set_position(surface, shsurf->output->x,
781 shsurf->output->y);
782 break;
783
784 default:
785 break;
786 }
787}
788
789static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300790set_toplevel(struct shell_surface *shsurf)
791{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400792 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300793}
794
795static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200796shell_surface_set_toplevel(struct wl_client *client,
797 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400798{
Pekka Paalanen98262232011-12-01 10:42:22 +0200799 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400800
Tiago Vignattibc052c92012-04-19 16:18:18 +0300801 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400802}
803
804static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200805shell_surface_set_transient(struct wl_client *client,
806 struct wl_resource *resource,
807 struct wl_resource *parent_resource,
808 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400809{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200810 struct shell_surface *shsurf = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200811
Alex Wu4539b082012-03-01 12:57:46 +0800812 /* assign to parents output */
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400813 shsurf->parent = parent_resource->data;
814 shsurf->popup.x = x;
815 shsurf->popup.y = y;
816 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400817}
818
Tiago Vignattibe143262012-04-16 17:31:41 +0300819static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800820shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200821{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400822 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800823}
824
825static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300826get_output_panel_height(struct desktop_shell *shell,
827 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800828{
829 struct shell_surface *priv;
830 int panel_height = 0;
831
832 if (!output)
833 return 0;
834
Tiago Vignattibe143262012-04-16 17:31:41 +0300835 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800836 if (priv->output == output) {
837 panel_height = priv->surface->geometry.height;
838 break;
839 }
840 }
841 return panel_height;
842}
843
844static void
845shell_surface_set_maximized(struct wl_client *client,
846 struct wl_resource *resource,
847 struct wl_resource *output_resource )
848{
849 struct shell_surface *shsurf = resource->data;
850 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300851 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800852 uint32_t edges = 0, panel_height = 0;
853
854 /* get the default output, if the client set it as NULL
855 check whether the ouput is available */
856 if (output_resource)
857 shsurf->output = output_resource->data;
858 else
859 shsurf->output = get_default_output(es->compositor);
860
Tiago Vignattibe143262012-04-16 17:31:41 +0300861 shell = shell_surface_get_shell(shsurf);
862 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800863 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500864
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400865 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500866 es->output->current->width,
867 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800868
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400869 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200870}
871
Alex Wu21858432012-04-01 20:13:08 +0800872static void
873black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
874
Alex Wu4539b082012-03-01 12:57:46 +0800875static struct weston_surface *
876create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800877 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800878 GLfloat x, GLfloat y, int w, int h)
879{
880 struct weston_surface *surface = NULL;
881
882 surface = weston_surface_create(ec);
883 if (surface == NULL) {
884 fprintf(stderr, "no memory\n");
885 return NULL;
886 }
887
Alex Wu21858432012-04-01 20:13:08 +0800888 surface->configure = black_surface_configure;
889 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800890 weston_surface_configure(surface, x, y, w, h);
891 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
892 return surface;
893}
894
895/* Create black surface and append it to the associated fullscreen surface.
896 * Handle size dismatch and positioning according to the method. */
897static void
898shell_configure_fullscreen(struct shell_surface *shsurf)
899{
900 struct weston_output *output = shsurf->fullscreen_output;
901 struct weston_surface *surface = shsurf->surface;
902 struct weston_matrix *matrix;
903 float scale;
904
905 center_on_output(surface, output);
906
907 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500908 shsurf->fullscreen.black_surface =
909 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800910 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500911 output->x, output->y,
912 output->current->width,
913 output->current->height);
914
915 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
916 wl_list_insert(&surface->layer_link,
917 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800918 shsurf->fullscreen.black_surface->output = output;
919
920 switch (shsurf->fullscreen.type) {
921 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
922 break;
923 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
924 matrix = &shsurf->fullscreen.transform.matrix;
925 weston_matrix_init(matrix);
926 scale = (float)output->current->width/(float)surface->geometry.width;
927 weston_matrix_scale(matrix, scale, scale, 1);
928 wl_list_remove(&shsurf->fullscreen.transform.link);
929 wl_list_insert(surface->geometry.transformation_list.prev,
930 &shsurf->fullscreen.transform.link);
931 weston_surface_set_position(surface, output->x, output->y);
932 break;
933 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800934 if (shell_surface_is_top_fullscreen(shsurf)) {
935 struct weston_mode mode = {0,
936 surface->geometry.width,
937 surface->geometry.height,
938 shsurf->fullscreen.framerate};
939
940 if (weston_output_switch_mode(output, &mode) == 0) {
941 weston_surface_configure(shsurf->fullscreen.black_surface,
942 output->x, output->y,
943 output->current->width,
944 output->current->height);
945 weston_surface_set_position(surface, output->x, output->y);
946 break;
947 }
948 }
Alex Wu4539b082012-03-01 12:57:46 +0800949 break;
950 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
951 break;
952 default:
953 break;
954 }
955}
956
957/* make the fullscreen and black surface at the top */
958static void
959shell_stack_fullscreen(struct shell_surface *shsurf)
960{
Alex Wubd3354b2012-04-17 17:20:49 +0800961 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800962 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300963 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800964
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500965 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500966 wl_list_insert(&shell->fullscreen_layer.surface_list,
967 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800968 weston_surface_damage(surface);
969
970 if (!shsurf->fullscreen.black_surface)
971 shsurf->fullscreen.black_surface =
972 create_black_surface(surface->compositor,
973 surface,
974 output->x, output->y,
975 output->current->width,
976 output->current->height);
977
978 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500979 wl_list_insert(&surface->layer_link,
980 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500981 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800982}
983
984static void
985shell_map_fullscreen(struct shell_surface *shsurf)
986{
Alex Wu4539b082012-03-01 12:57:46 +0800987 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800988 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800989}
990
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400991static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200992shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500993 struct wl_resource *resource,
994 uint32_t method,
995 uint32_t framerate,
996 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400997{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200998 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500999 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001000
1001 if (output_resource)
1002 shsurf->output = output_resource->data;
1003 else
1004 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001005
Alex Wu4539b082012-03-01 12:57:46 +08001006 shsurf->fullscreen_output = shsurf->output;
1007 shsurf->fullscreen.type = method;
1008 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001009 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001010
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001011 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001012 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -05001013
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001014 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001015 shsurf->output->current->width,
1016 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001017}
1018
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001019static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001020popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001021 struct wl_surface *surface, int32_t x, int32_t y)
1022{
1023 struct wl_input_device *device = grab->input_device;
1024 struct shell_surface *priv =
1025 container_of(grab, struct shell_surface, popup.grab);
1026 struct wl_client *client = priv->surface->surface.resource.client;
1027
Pekka Paalanencb108432012-01-19 16:25:40 +02001028 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001029 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001030 grab->focus = surface;
1031 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001032 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001033 grab->focus = NULL;
1034 }
1035}
1036
1037static void
Scott Moreau447013d2012-02-18 05:05:29 -07001038popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001039 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001040{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001041 struct wl_resource *resource;
1042
1043 resource = grab->input_device->pointer_focus_resource;
1044 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001045 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001046}
1047
1048static void
Scott Moreau447013d2012-02-18 05:05:29 -07001049popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001050 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001051{
1052 struct wl_resource *resource;
1053 struct shell_surface *shsurf =
1054 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001055 struct wl_display *display;
1056 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001057
1058 resource = grab->input_device->pointer_focus_resource;
1059 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001060 display = wl_client_get_display(resource->client);
1061 serial = wl_display_get_serial(display);
1062 wl_input_device_send_button(resource, serial,
1063 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001064 } else if (state == 0 &&
1065 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001066 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001067 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001068 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001069 shsurf->popup.grab.input_device = NULL;
1070 }
1071
1072 if (state == 0)
1073 shsurf->popup.initial_up = 1;
1074}
1075
Scott Moreau447013d2012-02-18 05:05:29 -07001076static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001077 popup_grab_focus,
1078 popup_grab_motion,
1079 popup_grab_button,
1080};
1081
1082static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001083shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001084{
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001085 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001086 struct weston_surface *es = shsurf->surface;
1087 struct weston_surface *parent = shsurf->parent->surface;
1088
1089 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001090 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001091
Pekka Paalanen938269a2012-02-07 14:19:01 +02001092 weston_surface_update_transform(parent);
1093 if (parent->transform.enabled) {
1094 shsurf->popup.parent_transform.matrix =
1095 parent->transform.matrix;
1096 } else {
1097 /* construct x, y translation matrix */
1098 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1099 shsurf->popup.parent_transform.matrix.d[12] =
1100 parent->geometry.x;
1101 shsurf->popup.parent_transform.matrix.d[13] =
1102 parent->geometry.y;
1103 }
1104 wl_list_insert(es->geometry.transformation_list.prev,
1105 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001106 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001107
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001108 shsurf->popup.initial_up = 0;
1109
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001110 /* We don't require the grab to still be active, but if another
1111 * grab has started in the meantime, we end the popup now. */
1112 if (device->grab_serial == shsurf->popup.serial) {
1113 wl_input_device_start_pointer_grab(device,
1114 &shsurf->popup.grab);
1115 } else {
1116 wl_shell_surface_send_popup_done(&shsurf->resource);
1117 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001118}
1119
1120static void
1121shell_surface_set_popup(struct wl_client *client,
1122 struct wl_resource *resource,
1123 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001124 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001125 struct wl_resource *parent_resource,
1126 int32_t x, int32_t y, uint32_t flags)
1127{
1128 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001129
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001130 shsurf->type = SHELL_SURFACE_POPUP;
1131 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001132 shsurf->popup.device = input_device_resource->data;
1133 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001134 shsurf->popup.x = x;
1135 shsurf->popup.y = y;
1136}
1137
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001138static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001139 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001140 shell_surface_move,
1141 shell_surface_resize,
1142 shell_surface_set_toplevel,
1143 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001144 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001145 shell_surface_set_popup,
1146 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001147};
1148
1149static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001150destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001151{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001152 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001153 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001154
Alex Wubd3354b2012-04-17 17:20:49 +08001155 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1156 shell_surface_is_top_fullscreen(shsurf)) {
1157 weston_output_switch_mode(shsurf->fullscreen_output,
1158 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001159 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160
Alex Wuaa08e2d2012-03-05 11:01:40 +08001161 if (shsurf->fullscreen.black_surface)
1162 weston_surface_destroy(shsurf->fullscreen.black_surface);
1163
Alex Wubd3354b2012-04-17 17:20:49 +08001164 /* As destroy_resource() use wl_list_for_each_safe(),
1165 * we can always remove the listener.
1166 */
1167 wl_list_remove(&shsurf->surface_destroy_listener.link);
1168 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001169 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001170
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001171 wl_list_remove(&shsurf->link);
1172 free(shsurf);
1173}
1174
1175static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001176shell_destroy_shell_surface(struct wl_resource *resource)
1177{
1178 struct shell_surface *shsurf = resource->data;
1179
1180 destroy_shell_surface(shsurf);
1181}
1182
1183static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001184shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001185{
1186 struct shell_surface *shsurf = container_of(listener,
1187 struct shell_surface,
1188 surface_destroy_listener);
1189
Tiago Vignattibc052c92012-04-19 16:18:18 +03001190 /* tricky way to check if resource was in fact created */
1191 if (shsurf->resource.object.implementation != 0)
1192 wl_resource_destroy(&shsurf->resource);
1193 else
1194 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001195}
1196
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001197static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001198get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001199{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001200 struct wl_listener *listener;
1201
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001202 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1203 shell_handle_surface_destroy);
1204 if (listener)
1205 return container_of(listener, struct shell_surface,
1206 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001207
1208 return NULL;
1209}
1210
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001211static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001212shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1213
1214static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001215create_shell_surface(void *shell, struct weston_surface *surface,
1216 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001217{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001218 struct shell_surface *shsurf;
1219
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001220 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001221 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001222 return;
1223 }
1224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001225 shsurf = calloc(1, sizeof *shsurf);
1226 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001227 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001228 return;
1229 }
1230
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001231 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001232 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001233
Tiago Vignattibc052c92012-04-19 16:18:18 +03001234 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001235 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001236 shsurf->unresponsive_animation.exists = 0;
1237 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001238 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001239 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001240 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001241 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001242 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1243 shsurf->fullscreen.framerate = 0;
1244 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001245 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001246 wl_list_init(&shsurf->fullscreen.transform.link);
1247
Tiago Vignattibc052c92012-04-19 16:18:18 +03001248 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001249 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1250 wl_signal_add(&surface->surface.resource.destroy_signal,
1251 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001252
1253 /* init link so its safe to always remove it in destroy_shell_surface */
1254 wl_list_init(&shsurf->link);
1255
Pekka Paalanen460099f2012-01-20 16:48:25 +02001256 /* empty when not in use */
1257 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001258 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001259
Pekka Paalanen98262232011-12-01 10:42:22 +02001260 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001261 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001262
Tiago Vignattibc052c92012-04-19 16:18:18 +03001263 *ret = shsurf;
1264}
1265
1266static void
1267shell_get_shell_surface(struct wl_client *client,
1268 struct wl_resource *resource,
1269 uint32_t id,
1270 struct wl_resource *surface_resource)
1271{
1272 struct weston_surface *surface = surface_resource->data;
1273 struct desktop_shell *shell = resource->data;
1274 struct shell_surface *shsurf;
1275
1276 if (get_shell_surface(surface)) {
1277 wl_resource_post_error(surface_resource,
1278 WL_DISPLAY_ERROR_INVALID_OBJECT,
1279 "desktop_shell::get_shell_surface already requested");
1280 return;
1281 }
1282
1283 create_shell_surface(shell, surface, &shsurf);
1284 if (!shsurf) {
1285 wl_resource_post_error(surface_resource,
1286 WL_DISPLAY_ERROR_INVALID_OBJECT,
1287 "surface->configure already set");
1288 return;
1289 }
1290
1291 shsurf->resource.destroy = shell_destroy_shell_surface;
1292 shsurf->resource.object.id = id;
1293 shsurf->resource.object.interface = &wl_shell_surface_interface;
1294 shsurf->resource.object.implementation =
1295 (void (**)(void)) &shell_surface_implementation;
1296 shsurf->resource.data = shsurf;
1297
1298 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001299}
1300
1301static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001302 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001303};
1304
Kristian Høgsberg07937562011-04-12 17:25:42 -04001305static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001306handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001307{
1308 proc->pid = 0;
1309}
1310
1311static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001312launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001313{
1314 if (shell->screensaver.binding)
1315 return;
1316
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001317 if (!shell->screensaver.path)
1318 return;
1319
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001320 if (shell->screensaver.process.pid != 0) {
1321 fprintf(stderr, "old screensaver still running\n");
1322 return;
1323 }
1324
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001325 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001326 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001327 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001328 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001329}
1330
1331static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001332terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001333{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001334 if (shell->screensaver.process.pid == 0)
1335 return;
1336
1337 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001338}
1339
1340static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001341show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001342{
1343 struct wl_list *list;
1344
1345 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001346 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001347 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001348 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001349
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001350 wl_list_remove(&surface->surface->layer_link);
1351 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001352 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001353 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001354}
1355
1356static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001357hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001358{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001359 wl_list_remove(&surface->surface->layer_link);
1360 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001361 surface->surface->output = NULL;
1362}
1363
1364static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001365desktop_shell_set_background(struct wl_client *client,
1366 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001367 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001368 struct wl_resource *surface_resource)
1369{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001370 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001371
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001372 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001373 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001374
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001375 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001376 surface_resource,
1377 shsurf->output->current->width,
1378 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001379}
1380
1381static void
1382desktop_shell_set_panel(struct wl_client *client,
1383 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001384 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001385 struct wl_resource *surface_resource)
1386{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001387 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001388
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001389 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001390 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001391
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001392 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001393 surface_resource,
1394 shsurf->output->current->width,
1395 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001396}
1397
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001398static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001399handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001400{
Tiago Vignattibe143262012-04-16 17:31:41 +03001401 struct desktop_shell *shell =
1402 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001403
1404 fprintf(stderr, "lock surface gone\n");
1405 shell->lock_surface = NULL;
1406}
1407
1408static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001409desktop_shell_set_lock_surface(struct wl_client *client,
1410 struct wl_resource *resource,
1411 struct wl_resource *surface_resource)
1412{
Tiago Vignattibe143262012-04-16 17:31:41 +03001413 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001414 struct shell_surface *surface = surface_resource->data;
1415
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001416 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001417
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001418 if (!shell->locked)
1419 return;
1420
Pekka Paalanen98262232011-12-01 10:42:22 +02001421 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001422
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001423 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1424 wl_signal_add(&surface_resource->destroy_signal,
1425 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001426
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001427 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001428}
1429
1430static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001431resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001432{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001433 struct shell_surface *tmp;
1434
1435 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1436 hide_screensaver(shell, tmp);
1437
1438 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001439
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001440 wl_list_remove(&shell->lock_layer.link);
1441 wl_list_insert(&shell->compositor->cursor_layer.link,
1442 &shell->fullscreen_layer.link);
1443 wl_list_insert(&shell->fullscreen_layer.link,
1444 &shell->panel_layer.link);
1445 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001446
1447 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001448 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001450 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001451}
1452
1453static void
1454desktop_shell_unlock(struct wl_client *client,
1455 struct wl_resource *resource)
1456{
Tiago Vignattibe143262012-04-16 17:31:41 +03001457 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001458
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001459 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001460
1461 if (shell->locked)
1462 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001463}
1464
Kristian Høgsberg75840622011-09-06 13:48:16 -04001465static const struct desktop_shell_interface desktop_shell_implementation = {
1466 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001467 desktop_shell_set_panel,
1468 desktop_shell_set_lock_surface,
1469 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001470};
1471
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001472static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001473get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001474{
1475 struct shell_surface *shsurf;
1476
1477 shsurf = get_shell_surface(surface);
1478 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001479 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001480 return shsurf->type;
1481}
1482
Kristian Høgsberg75840622011-09-06 13:48:16 -04001483static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001484move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001485 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001486{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001487 struct weston_surface *surface =
1488 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001489
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001490 if (surface == NULL)
1491 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001492
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001493 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001494 case SHELL_SURFACE_PANEL:
1495 case SHELL_SURFACE_BACKGROUND:
1496 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001497 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001498 return;
1499 default:
1500 break;
1501 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001502
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001503 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001504}
1505
1506static void
1507resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001508 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 struct weston_surface *surface =
1511 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001512 uint32_t edges = 0;
1513 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001514 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001515
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001516 if (surface == NULL)
1517 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001518
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001519 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001520 if (!shsurf)
1521 return;
1522
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001523 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001524 case SHELL_SURFACE_PANEL:
1525 case SHELL_SURFACE_BACKGROUND:
1526 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001527 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001528 return;
1529 default:
1530 break;
1531 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001532
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001533 weston_surface_from_global(surface,
1534 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001535
Pekka Paalanen60921e52012-01-25 15:55:43 +02001536 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001537 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001538 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001539 edges |= 0;
1540 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001541 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001542
Pekka Paalanen60921e52012-01-25 15:55:43 +02001543 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001544 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001545 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001546 edges |= 0;
1547 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001548 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001549
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001550 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001551 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001552}
1553
1554static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001555surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1556 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1557{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001558 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001559 struct shell_surface *shsurf;
1560 struct weston_surface *surface =
1561 (struct weston_surface *) device->pointer_focus;
1562
1563 if (surface == NULL)
1564 return;
1565
1566 shsurf = get_shell_surface(surface);
1567 if (!shsurf)
1568 return;
1569
1570 switch (shsurf->type) {
1571 case SHELL_SURFACE_BACKGROUND:
1572 case SHELL_SURFACE_SCREENSAVER:
1573 return;
1574 default:
1575 break;
1576 }
1577
1578 surface->alpha += value * step;
1579
1580 if (surface->alpha > 255)
1581 surface->alpha = 255;
1582 if (surface->alpha < step)
1583 surface->alpha = step;
1584
1585 surface->geometry.dirty = 1;
1586 weston_surface_damage(surface);
1587}
1588
1589static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001590zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001591 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001592{
1593 struct weston_input_device *wd = (struct weston_input_device *) device;
1594 struct weston_compositor *compositor = wd->compositor;
1595 struct weston_output *output;
1596
1597 wl_list_for_each(output, &compositor->output_list, link) {
1598 if (pixman_region32_contains_point(&output->region,
1599 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001600 output->zoom.active = 1;
1601 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001602
1603 if (output->zoom.level >= 1.0) {
1604 output->zoom.active = 0;
1605 output->zoom.level = 1.0;
1606 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001607
1608 if (output->zoom.level < output->zoom.increment)
1609 output->zoom.level = output->zoom.increment;
1610
1611 weston_output_update_zoom(output, device->x, device->y);
1612 }
1613 }
1614}
1615
Scott Moreauccbf29d2012-02-22 14:21:41 -07001616static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001617terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001618 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001619{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001620 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001621
1622 if (state)
1623 wl_display_terminate(compositor->wl_display);
1624}
1625
1626static void
Scott Moreau447013d2012-02-18 05:05:29 -07001627rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001628 uint32_t time, int32_t x, int32_t y)
1629{
1630 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001631 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001632 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001633 struct shell_surface *shsurf = rotate->base.shsurf;
1634 struct weston_surface *surface;
1635 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1636
1637 if (!shsurf)
1638 return;
1639
1640 surface = shsurf->surface;
1641
1642 cx = 0.5f * surface->geometry.width;
1643 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001644
1645 dx = device->x - rotate->center.x;
1646 dy = device->y - rotate->center.y;
1647 r = sqrtf(dx * dx + dy * dy);
1648
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001649 wl_list_remove(&shsurf->rotation.transform.link);
1650 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001651
1652 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001653 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001654 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001655
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001656 weston_matrix_init(&rotate->rotation);
1657 rotate->rotation.d[0] = dx / r;
1658 rotate->rotation.d[4] = -dy / r;
1659 rotate->rotation.d[1] = -rotate->rotation.d[4];
1660 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001661
1662 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001663 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001664 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001665 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001666 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001667
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001668 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001669 &shsurf->surface->geometry.transformation_list,
1670 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001671 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001672 wl_list_init(&shsurf->rotation.transform.link);
1673 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001674 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001675 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001676
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001677 /* We need to adjust the position of the surface
1678 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001679 cposx = surface->geometry.x + cx;
1680 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001681 dposx = rotate->center.x - cposx;
1682 dposy = rotate->center.y - cposy;
1683 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001684 weston_surface_set_position(surface,
1685 surface->geometry.x + dposx,
1686 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001687 }
1688
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001689 /* Repaint implies weston_surface_update_transform(), which
1690 * lazily applies the damage due to rotation update.
1691 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001692 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001693}
1694
1695static void
Scott Moreau447013d2012-02-18 05:05:29 -07001696rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001697 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001698{
1699 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001700 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001701 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001702 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001703
1704 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001705 if (shsurf)
1706 weston_matrix_multiply(&shsurf->rotation.rotation,
1707 &rotate->rotation);
1708 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001709 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001710 free(rotate);
1711 }
1712}
1713
Scott Moreau447013d2012-02-18 05:05:29 -07001714static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001715 noop_grab_focus,
1716 rotate_grab_motion,
1717 rotate_grab_button,
1718};
1719
1720static void
1721rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001722 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001723{
1724 struct weston_surface *base_surface =
1725 (struct weston_surface *) device->pointer_focus;
1726 struct shell_surface *surface;
1727 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001728 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001729 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001730
1731 if (base_surface == NULL)
1732 return;
1733
1734 surface = get_shell_surface(base_surface);
1735 if (!surface)
1736 return;
1737
1738 switch (surface->type) {
1739 case SHELL_SURFACE_PANEL:
1740 case SHELL_SURFACE_BACKGROUND:
1741 case SHELL_SURFACE_FULLSCREEN:
1742 case SHELL_SURFACE_SCREENSAVER:
1743 return;
1744 default:
1745 break;
1746 }
1747
Pekka Paalanen460099f2012-01-20 16:48:25 +02001748 rotate = malloc(sizeof *rotate);
1749 if (!rotate)
1750 return;
1751
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001752 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001753
1754 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001755 surface->surface->geometry.width / 2,
1756 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001757 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001758
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001759 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001760
1761 dx = device->x - rotate->center.x;
1762 dy = device->y - rotate->center.y;
1763 r = sqrtf(dx * dx + dy * dy);
1764 if (r > 20.0f) {
1765 struct weston_matrix inverse;
1766
1767 weston_matrix_init(&inverse);
1768 inverse.d[0] = dx / r;
1769 inverse.d[4] = dy / r;
1770 inverse.d[1] = -inverse.d[4];
1771 inverse.d[5] = inverse.d[0];
1772 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001773
1774 weston_matrix_init(&rotate->rotation);
1775 rotate->rotation.d[0] = dx / r;
1776 rotate->rotation.d[4] = -dy / r;
1777 rotate->rotation.d[1] = -rotate->rotation.d[4];
1778 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001779 } else {
1780 weston_matrix_init(&surface->rotation.rotation);
1781 weston_matrix_init(&rotate->rotation);
1782 }
1783
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001784 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001785}
1786
1787static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001788activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001789 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001790{
Alex Wu21858432012-04-01 20:13:08 +08001791 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001792
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001793 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001794
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001795 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001796 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001797 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001798 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001799 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001800
Pekka Paalanen77346a62011-11-30 16:26:35 +02001801 case SHELL_SURFACE_SCREENSAVER:
1802 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001803 if (shell->lock_surface)
1804 weston_surface_restack(es,
1805 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001806 break;
Alex Wu4539b082012-03-01 12:57:46 +08001807 case SHELL_SURFACE_FULLSCREEN:
1808 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001809 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001810 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001811 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001812 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001813 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001814 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1815 wl_list_for_each_reverse_safe(surf,
1816 prev,
1817 &shell->fullscreen_layer.surface_list,
1818 layer_link)
1819 weston_surface_restack(surf,
1820 &shell->toplevel_layer.surface_list);
1821 }
1822
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001823 weston_surface_restack(es,
1824 &shell->toplevel_layer.surface_list);
1825 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001826 }
1827}
1828
Alex Wu21858432012-04-01 20:13:08 +08001829/* no-op func for checking black surface */
1830static void
1831black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1832{
1833}
1834
1835static bool
1836is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1837{
1838 if (es->configure == black_surface_configure) {
1839 if (fs_surface)
1840 *fs_surface = (struct weston_surface *)es->private;
1841 return true;
1842 }
1843 return false;
1844}
1845
Kristian Høgsberg75840622011-09-06 13:48:16 -04001846static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001847click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001848 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001849 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001850{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001851 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001852 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001853 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001854 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001855
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001856 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001857 if (!focus)
1858 return;
1859
Alex Wu21858432012-04-01 20:13:08 +08001860 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001861 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001862
Alex Wu9c35e6b2012-03-05 14:13:13 +08001863 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001864 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001865}
1866
1867static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001868lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001869{
Tiago Vignattibe143262012-04-16 17:31:41 +03001870 struct desktop_shell *shell =
1871 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001872 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001873 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001874 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001875
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001876 if (shell->locked) {
1877 wl_list_for_each(output, &shell->compositor->output_list, link)
1878 /* TODO: find a way to jump to other DPMS levels */
1879 if (output->set_dpms)
1880 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001881 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001882 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001883
1884 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001885
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001886 /* Hide all surfaces by removing the fullscreen, panel and
1887 * toplevel layers. This way nothing else can show or receive
1888 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001889
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001890 wl_list_remove(&shell->panel_layer.link);
1891 wl_list_remove(&shell->toplevel_layer.link);
1892 wl_list_remove(&shell->fullscreen_layer.link);
1893 wl_list_insert(&shell->compositor->cursor_layer.link,
1894 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001895
Pekka Paalanen77346a62011-11-30 16:26:35 +02001896 launch_screensaver(shell);
1897
1898 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1899 show_screensaver(shell, shsurf);
1900
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001901 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001902 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001903 weston_compositor_wake(shell->compositor);
1904 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001905 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001906
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001907 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001908 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001909
1910 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001911 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1912 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001913 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001914 }
1915
1916 /* TODO: disable bindings that should not work while locked. */
1917
1918 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001919}
1920
1921static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001922unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001923{
Tiago Vignattibe143262012-04-16 17:31:41 +03001924 struct desktop_shell *shell =
1925 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001926
Pekka Paalanend81c2162011-11-16 13:47:34 +02001927 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001928 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001929 return;
1930 }
1931
1932 /* If desktop-shell client has gone away, unlock immediately. */
1933 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001934 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001935 return;
1936 }
1937
1938 if (shell->prepare_event_sent)
1939 return;
1940
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001941 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001942 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001943}
1944
1945static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001946center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001947{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001948 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001949 GLfloat x = (mode->width - surface->geometry.width) / 2;
1950 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001951
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001952 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001953}
1954
1955static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001956map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001957 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001958{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001959 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001960 struct shell_surface *shsurf;
1961 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001962 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001963 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001964
Pekka Paalanen77346a62011-11-30 16:26:35 +02001965 shsurf = get_shell_surface(surface);
1966 if (shsurf)
1967 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001968
Pekka Paalanen60921e52012-01-25 15:55:43 +02001969 surface->geometry.width = width;
1970 surface->geometry.height = height;
1971 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001972
1973 /* initial positioning, see also configure() */
1974 switch (surface_type) {
1975 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001976 weston_surface_set_position(surface, 10 + random() % 400,
1977 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001978 break;
1979 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001980 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001981 break;
Alex Wu4539b082012-03-01 12:57:46 +08001982 case SHELL_SURFACE_FULLSCREEN:
1983 shell_map_fullscreen(shsurf);
1984 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001985 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001986 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001987 panel_height = get_output_panel_height(shell,surface->output);
1988 weston_surface_set_position(surface, surface->output->x,
1989 surface->output->y + panel_height);
1990 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001991 case SHELL_SURFACE_LOCK:
1992 center_on_output(surface, get_default_output(compositor));
1993 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001994 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001995 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001996 case SHELL_SURFACE_NONE:
1997 weston_surface_set_position(surface,
1998 surface->geometry.x + sx,
1999 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002000 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002001 default:
2002 ;
2003 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002004
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002005 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002006 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002007 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002008 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002009 wl_list_insert(&shell->background_layer.surface_list,
2010 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002011 break;
2012 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002013 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002014 wl_list_insert(&shell->panel_layer.surface_list,
2015 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002016 break;
2017 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002018 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002019 wl_list_insert(&shell->lock_layer.surface_list,
2020 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002021 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002022 break;
2023 case SHELL_SURFACE_SCREENSAVER:
2024 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002025 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002026 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002027 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002028 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002029 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002031 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002032 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002033 case SHELL_SURFACE_POPUP:
2034 case SHELL_SURFACE_TRANSIENT:
2035 parent = shsurf->parent->surface;
2036 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2037 break;
Alex Wu4539b082012-03-01 12:57:46 +08002038 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002039 case SHELL_SURFACE_NONE:
2040 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002041 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002042 wl_list_insert(&shell->toplevel_layer.surface_list,
2043 &surface->layer_link);
2044 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002045 }
2046
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002047 if (surface_type != SHELL_SURFACE_NONE) {
2048 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002049 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2050 surface->output = shsurf->output;
2051 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002052
Juan Zhao7bb92f02011-12-15 11:31:51 -05002053 switch (surface_type) {
2054 case SHELL_SURFACE_TOPLEVEL:
2055 case SHELL_SURFACE_TRANSIENT:
2056 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002057 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002058 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002059 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002060 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002061 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002062 break;
2063 default:
2064 break;
2065 }
2066
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002067 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002068 {
2069 switch (shell->win_animation_type) {
2070 case ANIMATION_FADE:
2071 weston_fade_run(surface, NULL, NULL);
2072 break;
2073 case ANIMATION_ZOOM:
2074 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2075 break;
2076 default:
2077 break;
2078 }
2079 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002080}
2081
2082static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002083configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002084 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002085{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002086 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2087 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002088
Pekka Paalanen77346a62011-11-30 16:26:35 +02002089 shsurf = get_shell_surface(surface);
2090 if (shsurf)
2091 surface_type = shsurf->type;
2092
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002093 surface->geometry.x = x;
2094 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002095 surface->geometry.width = width;
2096 surface->geometry.height = height;
2097 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002098
2099 switch (surface_type) {
2100 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002101 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002102 break;
Alex Wu4539b082012-03-01 12:57:46 +08002103 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002104 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002105 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002106 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002107 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002108 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002109 surface->geometry.x = surface->output->x;
2110 surface->geometry.y = surface->output->y +
2111 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002112 break;
Alex Wu4539b082012-03-01 12:57:46 +08002113 case SHELL_SURFACE_TOPLEVEL:
2114 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002115 default:
2116 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002117 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002118
Alex Wu4539b082012-03-01 12:57:46 +08002119 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002120 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002121 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002122
2123 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2124 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002125 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2126 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002127 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002128}
2129
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002130static void
2131shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2132{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002133 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002134 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002135
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002136 if (shsurf->next_type != SHELL_SURFACE_NONE &&
2137 shsurf->type != shsurf->next_type)
2138 set_surface_type(shsurf);
2139
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002140 if (!weston_surface_is_mapped(es)) {
2141 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002142 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002143 es->geometry.width != es->buffer->width ||
2144 es->geometry.height != es->buffer->height) {
2145 GLfloat from_x, from_y;
2146 GLfloat to_x, to_y;
2147
2148 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2149 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2150 configure(shell, es,
2151 es->geometry.x + to_x - from_x,
2152 es->geometry.y + to_y - from_y,
2153 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002154 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002155 }
2156}
2157
Tiago Vignattibe143262012-04-16 17:31:41 +03002158static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002159
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002160static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002161desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002162{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002163 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002164 struct desktop_shell *shell =
2165 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002166
2167 shell->child.process.pid = 0;
2168 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002169
2170 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2171 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002172 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002173 shell->child.deathstamp = time;
2174 shell->child.deathcount = 0;
2175 }
2176
2177 shell->child.deathcount++;
2178 if (shell->child.deathcount > 5) {
2179 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2180 return;
2181 }
2182
2183 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2184 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002185}
2186
2187static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002188launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002189{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002190 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002191
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002192 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002193 &shell->child.process,
2194 shell_exe,
2195 desktop_shell_sigchld);
2196
2197 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002198 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002199 return 0;
2200}
2201
2202static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002203bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2204{
Tiago Vignattibe143262012-04-16 17:31:41 +03002205 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002206
2207 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002208 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002209}
2210
Kristian Høgsberg75840622011-09-06 13:48:16 -04002211static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002212unbind_desktop_shell(struct wl_resource *resource)
2213{
Tiago Vignattibe143262012-04-16 17:31:41 +03002214 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002215
2216 if (shell->locked)
2217 resume_desktop(shell);
2218
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002219 shell->child.desktop_shell = NULL;
2220 shell->prepare_event_sent = false;
2221 free(resource);
2222}
2223
2224static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002225bind_desktop_shell(struct wl_client *client,
2226 void *data, uint32_t version, uint32_t id)
2227{
Tiago Vignattibe143262012-04-16 17:31:41 +03002228 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002229 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002230
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002231 resource = wl_client_add_object(client, &desktop_shell_interface,
2232 &desktop_shell_implementation,
2233 id, shell);
2234
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002235 if (client == shell->child.client) {
2236 resource->destroy = unbind_desktop_shell;
2237 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002238 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002239 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002240
2241 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2242 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002243 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002244}
2245
Pekka Paalanen6e168112011-11-24 11:34:05 +02002246static void
2247screensaver_set_surface(struct wl_client *client,
2248 struct wl_resource *resource,
2249 struct wl_resource *shell_surface_resource,
2250 struct wl_resource *output_resource)
2251{
Tiago Vignattibe143262012-04-16 17:31:41 +03002252 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002253 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002254 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002255
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002256 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002257
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002258 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002259 surface->output = output;
2260 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002261}
2262
2263static const struct screensaver_interface screensaver_implementation = {
2264 screensaver_set_surface
2265};
2266
2267static void
2268unbind_screensaver(struct wl_resource *resource)
2269{
Tiago Vignattibe143262012-04-16 17:31:41 +03002270 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002271
Pekka Paalanen77346a62011-11-30 16:26:35 +02002272 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002273 free(resource);
2274}
2275
2276static void
2277bind_screensaver(struct wl_client *client,
2278 void *data, uint32_t version, uint32_t id)
2279{
Tiago Vignattibe143262012-04-16 17:31:41 +03002280 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002281 struct wl_resource *resource;
2282
2283 resource = wl_client_add_object(client, &screensaver_interface,
2284 &screensaver_implementation,
2285 id, shell);
2286
Pekka Paalanen77346a62011-11-30 16:26:35 +02002287 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002288 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002289 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002290 return;
2291 }
2292
2293 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2294 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002295 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002296}
2297
Kristian Høgsberg07045392012-02-19 18:52:44 -05002298struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002299 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002300 struct weston_surface *current;
2301 struct wl_listener listener;
2302 struct wl_keyboard_grab grab;
2303};
2304
2305static void
2306switcher_next(struct switcher *switcher)
2307{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002308 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002309 struct weston_surface *surface;
2310 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002311 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002312
2313 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002314 switch (get_shell_surface_type(surface)) {
2315 case SHELL_SURFACE_TOPLEVEL:
2316 case SHELL_SURFACE_FULLSCREEN:
2317 case SHELL_SURFACE_MAXIMIZED:
2318 if (first == NULL)
2319 first = surface;
2320 if (prev == switcher->current)
2321 next = surface;
2322 prev = surface;
2323 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002324 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002325 weston_surface_damage(surface);
2326 break;
2327 default:
2328 break;
2329 }
Alex Wu1659daa2012-04-01 20:13:09 +08002330
2331 if (is_black_surface(surface, NULL)) {
2332 surface->alpha = 64;
2333 surface->geometry.dirty = 1;
2334 weston_surface_damage(surface);
2335 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002336 }
2337
2338 if (next == NULL)
2339 next = first;
2340
Alex Wu07b26062012-03-12 16:06:01 +08002341 if (next == NULL)
2342 return;
2343
Kristian Høgsberg07045392012-02-19 18:52:44 -05002344 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002345 wl_signal_add(&next->surface.resource.destroy_signal,
2346 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002347
2348 switcher->current = next;
2349 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002350
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002351 shsurf = get_shell_surface(switcher->current);
2352 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2353 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002354}
2355
2356static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002357switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002358{
2359 struct switcher *switcher =
2360 container_of(listener, struct switcher, listener);
2361
2362 switcher_next(switcher);
2363}
2364
2365static void
2366switcher_destroy(struct switcher *switcher, uint32_t time)
2367{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002368 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002369 struct weston_surface *surface;
2370 struct weston_input_device *device =
2371 (struct weston_input_device *) switcher->grab.input_device;
2372
2373 wl_list_for_each(surface, &compositor->surface_list, link) {
2374 surface->alpha = 255;
2375 weston_surface_damage(surface);
2376 }
2377
Alex Wu07b26062012-03-12 16:06:01 +08002378 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002379 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002380 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002381 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002382 free(switcher);
2383}
2384
2385static void
2386switcher_key(struct wl_keyboard_grab *grab,
2387 uint32_t time, uint32_t key, int32_t state)
2388{
2389 struct switcher *switcher = container_of(grab, struct switcher, grab);
2390 struct weston_input_device *device =
2391 (struct weston_input_device *) grab->input_device;
2392
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002393 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002394 switcher_destroy(switcher, time);
2395 } else if (key == KEY_TAB && state) {
2396 switcher_next(switcher);
2397 }
2398};
2399
2400static const struct wl_keyboard_grab_interface switcher_grab = {
2401 switcher_key
2402};
2403
2404static void
2405switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002406 uint32_t key, uint32_t button, uint32_t axis,
2407 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002408{
Tiago Vignattibe143262012-04-16 17:31:41 +03002409 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002410 struct switcher *switcher;
2411
2412 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002413 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002414 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002415 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002416 wl_list_init(&switcher->listener.link);
2417
2418 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002419 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2420 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002421 switcher_next(switcher);
2422}
2423
Pekka Paalanen3c647232011-12-22 13:43:43 +02002424static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002425backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002426 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002427{
2428 struct weston_compositor *compositor = data;
2429 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002430 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002431
2432 /* TODO: we're limiting to simple use cases, where we assume just
2433 * control on the primary display. We'd have to extend later if we
2434 * ever get support for setting backlights on random desktop LCD
2435 * panels though */
2436 output = get_default_output(compositor);
2437 if (!output)
2438 return;
2439
2440 if (!output->set_backlight)
2441 return;
2442
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002443 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2444 backlight_new = output->backlight_current - 25;
2445 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2446 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002447
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002448 if (backlight_new < 5)
2449 backlight_new = 5;
2450 if (backlight_new > 255)
2451 backlight_new = 255;
2452
2453 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002454 output->set_backlight(output, output->backlight_current);
2455}
2456
2457static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002458debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002459 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002460{
Tiago Vignattibe143262012-04-16 17:31:41 +03002461 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002462 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002463 struct weston_surface *surface;
2464
2465 if (shell->debug_repaint_surface) {
2466 weston_surface_destroy(shell->debug_repaint_surface);
2467 shell->debug_repaint_surface = NULL;
2468 } else {
2469 surface = weston_surface_create(compositor);
2470 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2471 weston_surface_configure(surface, 0, 0, 8192, 8192);
2472 wl_list_insert(&compositor->fade_layer.surface_list,
2473 &surface->layer_link);
2474 weston_surface_assign_output(surface);
2475 pixman_region32_init(&surface->input);
2476
2477 /* Here's the dirty little trick that makes the
2478 * repaint debugging work: we force an
2479 * update_transform first to update dependent state
2480 * and clear the geometry.dirty bit. Then we clear
2481 * the surface damage so it only gets repainted
2482 * piecewise as we repaint other things. */
2483
2484 weston_surface_update_transform(surface);
2485 pixman_region32_fini(&surface->damage);
2486 pixman_region32_init(&surface->damage);
2487 shell->debug_repaint_surface = surface;
2488 }
2489}
2490
2491static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002492shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002493{
Tiago Vignattibe143262012-04-16 17:31:41 +03002494 struct desktop_shell *shell =
2495 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002496
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002497 if (shell->child.client)
2498 wl_client_destroy(shell->child.client);
2499
Pekka Paalanen3c647232011-12-22 13:43:43 +02002500 free(shell->screensaver.path);
2501 free(shell);
2502}
2503
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002504static void
2505shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2506{
2507 uint32_t mod;
2508
2509 /* fixed bindings */
2510 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2511 MODIFIER_CTRL | MODIFIER_ALT,
2512 terminate_binding, ec);
2513 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2514 click_to_activate_binding, shell);
2515 weston_compositor_add_binding(ec, 0, 0,
2516 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2517 MODIFIER_SUPER | MODIFIER_ALT,
2518 surface_opacity_binding, NULL);
2519 weston_compositor_add_binding(ec, 0, 0,
2520 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2521 MODIFIER_SUPER, zoom_binding, NULL);
2522
2523 /* configurable bindings */
2524 mod = shell->binding_modifier;
2525 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2526 move_binding, shell);
2527 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2528 resize_binding, shell);
2529 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2530 rotate_binding, NULL);
2531 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2532 switcher_binding, shell);
2533 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2534 backlight_binding, ec);
2535 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2536 backlight_binding, ec);
2537 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2538 backlight_binding, ec);
2539 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2540 backlight_binding, ec);
2541 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2542 debug_repaint_binding, shell);
2543}
2544
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002545int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002546shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002547
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002548WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002549shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002550{
Tiago Vignattibe143262012-04-16 17:31:41 +03002551 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002552
2553 shell = malloc(sizeof *shell);
2554 if (shell == NULL)
2555 return -1;
2556
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002557 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002558 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002559
2560 shell->destroy_listener.notify = shell_destroy;
2561 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2562 shell->lock_listener.notify = lock;
2563 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2564 shell->unlock_listener.notify = unlock;
2565 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002566 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002567 ec->shell_interface.create_shell_surface = create_shell_surface;
2568 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002569
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002570 wl_list_init(&shell->backgrounds);
2571 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002572 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002573
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002574 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2575 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2576 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2577 weston_layer_init(&shell->background_layer,
2578 &shell->toplevel_layer.link);
2579 wl_list_init(&shell->lock_layer.surface_list);
2580
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002581 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002582
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002583 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2584 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002585 return -1;
2586
Kristian Høgsberg75840622011-09-06 13:48:16 -04002587 if (wl_display_add_global(ec->wl_display,
2588 &desktop_shell_interface,
2589 shell, bind_desktop_shell) == NULL)
2590 return -1;
2591
Pekka Paalanen6e168112011-11-24 11:34:05 +02002592 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2593 shell, bind_screensaver) == NULL)
2594 return -1;
2595
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002596 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002597 if (launch_desktop_shell_process(shell) != 0)
2598 return -1;
2599
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002600 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002601
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002602 return 0;
2603}