blob: 5eb45dceed4aaaa43f1c9f45b96b9eb5115eb854 [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øgsberge7afd912012-05-02 09:47:44 -0400119 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500120 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800121 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800122 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600123 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100124
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500125 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500127 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200128 } rotation;
129
130 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700131 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200133 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400135 struct wl_input_device *device;
136 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 } popup;
138
Alex Wu4539b082012-03-01 12:57:46 +0800139 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300140 int32_t x, y;
141 } transient;
142
143 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800144 enum wl_shell_surface_fullscreen_method type;
145 struct weston_transform transform; /* matrix from x, y */
146 uint32_t framerate;
147 struct weston_surface *black_surface;
148 } fullscreen;
149
Scott Moreauff1db4a2012-04-17 19:06:18 -0600150 struct ping_timer *ping_timer;
151
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600152 struct {
153 struct weston_animation current;
154 int exists;
155 int fading_in;
156 uint32_t timestamp;
157 } unresponsive_animation;
158
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500159 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200162};
163
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300164struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700165 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300166 struct shell_surface *shsurf;
167 struct wl_listener shsurf_destroy_listener;
168};
169
170struct weston_move_grab {
171 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172 int32_t dx, dy;
173};
174
Pekka Paalanen460099f2012-01-20 16:48:25 +0200175struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500177 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200178 struct {
179 int32_t x;
180 int32_t y;
181 } center;
182};
183
Alex Wubd3354b2012-04-17 17:20:49 +0800184static struct shell_surface *
185get_shell_surface(struct weston_surface *surface);
186
187static struct desktop_shell *
188shell_surface_get_shell(struct shell_surface *shsurf);
189
190static bool
191shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
192{
193 struct desktop_shell *shell;
194 struct weston_surface *top_fs_es;
195
196 shell = shell_surface_get_shell(shsurf);
197
198 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
199 return false;
200
201 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
202 struct weston_surface,
203 layer_link);
204 return (shsurf == get_shell_surface(top_fs_es));
205}
206
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500207static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400208destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209{
210 struct shell_grab *grab;
211
212 grab = container_of(listener, struct shell_grab,
213 shsurf_destroy_listener);
214
215 grab->shsurf = NULL;
216}
217
218static void
219shell_grab_init(struct shell_grab *grab,
220 const struct wl_pointer_grab_interface *interface,
221 struct shell_surface *shsurf)
222{
223 grab->grab.interface = interface;
224 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400225 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
226 wl_signal_add(&shsurf->resource.destroy_signal,
227 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228
229}
230
231static void
232shell_grab_finish(struct shell_grab *grab)
233{
234 wl_list_remove(&grab->shsurf_destroy_listener.link);
235}
236
237static void
Alex Wu4539b082012-03-01 12:57:46 +0800238center_on_output(struct weston_surface *surface,
239 struct weston_output *output);
240
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300241static uint32_t
242get_modifier(char *modifier)
243{
244 if (!modifier)
245 return MODIFIER_SUPER;
246
247 if (!strcmp("ctrl", modifier))
248 return MODIFIER_CTRL;
249 else if (!strcmp("alt", modifier))
250 return MODIFIER_ALT;
251 else if (!strcmp("super", modifier))
252 return MODIFIER_SUPER;
253 else
254 return MODIFIER_SUPER;
255}
256
Juan Zhaoe10d2792012-04-25 19:09:52 +0800257static enum animation_type
258get_animation_type(char *animation)
259{
260 if (!animation)
261 return ANIMATION_NONE;
262
263 if (!strcmp("zoom", animation))
264 return ANIMATION_ZOOM;
265 else if (!strcmp("fade", animation))
266 return ANIMATION_FADE;
267 else
268 return ANIMATION_NONE;
269}
270
Alex Wu4539b082012-03-01 12:57:46 +0800271static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300272shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200273{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200274 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200275 char *path = NULL;
276 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300277 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800278 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200279
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400280 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300281 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800282 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200283 };
284
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400285 struct config_key saver_keys[] = {
286 { "path", CONFIG_KEY_STRING, &path },
287 { "duration", CONFIG_KEY_INTEGER, &duration },
288 };
289
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200290 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400291 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200292 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
293 };
294
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200295 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500296 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200297 free(config_file);
298
Pekka Paalanen7296e792011-12-07 16:22:00 +0200299 shell->screensaver.path = path;
300 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300301 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800302 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200303}
304
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200305static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400306noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500307 struct wl_surface *surface, int32_t x, int32_t y)
308{
309 grab->focus = NULL;
310}
311
312static void
Scott Moreau447013d2012-02-18 05:05:29 -0700313move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500314 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500317 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318 struct shell_surface *shsurf = move->base.shsurf;
319 struct weston_surface *es;
320
321 if (!shsurf)
322 return;
323
324 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500325
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500326 weston_surface_configure(es,
327 device->x + move->dx,
328 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200329 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500330}
331
332static void
Scott Moreau447013d2012-02-18 05:05:29 -0700333move_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100334 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
337 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500338 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500339
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500340 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400342 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500343 free(grab);
344 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500345}
346
Scott Moreau447013d2012-02-18 05:05:29 -0700347static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500348 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500349 move_grab_motion,
350 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500351};
352
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600353static void
354unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
355{
356 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
357
358 if(!shsurf->unresponsive_animation.exists) {
359 wl_list_insert(&shsurf->surface->compositor->animation_list,
360 &shsurf->unresponsive_animation.current.link);
361 shsurf->unresponsive_animation.exists = 1;
362 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
363 weston_surface_damage(shsurf->surface);
364 }
365}
366
367static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600368unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600369 struct weston_output *output, uint32_t msecs)
370{
371 struct shell_surface *shsurf =
372 container_of(animation, struct shell_surface, unresponsive_animation.current);
373 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600374 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600375
376 if (!surface || !shsurf)
377 return;
378
379 if (shsurf->unresponsive_animation.fading_in) {
380 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
381 if (surface->saturation > 1)
382 surface->saturation -= 5;
383 if (surface->brightness > 200)
384 surface->brightness--;
385
386 shsurf->unresponsive_animation.timestamp += step;
387 }
388
389 if (surface->saturation <= 1 && surface->brightness <= 200) {
390 wl_list_remove(&shsurf->unresponsive_animation.current.link);
391 shsurf->unresponsive_animation.exists = 0;
392 }
393 }
394 else {
395 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
396 if (surface->saturation < 255)
397 surface->saturation += 5;
398 if (surface->brightness < 255)
399 surface->brightness++;
400
401 shsurf->unresponsive_animation.timestamp += step;
402 }
403
404 if (surface->saturation >= 255 && surface->brightness >= 255) {
405 surface->saturation = surface->brightness = 255;
406 wl_list_remove(&shsurf->unresponsive_animation.current.link);
407 shsurf->unresponsive_animation.exists = 0;
408 }
409 }
410
411 surface->geometry.dirty = 1;
412 weston_surface_damage(surface);
413}
414
Scott Moreau9521d5e2012-04-19 13:06:17 -0600415static void
416ping_timer_destroy(struct shell_surface *shsurf)
417{
418 if (!shsurf || !shsurf->ping_timer)
419 return;
420
421 if (shsurf->ping_timer->source)
422 wl_event_source_remove(shsurf->ping_timer->source);
423
424 free(shsurf->ping_timer);
425 shsurf->ping_timer = NULL;
426}
427
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400428static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600429ping_timeout_handler(void *data)
430{
431 struct shell_surface *shsurf = data;
432
Scott Moreau9521d5e2012-04-19 13:06:17 -0600433 /* Client is not responding */
434 shsurf->unresponsive = 1;
435 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600436
437 return 1;
438}
439
440static void
441ping_handler(struct weston_surface *surface, uint32_t serial)
442{
443 struct shell_surface *shsurf;
444 shsurf = get_shell_surface(surface);
445 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600446 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600447
448 if (!shsurf)
449 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400450 if (!shsurf->resource.client)
451 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600452
453 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300454 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600455 if (!shsurf->ping_timer)
456 return;
457
458 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600459 loop = wl_display_get_event_loop(surface->compositor->wl_display);
460 shsurf->ping_timer->source =
461 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
462 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
463
464 wl_shell_surface_send_ping(&shsurf->resource, serial);
465 }
466}
467
468static void
469shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
470 uint32_t serial)
471{
472 struct shell_surface *shsurf = resource->data;
473
Scott Moreauff1db4a2012-04-17 19:06:18 -0600474 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600475 if (shsurf->unresponsive) {
476 /* Received pong from previously unresponsive client */
477 unresponsive_surface_fade(shsurf, true);
478 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600479 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600480 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600481 }
482}
483
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400484static void
485shell_surface_set_title(struct wl_client *client,
486 struct wl_resource *resource, const char *title)
487{
488 struct shell_surface *shsurf = resource->data;
489
490 free(shsurf->title);
491 shsurf->title = strdup(title);
492}
493
494static void
495shell_surface_set_class(struct wl_client *client,
496 struct wl_resource *resource, const char *class)
497{
498 struct shell_surface *shsurf = resource->data;
499
500 free(shsurf->class);
501 shsurf->class = strdup(class);
502}
503
Scott Moreauff1db4a2012-04-17 19:06:18 -0600504static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500505weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400506 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500507{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500508 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300509 struct shell_surface *shsurf = get_shell_surface(es);
510
511 if (!shsurf)
512 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500513
514 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400515 if (!move)
516 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500517
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300518 shell_grab_init(&move->base, &move_grab_interface, shsurf);
519
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200520 move->dx = es->geometry.x - wd->input_device.grab_x;
521 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500522
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300523 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400524 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500525
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400526 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400527
528 return 0;
529}
530
531static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200532shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400533 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400534{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500535 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200536 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400537
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500538 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400539 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500540 wd->input_device.pointer_focus != &shsurf->surface->surface)
541 return;
542
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400543 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400544 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500545}
546
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500547struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300548 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500549 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200550 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500551};
552
553static void
Scott Moreau447013d2012-02-18 05:05:29 -0700554resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555 uint32_t time, int32_t x, int32_t y)
556{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500557 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500558 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500559 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200560 int32_t from_x, from_y;
561 int32_t to_x, to_y;
562
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300563 if (!resize->base.shsurf)
564 return;
565
566 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200567 device->grab_x, device->grab_y,
568 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300569 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200570 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500571
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200572 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200573 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200574 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200575 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500576 } else {
577 width = resize->width;
578 }
579
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200580 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200581 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200582 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200583 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500584 } else {
585 height = resize->height;
586 }
587
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300588 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400589 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500590}
591
592static void
Scott Moreau447013d2012-02-18 05:05:29 -0700593resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100594 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500595{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300596 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500597 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500598
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500599 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300600 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400601 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500602 free(grab);
603 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500604}
605
Scott Moreau447013d2012-02-18 05:05:29 -0700606static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500607 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500608 resize_grab_motion,
609 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500610};
611
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400612static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500613weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400614 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500615{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500616 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500617
Alex Wu4539b082012-03-01 12:57:46 +0800618 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
619 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500620
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200621 if (edges == 0 || edges > 15 ||
622 (edges & 3) == 3 || (edges & 12) == 12)
623 return 0;
624
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500625 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400626 if (!resize)
627 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500628
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300629 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
630
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500631 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200632 resize->width = shsurf->surface->geometry.width;
633 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400634
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300635 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400636 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500637
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400638 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400639
640 return 0;
641}
642
643static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200644shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400645 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200646 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400647{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500648 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200649 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400650
Alex Wu4539b082012-03-01 12:57:46 +0800651 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
652 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400653
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500654 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400655 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500656 wd->input_device.pointer_focus != &shsurf->surface->surface)
657 return;
658
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400659 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400660 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500661}
662
Juan Zhao96879df2012-02-07 08:45:41 +0800663static struct weston_output *
664get_default_output(struct weston_compositor *compositor)
665{
666 return container_of(compositor->output_list.next,
667 struct weston_output, link);
668}
669
Alex Wu4539b082012-03-01 12:57:46 +0800670static void
671shell_unset_fullscreen(struct shell_surface *shsurf)
672{
673 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800674 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
675 shell_surface_is_top_fullscreen(shsurf)) {
676 weston_output_switch_mode(shsurf->fullscreen_output,
677 shsurf->fullscreen_output->origin);
678 }
Alex Wu4539b082012-03-01 12:57:46 +0800679 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
680 shsurf->fullscreen.framerate = 0;
681 wl_list_remove(&shsurf->fullscreen.transform.link);
682 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800683 if (shsurf->fullscreen.black_surface)
684 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800685 shsurf->fullscreen.black_surface = NULL;
686 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800687 weston_surface_set_position(shsurf->surface,
688 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800689 if (shsurf->saved_rotation_valid) {
690 wl_list_insert(&shsurf->surface->geometry.transformation_list,
691 &shsurf->rotation.transform.link);
692 shsurf->saved_rotation_valid = false;
693 }
Alex Wu4539b082012-03-01 12:57:46 +0800694}
695
Pekka Paalanen98262232011-12-01 10:42:22 +0200696static int
697reset_shell_surface_type(struct shell_surface *surface)
698{
699 switch (surface->type) {
700 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800701 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200702 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800703 case SHELL_SURFACE_MAXIMIZED:
704 surface->output = get_default_output(surface->surface->compositor);
705 weston_surface_set_position(surface->surface,
706 surface->saved_x,
707 surface->saved_y);
708 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200709 case SHELL_SURFACE_PANEL:
710 case SHELL_SURFACE_BACKGROUND:
711 wl_list_remove(&surface->link);
712 wl_list_init(&surface->link);
713 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200714 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200715 case SHELL_SURFACE_LOCK:
716 wl_resource_post_error(&surface->resource,
717 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200718 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200719 return -1;
720 case SHELL_SURFACE_NONE:
721 case SHELL_SURFACE_TOPLEVEL:
722 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500723 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200724 break;
725 }
726
727 surface->type = SHELL_SURFACE_NONE;
728 return 0;
729}
730
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500731static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400732set_surface_type(struct shell_surface *shsurf)
733{
734 struct weston_surface *surface = shsurf->surface;
735 struct shell_surface *pshsurf = shsurf->parent;
736 struct weston_surface *pes;
737 struct shell_surface *priv;
738 struct desktop_shell *shell = shsurf->shell;
739
740 reset_shell_surface_type(shsurf);
741
742 shsurf->type = shsurf->next_type;
743 shsurf->next_type = SHELL_SURFACE_NONE;
744
745 switch (shsurf->type) {
746 case SHELL_SURFACE_TOPLEVEL:
747 break;
748 case SHELL_SURFACE_TRANSIENT:
749 pes = pshsurf->surface;
750 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300751 pes->geometry.x + shsurf->transient.x,
752 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400753 break;
754
755 case SHELL_SURFACE_MAXIMIZED:
756 shsurf->saved_x = surface->geometry.x;
757 shsurf->saved_y = surface->geometry.y;
758 shsurf->saved_position_valid = true;
759 break;
760
761 case SHELL_SURFACE_FULLSCREEN:
762 shsurf->saved_x = surface->geometry.x;
763 shsurf->saved_y = surface->geometry.y;
764 shsurf->saved_position_valid = true;
765
766 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
767 wl_list_remove(&shsurf->rotation.transform.link);
768 wl_list_init(&shsurf->rotation.transform.link);
769 shsurf->surface->geometry.dirty = 1;
770 shsurf->saved_rotation_valid = true;
771 }
772 break;
773
774 case SHELL_SURFACE_BACKGROUND:
775 wl_list_for_each(priv, &shell->backgrounds, link) {
776 if (priv->output == shsurf->output) {
777 priv->surface->output = NULL;
778 wl_list_remove(&priv->surface->layer_link);
779 wl_list_remove(&priv->link);
780 break;
781 }
782 }
783
784 wl_list_insert(&shell->backgrounds, &shsurf->link);
785
786 weston_surface_set_position(surface, shsurf->output->x,
787 shsurf->output->y);
788 break;
789
790 case SHELL_SURFACE_PANEL:
791 wl_list_for_each(priv, &shell->panels, link) {
792 if (priv->output == shsurf->output) {
793 priv->surface->output = NULL;
794 wl_list_remove(&priv->surface->layer_link);
795 wl_list_remove(&priv->link);
796 break;
797 }
798 }
799
800 wl_list_insert(&shell->panels, &shsurf->link);
801
802 weston_surface_set_position(surface, shsurf->output->x,
803 shsurf->output->y);
804 break;
805
806 default:
807 break;
808 }
809}
810
811static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300812set_toplevel(struct shell_surface *shsurf)
813{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400814 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300815}
816
817static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200818shell_surface_set_toplevel(struct wl_client *client,
819 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400820{
Pekka Paalanen98262232011-12-01 10:42:22 +0200821 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400822
Tiago Vignattibc052c92012-04-19 16:18:18 +0300823 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400824}
825
826static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200827shell_surface_set_transient(struct wl_client *client,
828 struct wl_resource *resource,
829 struct wl_resource *parent_resource,
830 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400831{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200832 struct shell_surface *shsurf = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200833
Alex Wu4539b082012-03-01 12:57:46 +0800834 /* assign to parents output */
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400835 shsurf->parent = parent_resource->data;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300836 shsurf->transient.x = x;
837 shsurf->transient.y = y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400838 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400839}
840
Tiago Vignattibe143262012-04-16 17:31:41 +0300841static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800842shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200843{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400844 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800845}
846
847static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300848get_output_panel_height(struct desktop_shell *shell,
849 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800850{
851 struct shell_surface *priv;
852 int panel_height = 0;
853
854 if (!output)
855 return 0;
856
Tiago Vignattibe143262012-04-16 17:31:41 +0300857 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800858 if (priv->output == output) {
859 panel_height = priv->surface->geometry.height;
860 break;
861 }
862 }
863 return panel_height;
864}
865
866static void
867shell_surface_set_maximized(struct wl_client *client,
868 struct wl_resource *resource,
869 struct wl_resource *output_resource )
870{
871 struct shell_surface *shsurf = resource->data;
872 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300873 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800874 uint32_t edges = 0, panel_height = 0;
875
876 /* get the default output, if the client set it as NULL
877 check whether the ouput is available */
878 if (output_resource)
879 shsurf->output = output_resource->data;
880 else
881 shsurf->output = get_default_output(es->compositor);
882
Tiago Vignattibe143262012-04-16 17:31:41 +0300883 shell = shell_surface_get_shell(shsurf);
884 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800885 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500886
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400887 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500888 es->output->current->width,
889 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800890
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400891 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200892}
893
Alex Wu21858432012-04-01 20:13:08 +0800894static void
895black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
896
Alex Wu4539b082012-03-01 12:57:46 +0800897static struct weston_surface *
898create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800899 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800900 GLfloat x, GLfloat y, int w, int h)
901{
902 struct weston_surface *surface = NULL;
903
904 surface = weston_surface_create(ec);
905 if (surface == NULL) {
906 fprintf(stderr, "no memory\n");
907 return NULL;
908 }
909
Alex Wu21858432012-04-01 20:13:08 +0800910 surface->configure = black_surface_configure;
911 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800912 weston_surface_configure(surface, x, y, w, h);
913 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
914 return surface;
915}
916
917/* Create black surface and append it to the associated fullscreen surface.
918 * Handle size dismatch and positioning according to the method. */
919static void
920shell_configure_fullscreen(struct shell_surface *shsurf)
921{
922 struct weston_output *output = shsurf->fullscreen_output;
923 struct weston_surface *surface = shsurf->surface;
924 struct weston_matrix *matrix;
925 float scale;
926
927 center_on_output(surface, output);
928
929 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500930 shsurf->fullscreen.black_surface =
931 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800932 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500933 output->x, output->y,
934 output->current->width,
935 output->current->height);
936
937 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
938 wl_list_insert(&surface->layer_link,
939 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800940 shsurf->fullscreen.black_surface->output = output;
941
942 switch (shsurf->fullscreen.type) {
943 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
944 break;
945 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
946 matrix = &shsurf->fullscreen.transform.matrix;
947 weston_matrix_init(matrix);
948 scale = (float)output->current->width/(float)surface->geometry.width;
949 weston_matrix_scale(matrix, scale, scale, 1);
950 wl_list_remove(&shsurf->fullscreen.transform.link);
951 wl_list_insert(surface->geometry.transformation_list.prev,
952 &shsurf->fullscreen.transform.link);
953 weston_surface_set_position(surface, output->x, output->y);
954 break;
955 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800956 if (shell_surface_is_top_fullscreen(shsurf)) {
957 struct weston_mode mode = {0,
958 surface->geometry.width,
959 surface->geometry.height,
960 shsurf->fullscreen.framerate};
961
962 if (weston_output_switch_mode(output, &mode) == 0) {
963 weston_surface_configure(shsurf->fullscreen.black_surface,
964 output->x, output->y,
965 output->current->width,
966 output->current->height);
967 weston_surface_set_position(surface, output->x, output->y);
968 break;
969 }
970 }
Alex Wu4539b082012-03-01 12:57:46 +0800971 break;
972 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
973 break;
974 default:
975 break;
976 }
977}
978
979/* make the fullscreen and black surface at the top */
980static void
981shell_stack_fullscreen(struct shell_surface *shsurf)
982{
Alex Wubd3354b2012-04-17 17:20:49 +0800983 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800984 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300985 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800986
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500987 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500988 wl_list_insert(&shell->fullscreen_layer.surface_list,
989 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800990 weston_surface_damage(surface);
991
992 if (!shsurf->fullscreen.black_surface)
993 shsurf->fullscreen.black_surface =
994 create_black_surface(surface->compositor,
995 surface,
996 output->x, output->y,
997 output->current->width,
998 output->current->height);
999
1000 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001001 wl_list_insert(&surface->layer_link,
1002 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001003 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001004}
1005
1006static void
1007shell_map_fullscreen(struct shell_surface *shsurf)
1008{
Alex Wu4539b082012-03-01 12:57:46 +08001009 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001010 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001011}
1012
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001013static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001014shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001015 struct wl_resource *resource,
1016 uint32_t method,
1017 uint32_t framerate,
1018 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001019{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001020 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001021 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001022
1023 if (output_resource)
1024 shsurf->output = output_resource->data;
1025 else
1026 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001027
Alex Wu4539b082012-03-01 12:57:46 +08001028 shsurf->fullscreen_output = shsurf->output;
1029 shsurf->fullscreen.type = method;
1030 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001031 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001032
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001033 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001034 shsurf->output->current->width,
1035 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001036}
1037
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001038static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001039popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001040 struct wl_surface *surface, int32_t x, int32_t y)
1041{
1042 struct wl_input_device *device = grab->input_device;
1043 struct shell_surface *priv =
1044 container_of(grab, struct shell_surface, popup.grab);
1045 struct wl_client *client = priv->surface->surface.resource.client;
1046
Pekka Paalanencb108432012-01-19 16:25:40 +02001047 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001048 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001049 grab->focus = surface;
1050 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001051 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001052 grab->focus = NULL;
1053 }
1054}
1055
1056static void
Scott Moreau447013d2012-02-18 05:05:29 -07001057popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001058 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001059{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001060 struct wl_resource *resource;
1061
1062 resource = grab->input_device->pointer_focus_resource;
1063 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001064 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001065}
1066
1067static void
Scott Moreau447013d2012-02-18 05:05:29 -07001068popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001069 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001070{
1071 struct wl_resource *resource;
1072 struct shell_surface *shsurf =
1073 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001074 struct wl_display *display;
1075 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001076
1077 resource = grab->input_device->pointer_focus_resource;
1078 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001079 display = wl_client_get_display(resource->client);
1080 serial = wl_display_get_serial(display);
1081 wl_input_device_send_button(resource, serial,
1082 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001083 } else if (state == 0 &&
1084 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001085 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001086 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001087 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001088 shsurf->popup.grab.input_device = NULL;
1089 }
1090
1091 if (state == 0)
1092 shsurf->popup.initial_up = 1;
1093}
1094
Scott Moreau447013d2012-02-18 05:05:29 -07001095static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001096 popup_grab_focus,
1097 popup_grab_motion,
1098 popup_grab_button,
1099};
1100
1101static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001102shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001103{
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001104 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001105 struct weston_surface *es = shsurf->surface;
1106 struct weston_surface *parent = shsurf->parent->surface;
1107
1108 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001109 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001110
Pekka Paalanen938269a2012-02-07 14:19:01 +02001111 weston_surface_update_transform(parent);
1112 if (parent->transform.enabled) {
1113 shsurf->popup.parent_transform.matrix =
1114 parent->transform.matrix;
1115 } else {
1116 /* construct x, y translation matrix */
1117 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1118 shsurf->popup.parent_transform.matrix.d[12] =
1119 parent->geometry.x;
1120 shsurf->popup.parent_transform.matrix.d[13] =
1121 parent->geometry.y;
1122 }
1123 wl_list_insert(es->geometry.transformation_list.prev,
1124 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001125 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001126
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001127 shsurf->popup.initial_up = 0;
1128
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001129 /* We don't require the grab to still be active, but if another
1130 * grab has started in the meantime, we end the popup now. */
1131 if (device->grab_serial == shsurf->popup.serial) {
1132 wl_input_device_start_pointer_grab(device,
1133 &shsurf->popup.grab);
1134 } else {
1135 wl_shell_surface_send_popup_done(&shsurf->resource);
1136 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001137}
1138
1139static void
1140shell_surface_set_popup(struct wl_client *client,
1141 struct wl_resource *resource,
1142 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001143 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001144 struct wl_resource *parent_resource,
1145 int32_t x, int32_t y, uint32_t flags)
1146{
1147 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001148
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001149 shsurf->type = SHELL_SURFACE_POPUP;
1150 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001151 shsurf->popup.device = input_device_resource->data;
1152 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001153 shsurf->popup.x = x;
1154 shsurf->popup.y = y;
1155}
1156
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001157static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001158 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001159 shell_surface_move,
1160 shell_surface_resize,
1161 shell_surface_set_toplevel,
1162 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001163 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001164 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001165 shell_surface_set_maximized,
1166 shell_surface_set_title,
1167 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168};
1169
1170static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001171destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001172{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001173 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001174 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001175
Alex Wubd3354b2012-04-17 17:20:49 +08001176 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1177 shell_surface_is_top_fullscreen(shsurf)) {
1178 weston_output_switch_mode(shsurf->fullscreen_output,
1179 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001180 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001181
Alex Wuaa08e2d2012-03-05 11:01:40 +08001182 if (shsurf->fullscreen.black_surface)
1183 weston_surface_destroy(shsurf->fullscreen.black_surface);
1184
Alex Wubd3354b2012-04-17 17:20:49 +08001185 /* As destroy_resource() use wl_list_for_each_safe(),
1186 * we can always remove the listener.
1187 */
1188 wl_list_remove(&shsurf->surface_destroy_listener.link);
1189 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001190 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001191
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001192 wl_list_remove(&shsurf->link);
1193 free(shsurf);
1194}
1195
1196static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001197shell_destroy_shell_surface(struct wl_resource *resource)
1198{
1199 struct shell_surface *shsurf = resource->data;
1200
1201 destroy_shell_surface(shsurf);
1202}
1203
1204static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001205shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001206{
1207 struct shell_surface *shsurf = container_of(listener,
1208 struct shell_surface,
1209 surface_destroy_listener);
1210
Tiago Vignattibc052c92012-04-19 16:18:18 +03001211 /* tricky way to check if resource was in fact created */
1212 if (shsurf->resource.object.implementation != 0)
1213 wl_resource_destroy(&shsurf->resource);
1214 else
1215 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001216}
1217
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001218static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001219get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001220{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001221 struct wl_listener *listener;
1222
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001223 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1224 shell_handle_surface_destroy);
1225 if (listener)
1226 return container_of(listener, struct shell_surface,
1227 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001228
1229 return NULL;
1230}
1231
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001232static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001233shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1234
1235static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001236create_shell_surface(void *shell, struct weston_surface *surface,
1237 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001238{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001239 struct shell_surface *shsurf;
1240
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001241 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001242 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001243 return;
1244 }
1245
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001246 shsurf = calloc(1, sizeof *shsurf);
1247 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001248 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001249 return;
1250 }
1251
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001252 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001253 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001254
Tiago Vignattibc052c92012-04-19 16:18:18 +03001255 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001256 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001257 shsurf->unresponsive_animation.exists = 0;
1258 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001259 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001260 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001261 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001262 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001263 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1264 shsurf->fullscreen.framerate = 0;
1265 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001266 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001267 wl_list_init(&shsurf->fullscreen.transform.link);
1268
Tiago Vignattibc052c92012-04-19 16:18:18 +03001269 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001270 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1271 wl_signal_add(&surface->surface.resource.destroy_signal,
1272 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001273
1274 /* init link so its safe to always remove it in destroy_shell_surface */
1275 wl_list_init(&shsurf->link);
1276
Pekka Paalanen460099f2012-01-20 16:48:25 +02001277 /* empty when not in use */
1278 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001279 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001280
Pekka Paalanen98262232011-12-01 10:42:22 +02001281 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001282 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001283
Tiago Vignattibc052c92012-04-19 16:18:18 +03001284 *ret = shsurf;
1285}
1286
1287static void
1288shell_get_shell_surface(struct wl_client *client,
1289 struct wl_resource *resource,
1290 uint32_t id,
1291 struct wl_resource *surface_resource)
1292{
1293 struct weston_surface *surface = surface_resource->data;
1294 struct desktop_shell *shell = resource->data;
1295 struct shell_surface *shsurf;
1296
1297 if (get_shell_surface(surface)) {
1298 wl_resource_post_error(surface_resource,
1299 WL_DISPLAY_ERROR_INVALID_OBJECT,
1300 "desktop_shell::get_shell_surface already requested");
1301 return;
1302 }
1303
1304 create_shell_surface(shell, surface, &shsurf);
1305 if (!shsurf) {
1306 wl_resource_post_error(surface_resource,
1307 WL_DISPLAY_ERROR_INVALID_OBJECT,
1308 "surface->configure already set");
1309 return;
1310 }
1311
1312 shsurf->resource.destroy = shell_destroy_shell_surface;
1313 shsurf->resource.object.id = id;
1314 shsurf->resource.object.interface = &wl_shell_surface_interface;
1315 shsurf->resource.object.implementation =
1316 (void (**)(void)) &shell_surface_implementation;
1317 shsurf->resource.data = shsurf;
1318
1319 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001320}
1321
1322static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001323 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001324};
1325
Kristian Høgsberg07937562011-04-12 17:25:42 -04001326static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001327handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001328{
1329 proc->pid = 0;
1330}
1331
1332static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001333launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001334{
1335 if (shell->screensaver.binding)
1336 return;
1337
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001338 if (!shell->screensaver.path)
1339 return;
1340
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001341 if (shell->screensaver.process.pid != 0) {
1342 fprintf(stderr, "old screensaver still running\n");
1343 return;
1344 }
1345
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001346 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001347 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001348 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001349 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001350}
1351
1352static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001353terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001354{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001355 if (shell->screensaver.process.pid == 0)
1356 return;
1357
1358 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001359}
1360
1361static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001362show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001363{
1364 struct wl_list *list;
1365
1366 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001367 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001368 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001369 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001370
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001371 wl_list_remove(&surface->surface->layer_link);
1372 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001373 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001374 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001375}
1376
1377static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001378hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001379{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001380 wl_list_remove(&surface->surface->layer_link);
1381 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001382 surface->surface->output = NULL;
1383}
1384
1385static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001386desktop_shell_set_background(struct wl_client *client,
1387 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001388 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001389 struct wl_resource *surface_resource)
1390{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001391 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001392
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001393 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001394 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001395
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001396 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001397 surface_resource,
1398 shsurf->output->current->width,
1399 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001400}
1401
1402static void
1403desktop_shell_set_panel(struct wl_client *client,
1404 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001405 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001406 struct wl_resource *surface_resource)
1407{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001408 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001409
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001410 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001411 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001412
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001413 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001414 surface_resource,
1415 shsurf->output->current->width,
1416 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001417}
1418
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001419static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001420handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001421{
Tiago Vignattibe143262012-04-16 17:31:41 +03001422 struct desktop_shell *shell =
1423 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001424
1425 fprintf(stderr, "lock surface gone\n");
1426 shell->lock_surface = NULL;
1427}
1428
1429static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001430desktop_shell_set_lock_surface(struct wl_client *client,
1431 struct wl_resource *resource,
1432 struct wl_resource *surface_resource)
1433{
Tiago Vignattibe143262012-04-16 17:31:41 +03001434 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001435 struct shell_surface *surface = surface_resource->data;
1436
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001437 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001438
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001439 if (!shell->locked)
1440 return;
1441
Pekka Paalanen98262232011-12-01 10:42:22 +02001442 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001443
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001444 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1445 wl_signal_add(&surface_resource->destroy_signal,
1446 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001447
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001448 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001449}
1450
1451static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001452resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001453{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001454 struct shell_surface *tmp;
1455
1456 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1457 hide_screensaver(shell, tmp);
1458
1459 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001460
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001461 wl_list_remove(&shell->lock_layer.link);
1462 wl_list_insert(&shell->compositor->cursor_layer.link,
1463 &shell->fullscreen_layer.link);
1464 wl_list_insert(&shell->fullscreen_layer.link,
1465 &shell->panel_layer.link);
1466 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001467
1468 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001469 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001471 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001472}
1473
1474static void
1475desktop_shell_unlock(struct wl_client *client,
1476 struct wl_resource *resource)
1477{
Tiago Vignattibe143262012-04-16 17:31:41 +03001478 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001479
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001480 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001481
1482 if (shell->locked)
1483 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001484}
1485
Kristian Høgsberg75840622011-09-06 13:48:16 -04001486static const struct desktop_shell_interface desktop_shell_implementation = {
1487 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001488 desktop_shell_set_panel,
1489 desktop_shell_set_lock_surface,
1490 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001491};
1492
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001493static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001494get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001495{
1496 struct shell_surface *shsurf;
1497
1498 shsurf = get_shell_surface(surface);
1499 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001500 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001501 return shsurf->type;
1502}
1503
Kristian Høgsberg75840622011-09-06 13:48:16 -04001504static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001505move_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001506 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1507 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509 struct weston_surface *surface =
1510 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001511
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001512 if (surface == NULL)
1513 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001514
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001515 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001516 case SHELL_SURFACE_PANEL:
1517 case SHELL_SURFACE_BACKGROUND:
1518 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001519 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001520 return;
1521 default:
1522 break;
1523 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001524
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001525 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001526}
1527
1528static void
1529resize_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001530 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1531 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001533 struct weston_surface *surface =
1534 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001535 uint32_t edges = 0;
1536 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001537 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001538
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001539 if (surface == NULL)
1540 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001541
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001542 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001543 if (!shsurf)
1544 return;
1545
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001546 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001547 case SHELL_SURFACE_PANEL:
1548 case SHELL_SURFACE_BACKGROUND:
1549 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001550 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001551 return;
1552 default:
1553 break;
1554 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001555
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001556 weston_surface_from_global(surface,
1557 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001558
Pekka Paalanen60921e52012-01-25 15:55:43 +02001559 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001560 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001561 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001562 edges |= 0;
1563 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001564 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001565
Pekka Paalanen60921e52012-01-25 15:55:43 +02001566 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001567 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001568 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001569 edges |= 0;
1570 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001571 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001572
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001574 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001575}
1576
1577static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001578surface_opacity_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001579 uint32_t key, uint32_t button, uint32_t axis,
1580 int32_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001581{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001582 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001583 struct shell_surface *shsurf;
1584 struct weston_surface *surface =
1585 (struct weston_surface *) device->pointer_focus;
1586
1587 if (surface == NULL)
1588 return;
1589
1590 shsurf = get_shell_surface(surface);
1591 if (!shsurf)
1592 return;
1593
1594 switch (shsurf->type) {
1595 case SHELL_SURFACE_BACKGROUND:
1596 case SHELL_SURFACE_SCREENSAVER:
1597 return;
1598 default:
1599 break;
1600 }
1601
1602 surface->alpha += value * step;
1603
1604 if (surface->alpha > 255)
1605 surface->alpha = 255;
1606 if (surface->alpha < step)
1607 surface->alpha = step;
1608
1609 surface->geometry.dirty = 1;
1610 weston_surface_damage(surface);
1611}
1612
1613static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001614zoom_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001615 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1616 void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001617{
1618 struct weston_input_device *wd = (struct weston_input_device *) device;
1619 struct weston_compositor *compositor = wd->compositor;
1620 struct weston_output *output;
1621
1622 wl_list_for_each(output, &compositor->output_list, link) {
1623 if (pixman_region32_contains_point(&output->region,
1624 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001625 output->zoom.active = 1;
1626 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001627
1628 if (output->zoom.level >= 1.0) {
1629 output->zoom.active = 0;
1630 output->zoom.level = 1.0;
1631 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001632
1633 if (output->zoom.level < output->zoom.increment)
1634 output->zoom.level = output->zoom.increment;
1635
1636 weston_output_update_zoom(output, device->x, device->y);
1637 }
1638 }
1639}
1640
Scott Moreauccbf29d2012-02-22 14:21:41 -07001641static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001642terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001643 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001644{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001645 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001646
1647 if (state)
1648 wl_display_terminate(compositor->wl_display);
1649}
1650
1651static void
Scott Moreau447013d2012-02-18 05:05:29 -07001652rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001653 uint32_t time, int32_t x, int32_t y)
1654{
1655 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001656 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001657 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001658 struct shell_surface *shsurf = rotate->base.shsurf;
1659 struct weston_surface *surface;
1660 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1661
1662 if (!shsurf)
1663 return;
1664
1665 surface = shsurf->surface;
1666
1667 cx = 0.5f * surface->geometry.width;
1668 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001669
1670 dx = device->x - rotate->center.x;
1671 dy = device->y - rotate->center.y;
1672 r = sqrtf(dx * dx + dy * dy);
1673
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001674 wl_list_remove(&shsurf->rotation.transform.link);
1675 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001676
1677 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001678 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001679 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001680
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001681 weston_matrix_init(&rotate->rotation);
1682 rotate->rotation.d[0] = dx / r;
1683 rotate->rotation.d[4] = -dy / r;
1684 rotate->rotation.d[1] = -rotate->rotation.d[4];
1685 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001686
1687 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001688 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001689 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001690 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001691 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001692
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001693 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001694 &shsurf->surface->geometry.transformation_list,
1695 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001696 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001697 wl_list_init(&shsurf->rotation.transform.link);
1698 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001699 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001700 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001701
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001702 /* We need to adjust the position of the surface
1703 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001704 cposx = surface->geometry.x + cx;
1705 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001706 dposx = rotate->center.x - cposx;
1707 dposy = rotate->center.y - cposy;
1708 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001709 weston_surface_set_position(surface,
1710 surface->geometry.x + dposx,
1711 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001712 }
1713
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001714 /* Repaint implies weston_surface_update_transform(), which
1715 * lazily applies the damage due to rotation update.
1716 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001717 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001718}
1719
1720static void
Scott Moreau447013d2012-02-18 05:05:29 -07001721rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001722 uint32_t time, uint32_t button, uint32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001723{
1724 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001725 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001726 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001727 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001728
1729 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001730 if (shsurf)
1731 weston_matrix_multiply(&shsurf->rotation.rotation,
1732 &rotate->rotation);
1733 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001734 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001735 free(rotate);
1736 }
1737}
1738
Scott Moreau447013d2012-02-18 05:05:29 -07001739static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001740 noop_grab_focus,
1741 rotate_grab_motion,
1742 rotate_grab_button,
1743};
1744
1745static void
1746rotate_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001747 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1748 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001749{
1750 struct weston_surface *base_surface =
1751 (struct weston_surface *) device->pointer_focus;
1752 struct shell_surface *surface;
1753 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001754 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001755 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001756
1757 if (base_surface == NULL)
1758 return;
1759
1760 surface = get_shell_surface(base_surface);
1761 if (!surface)
1762 return;
1763
1764 switch (surface->type) {
1765 case SHELL_SURFACE_PANEL:
1766 case SHELL_SURFACE_BACKGROUND:
1767 case SHELL_SURFACE_FULLSCREEN:
1768 case SHELL_SURFACE_SCREENSAVER:
1769 return;
1770 default:
1771 break;
1772 }
1773
Pekka Paalanen460099f2012-01-20 16:48:25 +02001774 rotate = malloc(sizeof *rotate);
1775 if (!rotate)
1776 return;
1777
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001778 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001779
1780 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001781 surface->surface->geometry.width / 2,
1782 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001783 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001784
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001785 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001786
1787 dx = device->x - rotate->center.x;
1788 dy = device->y - rotate->center.y;
1789 r = sqrtf(dx * dx + dy * dy);
1790 if (r > 20.0f) {
1791 struct weston_matrix inverse;
1792
1793 weston_matrix_init(&inverse);
1794 inverse.d[0] = dx / r;
1795 inverse.d[4] = dy / r;
1796 inverse.d[1] = -inverse.d[4];
1797 inverse.d[5] = inverse.d[0];
1798 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001799
1800 weston_matrix_init(&rotate->rotation);
1801 rotate->rotation.d[0] = dx / r;
1802 rotate->rotation.d[4] = -dy / r;
1803 rotate->rotation.d[1] = -rotate->rotation.d[4];
1804 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001805 } else {
1806 weston_matrix_init(&surface->rotation.rotation);
1807 weston_matrix_init(&rotate->rotation);
1808 }
1809
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001810 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001811}
1812
1813static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001814activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001815 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001816{
Alex Wu21858432012-04-01 20:13:08 +08001817 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001818
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001819 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001820
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001821 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001822 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001823 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001824 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001825 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001826
Pekka Paalanen77346a62011-11-30 16:26:35 +02001827 case SHELL_SURFACE_SCREENSAVER:
1828 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001829 if (shell->lock_surface)
1830 weston_surface_restack(es,
1831 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001832 break;
Alex Wu4539b082012-03-01 12:57:46 +08001833 case SHELL_SURFACE_FULLSCREEN:
1834 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001835 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001836 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001837 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001838 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001839 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001840 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1841 wl_list_for_each_reverse_safe(surf,
1842 prev,
1843 &shell->fullscreen_layer.surface_list,
1844 layer_link)
1845 weston_surface_restack(surf,
1846 &shell->toplevel_layer.surface_list);
1847 }
1848
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001849 weston_surface_restack(es,
1850 &shell->toplevel_layer.surface_list);
1851 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001852 }
1853}
1854
Alex Wu21858432012-04-01 20:13:08 +08001855/* no-op func for checking black surface */
1856static void
1857black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1858{
1859}
1860
1861static bool
1862is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1863{
1864 if (es->configure == black_surface_configure) {
1865 if (fs_surface)
1866 *fs_surface = (struct weston_surface *)es->private;
1867 return true;
1868 }
1869 return false;
1870}
1871
Kristian Høgsberg75840622011-09-06 13:48:16 -04001872static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001873click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001874 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001875 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001877 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001878 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001879 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001880 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001881
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001882 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001883 if (!focus)
1884 return;
1885
Alex Wu21858432012-04-01 20:13:08 +08001886 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001887 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001888
Alex Wu9c35e6b2012-03-05 14:13:13 +08001889 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001890 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001891}
1892
1893static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001894lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001895{
Tiago Vignattibe143262012-04-16 17:31:41 +03001896 struct desktop_shell *shell =
1897 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001899 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001900 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001901
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001902 if (shell->locked) {
1903 wl_list_for_each(output, &shell->compositor->output_list, link)
1904 /* TODO: find a way to jump to other DPMS levels */
1905 if (output->set_dpms)
1906 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001907 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001908 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001909
1910 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001911
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001912 /* Hide all surfaces by removing the fullscreen, panel and
1913 * toplevel layers. This way nothing else can show or receive
1914 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001915
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001916 wl_list_remove(&shell->panel_layer.link);
1917 wl_list_remove(&shell->toplevel_layer.link);
1918 wl_list_remove(&shell->fullscreen_layer.link);
1919 wl_list_insert(&shell->compositor->cursor_layer.link,
1920 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001921
Pekka Paalanen77346a62011-11-30 16:26:35 +02001922 launch_screensaver(shell);
1923
1924 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1925 show_screensaver(shell, shsurf);
1926
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001927 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001928 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001929 weston_compositor_wake(shell->compositor);
1930 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001931 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001932
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001933 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001934 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001935
1936 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001937 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1938 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001939 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001940 }
1941
1942 /* TODO: disable bindings that should not work while locked. */
1943
1944 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001945}
1946
1947static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001948unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001949{
Tiago Vignattibe143262012-04-16 17:31:41 +03001950 struct desktop_shell *shell =
1951 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001952
Pekka Paalanend81c2162011-11-16 13:47:34 +02001953 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001954 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001955 return;
1956 }
1957
1958 /* If desktop-shell client has gone away, unlock immediately. */
1959 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001960 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001961 return;
1962 }
1963
1964 if (shell->prepare_event_sent)
1965 return;
1966
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001967 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001968 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001969}
1970
1971static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001972center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001973{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001974 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001975 GLfloat x = (mode->width - surface->geometry.width) / 2;
1976 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001977
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001978 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001979}
1980
1981static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001982map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001983 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001984{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001985 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001986 struct shell_surface *shsurf;
1987 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001988 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001989 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001990
Pekka Paalanen77346a62011-11-30 16:26:35 +02001991 shsurf = get_shell_surface(surface);
1992 if (shsurf)
1993 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001994
Pekka Paalanen60921e52012-01-25 15:55:43 +02001995 surface->geometry.width = width;
1996 surface->geometry.height = height;
1997 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001998
1999 /* initial positioning, see also configure() */
2000 switch (surface_type) {
2001 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002002 weston_surface_set_position(surface, 10 + random() % 400,
2003 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002004 break;
2005 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002006 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002007 break;
Alex Wu4539b082012-03-01 12:57:46 +08002008 case SHELL_SURFACE_FULLSCREEN:
2009 shell_map_fullscreen(shsurf);
2010 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002011 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002012 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002013 panel_height = get_output_panel_height(shell,surface->output);
2014 weston_surface_set_position(surface, surface->output->x,
2015 surface->output->y + panel_height);
2016 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002017 case SHELL_SURFACE_LOCK:
2018 center_on_output(surface, get_default_output(compositor));
2019 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002020 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002021 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002022 case SHELL_SURFACE_NONE:
2023 weston_surface_set_position(surface,
2024 surface->geometry.x + sx,
2025 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002026 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002027 default:
2028 ;
2029 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002030
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002031 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002032 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002033 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002034 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002035 wl_list_insert(&shell->background_layer.surface_list,
2036 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002037 break;
2038 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002039 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002040 wl_list_insert(&shell->panel_layer.surface_list,
2041 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002042 break;
2043 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002044 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002045 wl_list_insert(&shell->lock_layer.surface_list,
2046 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002047 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002048 break;
2049 case SHELL_SURFACE_SCREENSAVER:
2050 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002051 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002052 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002053 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002054 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002055 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002056 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002057 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002058 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002059 case SHELL_SURFACE_POPUP:
2060 case SHELL_SURFACE_TRANSIENT:
2061 parent = shsurf->parent->surface;
2062 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2063 break;
Alex Wu4539b082012-03-01 12:57:46 +08002064 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002065 case SHELL_SURFACE_NONE:
2066 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002067 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002068 wl_list_insert(&shell->toplevel_layer.surface_list,
2069 &surface->layer_link);
2070 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002071 }
2072
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002073 if (surface_type != SHELL_SURFACE_NONE) {
2074 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002075 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2076 surface->output = shsurf->output;
2077 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002078
Juan Zhao7bb92f02011-12-15 11:31:51 -05002079 switch (surface_type) {
2080 case SHELL_SURFACE_TOPLEVEL:
2081 case SHELL_SURFACE_TRANSIENT:
2082 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002083 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002084 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002085 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002086 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002087 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002088 break;
2089 default:
2090 break;
2091 }
2092
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002093 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002094 {
2095 switch (shell->win_animation_type) {
2096 case ANIMATION_FADE:
2097 weston_fade_run(surface, NULL, NULL);
2098 break;
2099 case ANIMATION_ZOOM:
2100 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2101 break;
2102 default:
2103 break;
2104 }
2105 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002106}
2107
2108static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002109configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002110 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002111{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002112 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2113 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002114
Pekka Paalanen77346a62011-11-30 16:26:35 +02002115 shsurf = get_shell_surface(surface);
2116 if (shsurf)
2117 surface_type = shsurf->type;
2118
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002119 surface->geometry.x = x;
2120 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002121 surface->geometry.width = width;
2122 surface->geometry.height = height;
2123 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002124
2125 switch (surface_type) {
2126 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002127 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002128 break;
Alex Wu4539b082012-03-01 12:57:46 +08002129 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002130 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002131 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002132 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002133 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002134 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002135 surface->geometry.x = surface->output->x;
2136 surface->geometry.y = surface->output->y +
2137 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002138 break;
Alex Wu4539b082012-03-01 12:57:46 +08002139 case SHELL_SURFACE_TOPLEVEL:
2140 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002141 default:
2142 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002143 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002144
Alex Wu4539b082012-03-01 12:57:46 +08002145 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002146 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002147 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002148
2149 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2150 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002151 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2152 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002153 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002154}
2155
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002156static void
2157shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2158{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002159 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002160 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002161 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002162
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002163 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002164 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002165 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002166 type_changed = 1;
2167 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002168
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002169 if (!weston_surface_is_mapped(es)) {
2170 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002171 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002172 es->geometry.width != es->buffer->width ||
2173 es->geometry.height != es->buffer->height) {
2174 GLfloat from_x, from_y;
2175 GLfloat to_x, to_y;
2176
2177 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2178 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2179 configure(shell, es,
2180 es->geometry.x + to_x - from_x,
2181 es->geometry.y + to_y - from_y,
2182 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002183 }
2184}
2185
Tiago Vignattibe143262012-04-16 17:31:41 +03002186static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002187
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002188static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002189desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002190{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002191 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002192 struct desktop_shell *shell =
2193 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002194
2195 shell->child.process.pid = 0;
2196 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002197
2198 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2199 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002200 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002201 shell->child.deathstamp = time;
2202 shell->child.deathcount = 0;
2203 }
2204
2205 shell->child.deathcount++;
2206 if (shell->child.deathcount > 5) {
2207 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2208 return;
2209 }
2210
2211 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2212 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002213}
2214
2215static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002216launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002217{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002218 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002219
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002220 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002221 &shell->child.process,
2222 shell_exe,
2223 desktop_shell_sigchld);
2224
2225 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002226 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002227 return 0;
2228}
2229
2230static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002231bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2232{
Tiago Vignattibe143262012-04-16 17:31:41 +03002233 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002234
2235 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002236 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002237}
2238
Kristian Høgsberg75840622011-09-06 13:48:16 -04002239static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002240unbind_desktop_shell(struct wl_resource *resource)
2241{
Tiago Vignattibe143262012-04-16 17:31:41 +03002242 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002243
2244 if (shell->locked)
2245 resume_desktop(shell);
2246
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002247 shell->child.desktop_shell = NULL;
2248 shell->prepare_event_sent = false;
2249 free(resource);
2250}
2251
2252static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002253bind_desktop_shell(struct wl_client *client,
2254 void *data, uint32_t version, uint32_t id)
2255{
Tiago Vignattibe143262012-04-16 17:31:41 +03002256 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002257 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002258
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002259 resource = wl_client_add_object(client, &desktop_shell_interface,
2260 &desktop_shell_implementation,
2261 id, shell);
2262
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002263 if (client == shell->child.client) {
2264 resource->destroy = unbind_desktop_shell;
2265 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002266 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002267 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002268
2269 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2270 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002271 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002272}
2273
Pekka Paalanen6e168112011-11-24 11:34:05 +02002274static void
2275screensaver_set_surface(struct wl_client *client,
2276 struct wl_resource *resource,
2277 struct wl_resource *shell_surface_resource,
2278 struct wl_resource *output_resource)
2279{
Tiago Vignattibe143262012-04-16 17:31:41 +03002280 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002281 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002282 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002283
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002284 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002285
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002286 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002287 surface->output = output;
2288 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002289}
2290
2291static const struct screensaver_interface screensaver_implementation = {
2292 screensaver_set_surface
2293};
2294
2295static void
2296unbind_screensaver(struct wl_resource *resource)
2297{
Tiago Vignattibe143262012-04-16 17:31:41 +03002298 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002299
Pekka Paalanen77346a62011-11-30 16:26:35 +02002300 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002301 free(resource);
2302}
2303
2304static void
2305bind_screensaver(struct wl_client *client,
2306 void *data, uint32_t version, uint32_t id)
2307{
Tiago Vignattibe143262012-04-16 17:31:41 +03002308 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002309 struct wl_resource *resource;
2310
2311 resource = wl_client_add_object(client, &screensaver_interface,
2312 &screensaver_implementation,
2313 id, shell);
2314
Pekka Paalanen77346a62011-11-30 16:26:35 +02002315 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002316 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002317 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002318 return;
2319 }
2320
2321 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2322 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002323 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002324}
2325
Kristian Høgsberg07045392012-02-19 18:52:44 -05002326struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002327 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002328 struct weston_surface *current;
2329 struct wl_listener listener;
2330 struct wl_keyboard_grab grab;
2331};
2332
2333static void
2334switcher_next(struct switcher *switcher)
2335{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002336 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002337 struct weston_surface *surface;
2338 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002339 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002340
2341 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002342 switch (get_shell_surface_type(surface)) {
2343 case SHELL_SURFACE_TOPLEVEL:
2344 case SHELL_SURFACE_FULLSCREEN:
2345 case SHELL_SURFACE_MAXIMIZED:
2346 if (first == NULL)
2347 first = surface;
2348 if (prev == switcher->current)
2349 next = surface;
2350 prev = surface;
2351 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002352 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002353 weston_surface_damage(surface);
2354 break;
2355 default:
2356 break;
2357 }
Alex Wu1659daa2012-04-01 20:13:09 +08002358
2359 if (is_black_surface(surface, NULL)) {
2360 surface->alpha = 64;
2361 surface->geometry.dirty = 1;
2362 weston_surface_damage(surface);
2363 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002364 }
2365
2366 if (next == NULL)
2367 next = first;
2368
Alex Wu07b26062012-03-12 16:06:01 +08002369 if (next == NULL)
2370 return;
2371
Kristian Høgsberg07045392012-02-19 18:52:44 -05002372 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002373 wl_signal_add(&next->surface.resource.destroy_signal,
2374 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002375
2376 switcher->current = next;
2377 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002378
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002379 shsurf = get_shell_surface(switcher->current);
2380 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2381 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002382}
2383
2384static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002385switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002386{
2387 struct switcher *switcher =
2388 container_of(listener, struct switcher, listener);
2389
2390 switcher_next(switcher);
2391}
2392
2393static void
2394switcher_destroy(struct switcher *switcher, uint32_t time)
2395{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002396 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002397 struct weston_surface *surface;
2398 struct weston_input_device *device =
2399 (struct weston_input_device *) switcher->grab.input_device;
2400
2401 wl_list_for_each(surface, &compositor->surface_list, link) {
2402 surface->alpha = 255;
2403 weston_surface_damage(surface);
2404 }
2405
Alex Wu07b26062012-03-12 16:06:01 +08002406 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002407 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002408 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002409 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002410 free(switcher);
2411}
2412
2413static void
2414switcher_key(struct wl_keyboard_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01002415 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002416{
2417 struct switcher *switcher = container_of(grab, struct switcher, grab);
2418 struct weston_input_device *device =
2419 (struct weston_input_device *) grab->input_device;
2420
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002421 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002422 switcher_destroy(switcher, time);
2423 } else if (key == KEY_TAB && state) {
2424 switcher_next(switcher);
2425 }
2426};
2427
2428static const struct wl_keyboard_grab_interface switcher_grab = {
2429 switcher_key
2430};
2431
2432static void
2433switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002434 uint32_t key, uint32_t button, uint32_t axis,
Daniel Stonee5a01202012-05-04 11:21:57 +01002435 int32_t value, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002436{
Tiago Vignattibe143262012-04-16 17:31:41 +03002437 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002438 struct switcher *switcher;
2439
2440 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002441 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002442 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002443 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002444 wl_list_init(&switcher->listener.link);
2445
2446 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002447 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2448 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002449 switcher_next(switcher);
2450}
2451
Pekka Paalanen3c647232011-12-22 13:43:43 +02002452static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002453backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002454 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002455{
2456 struct weston_compositor *compositor = data;
2457 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002458 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002459
2460 /* TODO: we're limiting to simple use cases, where we assume just
2461 * control on the primary display. We'd have to extend later if we
2462 * ever get support for setting backlights on random desktop LCD
2463 * panels though */
2464 output = get_default_output(compositor);
2465 if (!output)
2466 return;
2467
2468 if (!output->set_backlight)
2469 return;
2470
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002471 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2472 backlight_new = output->backlight_current - 25;
2473 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2474 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002475
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002476 if (backlight_new < 5)
2477 backlight_new = 5;
2478 if (backlight_new > 255)
2479 backlight_new = 255;
2480
2481 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002482 output->set_backlight(output, output->backlight_current);
2483}
2484
2485static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002486debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01002487 uint32_t key, uint32_t button, uint32_t axis,
2488 int32_t value, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002489{
Tiago Vignattibe143262012-04-16 17:31:41 +03002490 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002491 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002492 struct weston_surface *surface;
2493
2494 if (shell->debug_repaint_surface) {
2495 weston_surface_destroy(shell->debug_repaint_surface);
2496 shell->debug_repaint_surface = NULL;
2497 } else {
2498 surface = weston_surface_create(compositor);
2499 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2500 weston_surface_configure(surface, 0, 0, 8192, 8192);
2501 wl_list_insert(&compositor->fade_layer.surface_list,
2502 &surface->layer_link);
2503 weston_surface_assign_output(surface);
2504 pixman_region32_init(&surface->input);
2505
2506 /* Here's the dirty little trick that makes the
2507 * repaint debugging work: we force an
2508 * update_transform first to update dependent state
2509 * and clear the geometry.dirty bit. Then we clear
2510 * the surface damage so it only gets repainted
2511 * piecewise as we repaint other things. */
2512
2513 weston_surface_update_transform(surface);
2514 pixman_region32_fini(&surface->damage);
2515 pixman_region32_init(&surface->damage);
2516 shell->debug_repaint_surface = surface;
2517 }
2518}
2519
2520static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002521shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002522{
Tiago Vignattibe143262012-04-16 17:31:41 +03002523 struct desktop_shell *shell =
2524 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002525
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002526 if (shell->child.client)
2527 wl_client_destroy(shell->child.client);
2528
Pekka Paalanen3c647232011-12-22 13:43:43 +02002529 free(shell->screensaver.path);
2530 free(shell);
2531}
2532
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002533static void
2534shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2535{
2536 uint32_t mod;
2537
2538 /* fixed bindings */
2539 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2540 MODIFIER_CTRL | MODIFIER_ALT,
2541 terminate_binding, ec);
2542 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2543 click_to_activate_binding, shell);
2544 weston_compositor_add_binding(ec, 0, 0,
2545 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2546 MODIFIER_SUPER | MODIFIER_ALT,
2547 surface_opacity_binding, NULL);
2548 weston_compositor_add_binding(ec, 0, 0,
2549 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2550 MODIFIER_SUPER, zoom_binding, NULL);
2551
2552 /* configurable bindings */
2553 mod = shell->binding_modifier;
2554 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2555 move_binding, shell);
2556 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2557 resize_binding, shell);
2558 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2559 rotate_binding, NULL);
2560 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2561 switcher_binding, shell);
2562 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2563 backlight_binding, ec);
2564 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2565 backlight_binding, ec);
2566 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2567 backlight_binding, ec);
2568 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2569 backlight_binding, ec);
2570 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2571 debug_repaint_binding, shell);
2572}
2573
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002574int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002575shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002576
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002577WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002578shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002579{
Tiago Vignattibe143262012-04-16 17:31:41 +03002580 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002581
2582 shell = malloc(sizeof *shell);
2583 if (shell == NULL)
2584 return -1;
2585
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002586 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002587 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002588
2589 shell->destroy_listener.notify = shell_destroy;
2590 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2591 shell->lock_listener.notify = lock;
2592 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2593 shell->unlock_listener.notify = unlock;
2594 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002595 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002596 ec->shell_interface.create_shell_surface = create_shell_surface;
2597 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002598
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002599 wl_list_init(&shell->backgrounds);
2600 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002601 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002602
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002603 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2604 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2605 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2606 weston_layer_init(&shell->background_layer,
2607 &shell->toplevel_layer.link);
2608 wl_list_init(&shell->lock_layer.surface_list);
2609
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002610 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002611
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002612 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2613 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002614 return -1;
2615
Kristian Høgsberg75840622011-09-06 13:48:16 -04002616 if (wl_display_add_global(ec->wl_display,
2617 &desktop_shell_interface,
2618 shell, bind_desktop_shell) == NULL)
2619 return -1;
2620
Pekka Paalanen6e168112011-11-24 11:34:05 +02002621 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2622 shell, bind_screensaver) == NULL)
2623 return -1;
2624
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002625 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002626 if (launch_desktop_shell_process(shell) != 0)
2627 return -1;
2628
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002629 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002630
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002631 return 0;
2632}