blob: f11f573df0f53670c62dbbd9badfb88a7ec78794 [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;
Daniel Stone37816df2012-05-16 18:45:18 +0100135 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400136 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;
Daniel Stone103db7f2012-05-08 17:17:55 +0100172 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500173};
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,
Daniel Stone103db7f2012-05-08 17:17:55 +0100307 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500308{
309 grab->focus = NULL;
310}
311
312static void
Scott Moreau447013d2012-02-18 05:05:29 -0700313move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100314 uint32_t time, wl_fixed_t x, wl_fixed_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;
Daniel Stone37816df2012-05-16 18:45:18 +0100317 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318 struct shell_surface *shsurf = move->base.shsurf;
319 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100320 int dx = wl_fixed_to_int(pointer->x + move->dx);
321 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322
323 if (!shsurf)
324 return;
325
326 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500327
Daniel Stone103db7f2012-05-08 17:17:55 +0100328 weston_surface_configure(es, dx, 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);
Daniel Stone37816df2012-05-16 18:45:18 +0100338 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500339
Daniel Stone37816df2012-05-16 18:45:18 +0100340 if (pointer->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100342 wl_pointer_end_grab(pointer);
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{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400443 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600444 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600445 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600446
447 if (!shsurf)
448 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400449 if (!shsurf->resource.client)
450 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600451
452 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300453 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600454 if (!shsurf->ping_timer)
455 return;
456
457 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600458 loop = wl_display_get_event_loop(surface->compositor->wl_display);
459 shsurf->ping_timer->source =
460 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
461 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
462
463 wl_shell_surface_send_ping(&shsurf->resource, serial);
464 }
465}
466
467static void
468shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
469 uint32_t serial)
470{
471 struct shell_surface *shsurf = resource->data;
472
Scott Moreauff1db4a2012-04-17 19:06:18 -0600473 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600474 if (shsurf->unresponsive) {
475 /* Received pong from previously unresponsive client */
476 unresponsive_surface_fade(shsurf, true);
477 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600478 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600479 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600480 }
481}
482
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400483static void
484shell_surface_set_title(struct wl_client *client,
485 struct wl_resource *resource, const char *title)
486{
487 struct shell_surface *shsurf = resource->data;
488
489 free(shsurf->title);
490 shsurf->title = strdup(title);
491}
492
493static void
494shell_surface_set_class(struct wl_client *client,
495 struct wl_resource *resource, const char *class)
496{
497 struct shell_surface *shsurf = resource->data;
498
499 free(shsurf->class);
500 shsurf->class = strdup(class);
501}
502
Scott Moreauff1db4a2012-04-17 19:06:18 -0600503static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500504weston_surface_move(struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +0100505 struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500507 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300508 struct shell_surface *shsurf = get_shell_surface(es);
509
510 if (!shsurf)
511 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500512
513 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400514 if (!move)
515 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500516
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300517 shell_grab_init(&move->base, &move_grab_interface, shsurf);
518
Daniel Stone37816df2012-05-16 18:45:18 +0100519 move->dx = wl_fixed_from_double(es->geometry.x) -
520 ws->seat.pointer->grab_x;
521 move->dy = wl_fixed_from_double(es->geometry.y) -
522 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500523
Daniel Stone37816df2012-05-16 18:45:18 +0100524 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500525
Daniel Stone37816df2012-05-16 18:45:18 +0100526 wl_pointer_set_focus(ws->seat.pointer, NULL,
527 wl_fixed_from_int(0),
528 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400529
530 return 0;
531}
532
533static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200534shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100535 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400536{
Daniel Stone37816df2012-05-16 18:45:18 +0100537 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200538 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400539
Daniel Stone37816df2012-05-16 18:45:18 +0100540 if (ws->seat.pointer->button_count == 0 ||
541 ws->seat.pointer->grab_serial != serial ||
542 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500543 return;
544
Daniel Stone37816df2012-05-16 18:45:18 +0100545 if (weston_surface_move(shsurf->surface, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400546 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500547}
548
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500549struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300550 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500551 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200552 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500553};
554
555static void
Scott Moreau447013d2012-02-18 05:05:29 -0700556resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100557 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500558{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500559 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100560 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500561 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100562 wl_fixed_t from_x, from_y;
563 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200564
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300565 if (!resize->base.shsurf)
566 return;
567
Daniel Stone103db7f2012-05-08 17:17:55 +0100568 weston_surface_from_global_fixed(resize->base.shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100569 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100570 &from_x, &from_y);
571 weston_surface_from_global_fixed(resize->base.shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100572 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500573
Daniel Stone103db7f2012-05-08 17:17:55 +0100574 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200575 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100576 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200577 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100578 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500579 }
580
Daniel Stone103db7f2012-05-08 17:17:55 +0100581 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200582 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100583 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200584 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100585 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500586 }
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;
Daniel Stone37816df2012-05-16 18:45:18 +0100597 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500598
Daniel Stone37816df2012-05-16 18:45:18 +0100599 if (pointer->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300600 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100601 wl_pointer_end_grab(pointer);
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,
Daniel Stone37816df2012-05-16 18:45:18 +0100614 struct weston_seat *ws, 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
Daniel Stone37816df2012-05-16 18:45:18 +0100635 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500636
Daniel Stone37816df2012-05-16 18:45:18 +0100637 wl_pointer_set_focus(ws->seat.pointer, NULL,
638 wl_fixed_from_int(0),
639 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400640
641 return 0;
642}
643
644static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200645shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100646 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200647 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400648{
Daniel Stone37816df2012-05-16 18:45:18 +0100649 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200650 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400651
Alex Wu4539b082012-03-01 12:57:46 +0800652 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
653 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400654
Daniel Stone37816df2012-05-16 18:45:18 +0100655 if (ws->seat.pointer->button_count == 0 ||
656 ws->seat.pointer->grab_serial != serial ||
657 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500658 return;
659
Daniel Stone37816df2012-05-16 18:45:18 +0100660 if (weston_surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400661 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500662}
663
Juan Zhao96879df2012-02-07 08:45:41 +0800664static struct weston_output *
665get_default_output(struct weston_compositor *compositor)
666{
667 return container_of(compositor->output_list.next,
668 struct weston_output, link);
669}
670
Alex Wu4539b082012-03-01 12:57:46 +0800671static void
672shell_unset_fullscreen(struct shell_surface *shsurf)
673{
674 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800675 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
676 shell_surface_is_top_fullscreen(shsurf)) {
677 weston_output_switch_mode(shsurf->fullscreen_output,
678 shsurf->fullscreen_output->origin);
679 }
Alex Wu4539b082012-03-01 12:57:46 +0800680 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
681 shsurf->fullscreen.framerate = 0;
682 wl_list_remove(&shsurf->fullscreen.transform.link);
683 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800684 if (shsurf->fullscreen.black_surface)
685 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800686 shsurf->fullscreen.black_surface = NULL;
687 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800688 weston_surface_set_position(shsurf->surface,
689 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800690 if (shsurf->saved_rotation_valid) {
691 wl_list_insert(&shsurf->surface->geometry.transformation_list,
692 &shsurf->rotation.transform.link);
693 shsurf->saved_rotation_valid = false;
694 }
Alex Wu4539b082012-03-01 12:57:46 +0800695}
696
Pekka Paalanen98262232011-12-01 10:42:22 +0200697static int
698reset_shell_surface_type(struct shell_surface *surface)
699{
700 switch (surface->type) {
701 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800702 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200703 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800704 case SHELL_SURFACE_MAXIMIZED:
705 surface->output = get_default_output(surface->surface->compositor);
706 weston_surface_set_position(surface->surface,
707 surface->saved_x,
708 surface->saved_y);
709 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200710 case SHELL_SURFACE_PANEL:
711 case SHELL_SURFACE_BACKGROUND:
712 wl_list_remove(&surface->link);
713 wl_list_init(&surface->link);
714 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200715 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200716 case SHELL_SURFACE_LOCK:
717 wl_resource_post_error(&surface->resource,
718 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200719 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200720 return -1;
721 case SHELL_SURFACE_NONE:
722 case SHELL_SURFACE_TOPLEVEL:
723 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500724 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200725 break;
726 }
727
728 surface->type = SHELL_SURFACE_NONE;
729 return 0;
730}
731
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500732static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400733set_surface_type(struct shell_surface *shsurf)
734{
735 struct weston_surface *surface = shsurf->surface;
736 struct shell_surface *pshsurf = shsurf->parent;
737 struct weston_surface *pes;
738 struct shell_surface *priv;
739 struct desktop_shell *shell = shsurf->shell;
740
741 reset_shell_surface_type(shsurf);
742
743 shsurf->type = shsurf->next_type;
744 shsurf->next_type = SHELL_SURFACE_NONE;
745
746 switch (shsurf->type) {
747 case SHELL_SURFACE_TOPLEVEL:
748 break;
749 case SHELL_SURFACE_TRANSIENT:
750 pes = pshsurf->surface;
751 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300752 pes->geometry.x + shsurf->transient.x,
753 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400754 break;
755
756 case SHELL_SURFACE_MAXIMIZED:
757 shsurf->saved_x = surface->geometry.x;
758 shsurf->saved_y = surface->geometry.y;
759 shsurf->saved_position_valid = true;
760 break;
761
762 case SHELL_SURFACE_FULLSCREEN:
763 shsurf->saved_x = surface->geometry.x;
764 shsurf->saved_y = surface->geometry.y;
765 shsurf->saved_position_valid = true;
766
767 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
768 wl_list_remove(&shsurf->rotation.transform.link);
769 wl_list_init(&shsurf->rotation.transform.link);
770 shsurf->surface->geometry.dirty = 1;
771 shsurf->saved_rotation_valid = true;
772 }
773 break;
774
775 case SHELL_SURFACE_BACKGROUND:
776 wl_list_for_each(priv, &shell->backgrounds, link) {
777 if (priv->output == shsurf->output) {
778 priv->surface->output = NULL;
779 wl_list_remove(&priv->surface->layer_link);
780 wl_list_remove(&priv->link);
781 break;
782 }
783 }
784
785 wl_list_insert(&shell->backgrounds, &shsurf->link);
786
787 weston_surface_set_position(surface, shsurf->output->x,
788 shsurf->output->y);
789 break;
790
791 case SHELL_SURFACE_PANEL:
792 wl_list_for_each(priv, &shell->panels, link) {
793 if (priv->output == shsurf->output) {
794 priv->surface->output = NULL;
795 wl_list_remove(&priv->surface->layer_link);
796 wl_list_remove(&priv->link);
797 break;
798 }
799 }
800
801 wl_list_insert(&shell->panels, &shsurf->link);
802
803 weston_surface_set_position(surface, shsurf->output->x,
804 shsurf->output->y);
805 break;
806
807 default:
808 break;
809 }
810}
811
812static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300813set_toplevel(struct shell_surface *shsurf)
814{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400815 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300816}
817
818static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200819shell_surface_set_toplevel(struct wl_client *client,
820 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400821{
Pekka Paalanen98262232011-12-01 10:42:22 +0200822 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400823
Tiago Vignattibc052c92012-04-19 16:18:18 +0300824 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400825}
826
827static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200828shell_surface_set_transient(struct wl_client *client,
829 struct wl_resource *resource,
830 struct wl_resource *parent_resource,
831 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400832{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200833 struct shell_surface *shsurf = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200834
Alex Wu4539b082012-03-01 12:57:46 +0800835 /* assign to parents output */
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400836 shsurf->parent = parent_resource->data;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300837 shsurf->transient.x = x;
838 shsurf->transient.y = y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400839 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400840}
841
Tiago Vignattibe143262012-04-16 17:31:41 +0300842static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800843shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200844{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400845 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800846}
847
848static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300849get_output_panel_height(struct desktop_shell *shell,
850 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800851{
852 struct shell_surface *priv;
853 int panel_height = 0;
854
855 if (!output)
856 return 0;
857
Tiago Vignattibe143262012-04-16 17:31:41 +0300858 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800859 if (priv->output == output) {
860 panel_height = priv->surface->geometry.height;
861 break;
862 }
863 }
864 return panel_height;
865}
866
867static void
868shell_surface_set_maximized(struct wl_client *client,
869 struct wl_resource *resource,
870 struct wl_resource *output_resource )
871{
872 struct shell_surface *shsurf = resource->data;
873 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300874 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800875 uint32_t edges = 0, panel_height = 0;
876
877 /* get the default output, if the client set it as NULL
878 check whether the ouput is available */
879 if (output_resource)
880 shsurf->output = output_resource->data;
881 else
882 shsurf->output = get_default_output(es->compositor);
883
Tiago Vignattibe143262012-04-16 17:31:41 +0300884 shell = shell_surface_get_shell(shsurf);
885 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800886 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500887
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400888 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500889 es->output->current->width,
890 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800891
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400892 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200893}
894
Alex Wu21858432012-04-01 20:13:08 +0800895static void
896black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
897
Alex Wu4539b082012-03-01 12:57:46 +0800898static struct weston_surface *
899create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800900 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800901 GLfloat x, GLfloat y, int w, int h)
902{
903 struct weston_surface *surface = NULL;
904
905 surface = weston_surface_create(ec);
906 if (surface == NULL) {
907 fprintf(stderr, "no memory\n");
908 return NULL;
909 }
910
Alex Wu21858432012-04-01 20:13:08 +0800911 surface->configure = black_surface_configure;
912 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800913 weston_surface_configure(surface, x, y, w, h);
914 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
915 return surface;
916}
917
918/* Create black surface and append it to the associated fullscreen surface.
919 * Handle size dismatch and positioning according to the method. */
920static void
921shell_configure_fullscreen(struct shell_surface *shsurf)
922{
923 struct weston_output *output = shsurf->fullscreen_output;
924 struct weston_surface *surface = shsurf->surface;
925 struct weston_matrix *matrix;
926 float scale;
927
928 center_on_output(surface, output);
929
930 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500931 shsurf->fullscreen.black_surface =
932 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800933 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500934 output->x, output->y,
935 output->current->width,
936 output->current->height);
937
938 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
939 wl_list_insert(&surface->layer_link,
940 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800941 shsurf->fullscreen.black_surface->output = output;
942
943 switch (shsurf->fullscreen.type) {
944 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
945 break;
946 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
947 matrix = &shsurf->fullscreen.transform.matrix;
948 weston_matrix_init(matrix);
949 scale = (float)output->current->width/(float)surface->geometry.width;
950 weston_matrix_scale(matrix, scale, scale, 1);
951 wl_list_remove(&shsurf->fullscreen.transform.link);
952 wl_list_insert(surface->geometry.transformation_list.prev,
953 &shsurf->fullscreen.transform.link);
954 weston_surface_set_position(surface, output->x, output->y);
955 break;
956 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800957 if (shell_surface_is_top_fullscreen(shsurf)) {
958 struct weston_mode mode = {0,
959 surface->geometry.width,
960 surface->geometry.height,
961 shsurf->fullscreen.framerate};
962
963 if (weston_output_switch_mode(output, &mode) == 0) {
964 weston_surface_configure(shsurf->fullscreen.black_surface,
965 output->x, output->y,
966 output->current->width,
967 output->current->height);
968 weston_surface_set_position(surface, output->x, output->y);
969 break;
970 }
971 }
Alex Wu4539b082012-03-01 12:57:46 +0800972 break;
973 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
974 break;
975 default:
976 break;
977 }
978}
979
980/* make the fullscreen and black surface at the top */
981static void
982shell_stack_fullscreen(struct shell_surface *shsurf)
983{
Alex Wubd3354b2012-04-17 17:20:49 +0800984 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800985 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300986 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800987
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500988 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989 wl_list_insert(&shell->fullscreen_layer.surface_list,
990 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800991 weston_surface_damage(surface);
992
993 if (!shsurf->fullscreen.black_surface)
994 shsurf->fullscreen.black_surface =
995 create_black_surface(surface->compositor,
996 surface,
997 output->x, output->y,
998 output->current->width,
999 output->current->height);
1000
1001 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001002 wl_list_insert(&surface->layer_link,
1003 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001004 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001005}
1006
1007static void
1008shell_map_fullscreen(struct shell_surface *shsurf)
1009{
Alex Wu4539b082012-03-01 12:57:46 +08001010 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001011 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001012}
1013
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001014static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001015shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001016 struct wl_resource *resource,
1017 uint32_t method,
1018 uint32_t framerate,
1019 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001020{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001021 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001022 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001023
1024 if (output_resource)
1025 shsurf->output = output_resource->data;
1026 else
1027 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001028
Alex Wu4539b082012-03-01 12:57:46 +08001029 shsurf->fullscreen_output = shsurf->output;
1030 shsurf->fullscreen.type = method;
1031 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001032 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001033
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001034 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001035 shsurf->output->current->width,
1036 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001037}
1038
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001039static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001040popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001041 struct wl_surface *surface,
1042 wl_fixed_t x,
1043 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001044{
Daniel Stone37816df2012-05-16 18:45:18 +01001045 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001046 struct shell_surface *priv =
1047 container_of(grab, struct shell_surface, popup.grab);
1048 struct wl_client *client = priv->surface->surface.resource.client;
1049
Pekka Paalanencb108432012-01-19 16:25:40 +02001050 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001051 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001052 grab->focus = surface;
1053 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001054 wl_pointer_set_focus(pointer, NULL,
1055 wl_fixed_from_int(0),
1056 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001057 grab->focus = NULL;
1058 }
1059}
1060
1061static void
Scott Moreau447013d2012-02-18 05:05:29 -07001062popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001063 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001064{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001065 struct wl_resource *resource;
1066
Daniel Stone37816df2012-05-16 18:45:18 +01001067 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001068 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001069 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001070}
1071
1072static void
Scott Moreau447013d2012-02-18 05:05:29 -07001073popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001074 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001075{
1076 struct wl_resource *resource;
1077 struct shell_surface *shsurf =
1078 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001079 struct wl_display *display;
1080 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001081
Daniel Stone37816df2012-05-16 18:45:18 +01001082 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001083 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001084 display = wl_client_get_display(resource->client);
1085 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001086 wl_pointer_send_button(resource, serial, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001087 } else if (state == 0 &&
1088 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001089 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001090 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001091 wl_pointer_end_grab(grab->pointer);
1092 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001093 }
1094
1095 if (state == 0)
1096 shsurf->popup.initial_up = 1;
1097}
1098
Scott Moreau447013d2012-02-18 05:05:29 -07001099static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001100 popup_grab_focus,
1101 popup_grab_motion,
1102 popup_grab_button,
1103};
1104
1105static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001106shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001107{
Daniel Stone37816df2012-05-16 18:45:18 +01001108 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001109 struct weston_surface *es = shsurf->surface;
1110 struct weston_surface *parent = shsurf->parent->surface;
1111
1112 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001113 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001114
Pekka Paalanen938269a2012-02-07 14:19:01 +02001115 weston_surface_update_transform(parent);
1116 if (parent->transform.enabled) {
1117 shsurf->popup.parent_transform.matrix =
1118 parent->transform.matrix;
1119 } else {
1120 /* construct x, y translation matrix */
1121 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1122 shsurf->popup.parent_transform.matrix.d[12] =
1123 parent->geometry.x;
1124 shsurf->popup.parent_transform.matrix.d[13] =
1125 parent->geometry.y;
1126 }
1127 wl_list_insert(es->geometry.transformation_list.prev,
1128 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001129 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001130
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001131 shsurf->popup.initial_up = 0;
1132
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001133 /* We don't require the grab to still be active, but if another
1134 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001135 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1136 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001137 } else {
1138 wl_shell_surface_send_popup_done(&shsurf->resource);
1139 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001140}
1141
1142static void
1143shell_surface_set_popup(struct wl_client *client,
1144 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001145 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001146 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001147 struct wl_resource *parent_resource,
1148 int32_t x, int32_t y, uint32_t flags)
1149{
1150 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001151
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001152 shsurf->type = SHELL_SURFACE_POPUP;
1153 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001154 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001155 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001156 shsurf->popup.x = x;
1157 shsurf->popup.y = y;
1158}
1159
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001161 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001162 shell_surface_move,
1163 shell_surface_resize,
1164 shell_surface_set_toplevel,
1165 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001166 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001167 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001168 shell_surface_set_maximized,
1169 shell_surface_set_title,
1170 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001171};
1172
1173static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001174destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001175{
Daniel Stone37816df2012-05-16 18:45:18 +01001176 if (shsurf->popup.grab.pointer)
1177 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001178
Alex Wubd3354b2012-04-17 17:20:49 +08001179 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1180 shell_surface_is_top_fullscreen(shsurf)) {
1181 weston_output_switch_mode(shsurf->fullscreen_output,
1182 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001183 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001184
Alex Wuaa08e2d2012-03-05 11:01:40 +08001185 if (shsurf->fullscreen.black_surface)
1186 weston_surface_destroy(shsurf->fullscreen.black_surface);
1187
Alex Wubd3354b2012-04-17 17:20:49 +08001188 /* As destroy_resource() use wl_list_for_each_safe(),
1189 * we can always remove the listener.
1190 */
1191 wl_list_remove(&shsurf->surface_destroy_listener.link);
1192 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001193 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001194
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001195 wl_list_remove(&shsurf->link);
1196 free(shsurf);
1197}
1198
1199static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001200shell_destroy_shell_surface(struct wl_resource *resource)
1201{
1202 struct shell_surface *shsurf = resource->data;
1203
1204 destroy_shell_surface(shsurf);
1205}
1206
1207static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001208shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001209{
1210 struct shell_surface *shsurf = container_of(listener,
1211 struct shell_surface,
1212 surface_destroy_listener);
1213
Tiago Vignattibc052c92012-04-19 16:18:18 +03001214 /* tricky way to check if resource was in fact created */
1215 if (shsurf->resource.object.implementation != 0)
1216 wl_resource_destroy(&shsurf->resource);
1217 else
1218 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001219}
1220
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001221static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001222get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001223{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001224 struct wl_listener *listener;
1225
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001226 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1227 shell_handle_surface_destroy);
1228 if (listener)
1229 return container_of(listener, struct shell_surface,
1230 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001231
1232 return NULL;
1233}
1234
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001235static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001236shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1237
1238static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001239create_shell_surface(void *shell, struct weston_surface *surface,
1240 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001241{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001242 struct shell_surface *shsurf;
1243
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001244 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001245 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001246 return;
1247 }
1248
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001249 shsurf = calloc(1, sizeof *shsurf);
1250 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001251 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001252 return;
1253 }
1254
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001255 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001256 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001257
Tiago Vignattibc052c92012-04-19 16:18:18 +03001258 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001259 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001260 shsurf->unresponsive_animation.exists = 0;
1261 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001262 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001263 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001264 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001265 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001266 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1267 shsurf->fullscreen.framerate = 0;
1268 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001269 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001270 wl_list_init(&shsurf->fullscreen.transform.link);
1271
Tiago Vignattibc052c92012-04-19 16:18:18 +03001272 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001273 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1274 wl_signal_add(&surface->surface.resource.destroy_signal,
1275 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001276
1277 /* init link so its safe to always remove it in destroy_shell_surface */
1278 wl_list_init(&shsurf->link);
1279
Pekka Paalanen460099f2012-01-20 16:48:25 +02001280 /* empty when not in use */
1281 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001282 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001283
Pekka Paalanen98262232011-12-01 10:42:22 +02001284 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001285 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001286
Tiago Vignattibc052c92012-04-19 16:18:18 +03001287 *ret = shsurf;
1288}
1289
1290static void
1291shell_get_shell_surface(struct wl_client *client,
1292 struct wl_resource *resource,
1293 uint32_t id,
1294 struct wl_resource *surface_resource)
1295{
1296 struct weston_surface *surface = surface_resource->data;
1297 struct desktop_shell *shell = resource->data;
1298 struct shell_surface *shsurf;
1299
1300 if (get_shell_surface(surface)) {
1301 wl_resource_post_error(surface_resource,
1302 WL_DISPLAY_ERROR_INVALID_OBJECT,
1303 "desktop_shell::get_shell_surface already requested");
1304 return;
1305 }
1306
1307 create_shell_surface(shell, surface, &shsurf);
1308 if (!shsurf) {
1309 wl_resource_post_error(surface_resource,
1310 WL_DISPLAY_ERROR_INVALID_OBJECT,
1311 "surface->configure already set");
1312 return;
1313 }
1314
1315 shsurf->resource.destroy = shell_destroy_shell_surface;
1316 shsurf->resource.object.id = id;
1317 shsurf->resource.object.interface = &wl_shell_surface_interface;
1318 shsurf->resource.object.implementation =
1319 (void (**)(void)) &shell_surface_implementation;
1320 shsurf->resource.data = shsurf;
1321
1322 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001323}
1324
1325static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001326 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001327};
1328
Kristian Høgsberg07937562011-04-12 17:25:42 -04001329static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001330handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001331{
1332 proc->pid = 0;
1333}
1334
1335static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001336launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001337{
1338 if (shell->screensaver.binding)
1339 return;
1340
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001341 if (!shell->screensaver.path)
1342 return;
1343
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001344 if (shell->screensaver.process.pid != 0) {
1345 fprintf(stderr, "old screensaver still running\n");
1346 return;
1347 }
1348
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001349 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001350 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001351 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001352 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001353}
1354
1355static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001356terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001357{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001358 if (shell->screensaver.process.pid == 0)
1359 return;
1360
1361 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001362}
1363
1364static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001365show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001366{
1367 struct wl_list *list;
1368
1369 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001370 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001371 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001372 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001373
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001374 wl_list_remove(&surface->surface->layer_link);
1375 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001376 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001377 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001378}
1379
1380static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001381hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001382{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001383 wl_list_remove(&surface->surface->layer_link);
1384 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001385 surface->surface->output = NULL;
1386}
1387
1388static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001389desktop_shell_set_background(struct wl_client *client,
1390 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001391 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001392 struct wl_resource *surface_resource)
1393{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001394 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001395
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001396 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001397 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001398
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001399 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001400 surface_resource,
1401 shsurf->output->current->width,
1402 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001403}
1404
1405static void
1406desktop_shell_set_panel(struct wl_client *client,
1407 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001408 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001409 struct wl_resource *surface_resource)
1410{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001411 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001412
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001413 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001414 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001415
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001416 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001417 surface_resource,
1418 shsurf->output->current->width,
1419 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001420}
1421
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001422static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001423handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001424{
Tiago Vignattibe143262012-04-16 17:31:41 +03001425 struct desktop_shell *shell =
1426 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001427
1428 fprintf(stderr, "lock surface gone\n");
1429 shell->lock_surface = NULL;
1430}
1431
1432static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001433desktop_shell_set_lock_surface(struct wl_client *client,
1434 struct wl_resource *resource,
1435 struct wl_resource *surface_resource)
1436{
Tiago Vignattibe143262012-04-16 17:31:41 +03001437 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001438 struct shell_surface *surface = surface_resource->data;
1439
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001440 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001441
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001442 if (!shell->locked)
1443 return;
1444
Pekka Paalanen98262232011-12-01 10:42:22 +02001445 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001446
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001447 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1448 wl_signal_add(&surface_resource->destroy_signal,
1449 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001450
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001451 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001452}
1453
1454static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001455resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001456{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001457 struct shell_surface *tmp;
1458
1459 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1460 hide_screensaver(shell, tmp);
1461
1462 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001463
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001464 wl_list_remove(&shell->lock_layer.link);
1465 wl_list_insert(&shell->compositor->cursor_layer.link,
1466 &shell->fullscreen_layer.link);
1467 wl_list_insert(&shell->fullscreen_layer.link,
1468 &shell->panel_layer.link);
1469 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001470
1471 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001472 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001473 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001474 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001475}
1476
1477static void
1478desktop_shell_unlock(struct wl_client *client,
1479 struct wl_resource *resource)
1480{
Tiago Vignattibe143262012-04-16 17:31:41 +03001481 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001482
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001483 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001484
1485 if (shell->locked)
1486 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001487}
1488
Kristian Høgsberg75840622011-09-06 13:48:16 -04001489static const struct desktop_shell_interface desktop_shell_implementation = {
1490 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001491 desktop_shell_set_panel,
1492 desktop_shell_set_lock_surface,
1493 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001494};
1495
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001496static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001497get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001498{
1499 struct shell_surface *shsurf;
1500
1501 shsurf = get_shell_surface(surface);
1502 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001503 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001504 return shsurf->type;
1505}
1506
Kristian Høgsberg75840622011-09-06 13:48:16 -04001507static void
Daniel Stone37816df2012-05-16 18:45:18 +01001508move_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001509 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1510 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001511{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001512 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001513 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001514
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001515 if (surface == NULL)
1516 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001517
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001518 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001519 case SHELL_SURFACE_PANEL:
1520 case SHELL_SURFACE_BACKGROUND:
1521 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001522 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001523 return;
1524 default:
1525 break;
1526 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001527
Daniel Stone37816df2012-05-16 18:45:18 +01001528 weston_surface_move(surface, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001529}
1530
1531static void
Daniel Stone37816df2012-05-16 18:45:18 +01001532resize_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001533 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1534 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001535{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001537 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001538 uint32_t edges = 0;
1539 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001540 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001541
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001542 if (surface == NULL)
1543 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001544
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001545 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001546 if (!shsurf)
1547 return;
1548
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001549 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001550 case SHELL_SURFACE_PANEL:
1551 case SHELL_SURFACE_BACKGROUND:
1552 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001553 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001554 return;
1555 default:
1556 break;
1557 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001558
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001559 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001560 wl_fixed_to_int(seat->pointer->grab_x),
1561 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001562 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001563
Pekka Paalanen60921e52012-01-25 15:55:43 +02001564 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001565 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001566 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001567 edges |= 0;
1568 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001569 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001570
Pekka Paalanen60921e52012-01-25 15:55:43 +02001571 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001572 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001573 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001574 edges |= 0;
1575 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001576 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001577
Daniel Stone37816df2012-05-16 18:45:18 +01001578 weston_surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001579}
1580
1581static void
Daniel Stone37816df2012-05-16 18:45:18 +01001582surface_opacity_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001583 uint32_t key, uint32_t button, uint32_t axis,
1584 int32_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001585{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001586 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001587 struct shell_surface *shsurf;
1588 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001589 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001590
1591 if (surface == NULL)
1592 return;
1593
1594 shsurf = get_shell_surface(surface);
1595 if (!shsurf)
1596 return;
1597
1598 switch (shsurf->type) {
1599 case SHELL_SURFACE_BACKGROUND:
1600 case SHELL_SURFACE_SCREENSAVER:
1601 return;
1602 default:
1603 break;
1604 }
1605
1606 surface->alpha += value * step;
1607
1608 if (surface->alpha > 255)
1609 surface->alpha = 255;
1610 if (surface->alpha < step)
1611 surface->alpha = step;
1612
1613 surface->geometry.dirty = 1;
1614 weston_surface_damage(surface);
1615}
1616
1617static void
Daniel Stone37816df2012-05-16 18:45:18 +01001618zoom_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001619 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1620 void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001621{
Daniel Stone37816df2012-05-16 18:45:18 +01001622 struct weston_seat *ws = (struct weston_seat *) seat;
1623 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001624 struct weston_output *output;
1625
1626 wl_list_for_each(output, &compositor->output_list, link) {
1627 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01001628 wl_fixed_to_double(seat->pointer->x),
1629 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001630 NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001631 output->zoom.active = 1;
1632 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001633
1634 if (output->zoom.level >= 1.0) {
1635 output->zoom.active = 0;
1636 output->zoom.level = 1.0;
1637 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001638
1639 if (output->zoom.level < output->zoom.increment)
1640 output->zoom.level = output->zoom.increment;
1641
Daniel Stone37816df2012-05-16 18:45:18 +01001642 weston_output_update_zoom(output,
1643 seat->pointer->x,
1644 seat->pointer->y);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001645 }
1646 }
1647}
1648
Scott Moreauccbf29d2012-02-22 14:21:41 -07001649static void
Daniel Stone37816df2012-05-16 18:45:18 +01001650terminate_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001651 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001652{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001653 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001654
1655 if (state)
1656 wl_display_terminate(compositor->wl_display);
1657}
1658
1659static void
Scott Moreau447013d2012-02-18 05:05:29 -07001660rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001661 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001662{
1663 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001664 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001665 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001666 struct shell_surface *shsurf = rotate->base.shsurf;
1667 struct weston_surface *surface;
1668 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1669
1670 if (!shsurf)
1671 return;
1672
1673 surface = shsurf->surface;
1674
1675 cx = 0.5f * surface->geometry.width;
1676 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001677
Daniel Stone37816df2012-05-16 18:45:18 +01001678 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1679 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001680 r = sqrtf(dx * dx + dy * dy);
1681
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001682 wl_list_remove(&shsurf->rotation.transform.link);
1683 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001684
1685 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001686 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001687 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001688
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001689 weston_matrix_init(&rotate->rotation);
1690 rotate->rotation.d[0] = dx / r;
1691 rotate->rotation.d[4] = -dy / r;
1692 rotate->rotation.d[1] = -rotate->rotation.d[4];
1693 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001694
1695 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001696 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001697 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001698 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001699 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001700
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001701 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001702 &shsurf->surface->geometry.transformation_list,
1703 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001704 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001705 wl_list_init(&shsurf->rotation.transform.link);
1706 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001707 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001708 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001709
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001710 /* We need to adjust the position of the surface
1711 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001712 cposx = surface->geometry.x + cx;
1713 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001714 dposx = rotate->center.x - cposx;
1715 dposy = rotate->center.y - cposy;
1716 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001717 weston_surface_set_position(surface,
1718 surface->geometry.x + dposx,
1719 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001720 }
1721
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001722 /* Repaint implies weston_surface_update_transform(), which
1723 * lazily applies the damage due to rotation update.
1724 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001725 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001726}
1727
1728static void
Scott Moreau447013d2012-02-18 05:05:29 -07001729rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001730 uint32_t time, uint32_t button, uint32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001731{
1732 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001733 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001734 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001735 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001736
Daniel Stone37816df2012-05-16 18:45:18 +01001737 if (pointer->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001738 if (shsurf)
1739 weston_matrix_multiply(&shsurf->rotation.rotation,
1740 &rotate->rotation);
1741 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001742 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001743 free(rotate);
1744 }
1745}
1746
Scott Moreau447013d2012-02-18 05:05:29 -07001747static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001748 noop_grab_focus,
1749 rotate_grab_motion,
1750 rotate_grab_button,
1751};
1752
1753static void
Daniel Stone37816df2012-05-16 18:45:18 +01001754rotate_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001755 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1756 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001757{
1758 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001759 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001760 struct shell_surface *surface;
1761 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001762 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001763 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001764
1765 if (base_surface == NULL)
1766 return;
1767
1768 surface = get_shell_surface(base_surface);
1769 if (!surface)
1770 return;
1771
1772 switch (surface->type) {
1773 case SHELL_SURFACE_PANEL:
1774 case SHELL_SURFACE_BACKGROUND:
1775 case SHELL_SURFACE_FULLSCREEN:
1776 case SHELL_SURFACE_SCREENSAVER:
1777 return;
1778 default:
1779 break;
1780 }
1781
Pekka Paalanen460099f2012-01-20 16:48:25 +02001782 rotate = malloc(sizeof *rotate);
1783 if (!rotate)
1784 return;
1785
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001786 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001787
1788 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001789 surface->surface->geometry.width / 2,
1790 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001791 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001792
Daniel Stone37816df2012-05-16 18:45:18 +01001793 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001794
Daniel Stone37816df2012-05-16 18:45:18 +01001795 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1796 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001797 r = sqrtf(dx * dx + dy * dy);
1798 if (r > 20.0f) {
1799 struct weston_matrix inverse;
1800
1801 weston_matrix_init(&inverse);
1802 inverse.d[0] = dx / r;
1803 inverse.d[4] = dy / r;
1804 inverse.d[1] = -inverse.d[4];
1805 inverse.d[5] = inverse.d[0];
1806 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001807
1808 weston_matrix_init(&rotate->rotation);
1809 rotate->rotation.d[0] = dx / r;
1810 rotate->rotation.d[4] = -dy / r;
1811 rotate->rotation.d[1] = -rotate->rotation.d[4];
1812 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001813 } else {
1814 weston_matrix_init(&surface->rotation.rotation);
1815 weston_matrix_init(&rotate->rotation);
1816 }
1817
Daniel Stone37816df2012-05-16 18:45:18 +01001818 wl_pointer_set_focus(seat->pointer, NULL,
1819 wl_fixed_from_int(0),
1820 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02001821}
1822
1823static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001824activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01001825 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001826{
Alex Wu21858432012-04-01 20:13:08 +08001827 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001828
Daniel Stone37816df2012-05-16 18:45:18 +01001829 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001830
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001831 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001832 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001833 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001834 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001835 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001836
Pekka Paalanen77346a62011-11-30 16:26:35 +02001837 case SHELL_SURFACE_SCREENSAVER:
1838 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001839 if (shell->lock_surface)
1840 weston_surface_restack(es,
1841 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001842 break;
Alex Wu4539b082012-03-01 12:57:46 +08001843 case SHELL_SURFACE_FULLSCREEN:
1844 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001845 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001846 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001847 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001848 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001849 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001850 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1851 wl_list_for_each_reverse_safe(surf,
1852 prev,
1853 &shell->fullscreen_layer.surface_list,
1854 layer_link)
1855 weston_surface_restack(surf,
1856 &shell->toplevel_layer.surface_list);
1857 }
1858
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001859 weston_surface_restack(es,
1860 &shell->toplevel_layer.surface_list);
1861 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001862 }
1863}
1864
Alex Wu21858432012-04-01 20:13:08 +08001865/* no-op func for checking black surface */
1866static void
1867black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1868{
1869}
1870
1871static bool
1872is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1873{
1874 if (es->configure == black_surface_configure) {
1875 if (fs_surface)
1876 *fs_surface = (struct weston_surface *)es->private;
1877 return true;
1878 }
1879 return false;
1880}
1881
Kristian Høgsberg75840622011-09-06 13:48:16 -04001882static void
Daniel Stone37816df2012-05-16 18:45:18 +01001883click_to_activate_binding(struct wl_seat *seat,
Alex Wu4539b082012-03-01 12:57:46 +08001884 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001885 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001886{
Daniel Stone37816df2012-05-16 18:45:18 +01001887 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03001888 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001889 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001890 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001891
Daniel Stone37816df2012-05-16 18:45:18 +01001892 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001893 if (!focus)
1894 return;
1895
Alex Wu21858432012-04-01 20:13:08 +08001896 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001897 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001898
Daniel Stone37816df2012-05-16 18:45:18 +01001899 if (state && seat->pointer->grab == &seat->pointer->default_grab)
1900 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001901}
1902
1903static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001904lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001905{
Tiago Vignattibe143262012-04-16 17:31:41 +03001906 struct desktop_shell *shell =
1907 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01001908 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001909 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001910 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001911
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001912 if (shell->locked) {
1913 wl_list_for_each(output, &shell->compositor->output_list, link)
1914 /* TODO: find a way to jump to other DPMS levels */
1915 if (output->set_dpms)
1916 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001917 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001918 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001919
1920 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001921
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001922 /* Hide all surfaces by removing the fullscreen, panel and
1923 * toplevel layers. This way nothing else can show or receive
1924 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001925
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001926 wl_list_remove(&shell->panel_layer.link);
1927 wl_list_remove(&shell->toplevel_layer.link);
1928 wl_list_remove(&shell->fullscreen_layer.link);
1929 wl_list_insert(&shell->compositor->cursor_layer.link,
1930 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001931
Pekka Paalanen77346a62011-11-30 16:26:35 +02001932 launch_screensaver(shell);
1933
1934 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1935 show_screensaver(shell, shsurf);
1936
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001937 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001938 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001939 weston_compositor_wake(shell->compositor);
1940 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001941 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001942
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001943 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001944 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001945
1946 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01001947 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1948 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001949 }
1950
1951 /* TODO: disable bindings that should not work while locked. */
1952
1953 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001954}
1955
1956static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001957unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001958{
Tiago Vignattibe143262012-04-16 17:31:41 +03001959 struct desktop_shell *shell =
1960 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001961
Pekka Paalanend81c2162011-11-16 13:47:34 +02001962 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001963 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001964 return;
1965 }
1966
1967 /* If desktop-shell client has gone away, unlock immediately. */
1968 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001969 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001970 return;
1971 }
1972
1973 if (shell->prepare_event_sent)
1974 return;
1975
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001976 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001977 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001978}
1979
1980static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001981center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001984 GLfloat x = (mode->width - surface->geometry.width) / 2;
1985 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001986
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001987 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001988}
1989
1990static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001991map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001992 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001993{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001994 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001995 struct shell_surface *shsurf;
1996 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001997 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001998 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001999
Pekka Paalanen77346a62011-11-30 16:26:35 +02002000 shsurf = get_shell_surface(surface);
2001 if (shsurf)
2002 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002003
Pekka Paalanen60921e52012-01-25 15:55:43 +02002004 surface->geometry.width = width;
2005 surface->geometry.height = height;
2006 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002007
2008 /* initial positioning, see also configure() */
2009 switch (surface_type) {
2010 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002011 weston_surface_set_position(surface, 10 + random() % 400,
2012 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002013 break;
2014 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002015 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002016 break;
Alex Wu4539b082012-03-01 12:57:46 +08002017 case SHELL_SURFACE_FULLSCREEN:
2018 shell_map_fullscreen(shsurf);
2019 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002020 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002021 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002022 panel_height = get_output_panel_height(shell,surface->output);
2023 weston_surface_set_position(surface, surface->output->x,
2024 surface->output->y + panel_height);
2025 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002026 case SHELL_SURFACE_LOCK:
2027 center_on_output(surface, get_default_output(compositor));
2028 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002029 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002030 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002031 case SHELL_SURFACE_NONE:
2032 weston_surface_set_position(surface,
2033 surface->geometry.x + sx,
2034 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002035 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002036 default:
2037 ;
2038 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002039
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002040 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002041 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002042 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002043 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002044 wl_list_insert(&shell->background_layer.surface_list,
2045 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002046 break;
2047 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002048 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002049 wl_list_insert(&shell->panel_layer.surface_list,
2050 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002051 break;
2052 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002053 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002054 wl_list_insert(&shell->lock_layer.surface_list,
2055 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002056 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002057 break;
2058 case SHELL_SURFACE_SCREENSAVER:
2059 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002060 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002061 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002062 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002063 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002064 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002065 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002066 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002067 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002068 case SHELL_SURFACE_POPUP:
2069 case SHELL_SURFACE_TRANSIENT:
2070 parent = shsurf->parent->surface;
2071 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2072 break;
Alex Wu4539b082012-03-01 12:57:46 +08002073 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002074 case SHELL_SURFACE_NONE:
2075 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002076 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002077 wl_list_insert(&shell->toplevel_layer.surface_list,
2078 &surface->layer_link);
2079 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002080 }
2081
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002082 if (surface_type != SHELL_SURFACE_NONE) {
2083 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002084 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2085 surface->output = shsurf->output;
2086 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002087
Juan Zhao7bb92f02011-12-15 11:31:51 -05002088 switch (surface_type) {
2089 case SHELL_SURFACE_TOPLEVEL:
2090 case SHELL_SURFACE_TRANSIENT:
2091 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002092 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002093 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002094 activate(shell, surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002095 (struct weston_seat *) compositor->seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002096 break;
2097 default:
2098 break;
2099 }
2100
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002101 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002102 {
2103 switch (shell->win_animation_type) {
2104 case ANIMATION_FADE:
2105 weston_fade_run(surface, NULL, NULL);
2106 break;
2107 case ANIMATION_ZOOM:
2108 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2109 break;
2110 default:
2111 break;
2112 }
2113 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002114}
2115
2116static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002117configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002118 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002119{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002120 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2121 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002122
Pekka Paalanen77346a62011-11-30 16:26:35 +02002123 shsurf = get_shell_surface(surface);
2124 if (shsurf)
2125 surface_type = shsurf->type;
2126
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002127 surface->geometry.x = x;
2128 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002129 surface->geometry.width = width;
2130 surface->geometry.height = height;
2131 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002132
2133 switch (surface_type) {
2134 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002135 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002136 break;
Alex Wu4539b082012-03-01 12:57:46 +08002137 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002138 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002139 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002140 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002141 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002142 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002143 surface->geometry.x = surface->output->x;
2144 surface->geometry.y = surface->output->y +
2145 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002146 break;
Alex Wu4539b082012-03-01 12:57:46 +08002147 case SHELL_SURFACE_TOPLEVEL:
2148 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002149 default:
2150 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002151 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002152
Alex Wu4539b082012-03-01 12:57:46 +08002153 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002154 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002155 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002156
2157 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2158 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002159 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2160 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002161 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002162}
2163
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002164static void
2165shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2166{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002167 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002168 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002169 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002170
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002171 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002172 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002173 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002174 type_changed = 1;
2175 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002176
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002177 if (!weston_surface_is_mapped(es)) {
2178 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002179 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002180 es->geometry.width != es->buffer->width ||
2181 es->geometry.height != es->buffer->height) {
2182 GLfloat from_x, from_y;
2183 GLfloat to_x, to_y;
2184
2185 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2186 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2187 configure(shell, es,
2188 es->geometry.x + to_x - from_x,
2189 es->geometry.y + to_y - from_y,
2190 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002191 }
2192}
2193
Tiago Vignattibe143262012-04-16 17:31:41 +03002194static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002195
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002196static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002197desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002198{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002199 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002200 struct desktop_shell *shell =
2201 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002202
2203 shell->child.process.pid = 0;
2204 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002205
2206 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2207 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002208 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002209 shell->child.deathstamp = time;
2210 shell->child.deathcount = 0;
2211 }
2212
2213 shell->child.deathcount++;
2214 if (shell->child.deathcount > 5) {
2215 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2216 return;
2217 }
2218
2219 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2220 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002221}
2222
2223static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002224launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002225{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002226 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002227
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002228 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002229 &shell->child.process,
2230 shell_exe,
2231 desktop_shell_sigchld);
2232
2233 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002234 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002235 return 0;
2236}
2237
2238static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002239bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2240{
Tiago Vignattibe143262012-04-16 17:31:41 +03002241 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002242
2243 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002244 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002245}
2246
Kristian Høgsberg75840622011-09-06 13:48:16 -04002247static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002248unbind_desktop_shell(struct wl_resource *resource)
2249{
Tiago Vignattibe143262012-04-16 17:31:41 +03002250 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002251
2252 if (shell->locked)
2253 resume_desktop(shell);
2254
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002255 shell->child.desktop_shell = NULL;
2256 shell->prepare_event_sent = false;
2257 free(resource);
2258}
2259
2260static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002261bind_desktop_shell(struct wl_client *client,
2262 void *data, uint32_t version, uint32_t id)
2263{
Tiago Vignattibe143262012-04-16 17:31:41 +03002264 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002265 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002266
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002267 resource = wl_client_add_object(client, &desktop_shell_interface,
2268 &desktop_shell_implementation,
2269 id, shell);
2270
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002271 if (client == shell->child.client) {
2272 resource->destroy = unbind_desktop_shell;
2273 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002274 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002275 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002276
2277 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2278 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002279 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002280}
2281
Pekka Paalanen6e168112011-11-24 11:34:05 +02002282static void
2283screensaver_set_surface(struct wl_client *client,
2284 struct wl_resource *resource,
2285 struct wl_resource *shell_surface_resource,
2286 struct wl_resource *output_resource)
2287{
Tiago Vignattibe143262012-04-16 17:31:41 +03002288 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002289 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002290 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002291
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002292 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002293
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002294 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002295 surface->output = output;
2296 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002297}
2298
2299static const struct screensaver_interface screensaver_implementation = {
2300 screensaver_set_surface
2301};
2302
2303static void
2304unbind_screensaver(struct wl_resource *resource)
2305{
Tiago Vignattibe143262012-04-16 17:31:41 +03002306 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002307
Pekka Paalanen77346a62011-11-30 16:26:35 +02002308 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002309 free(resource);
2310}
2311
2312static void
2313bind_screensaver(struct wl_client *client,
2314 void *data, uint32_t version, uint32_t id)
2315{
Tiago Vignattibe143262012-04-16 17:31:41 +03002316 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002317 struct wl_resource *resource;
2318
2319 resource = wl_client_add_object(client, &screensaver_interface,
2320 &screensaver_implementation,
2321 id, shell);
2322
Pekka Paalanen77346a62011-11-30 16:26:35 +02002323 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002324 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002325 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002326 return;
2327 }
2328
2329 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2330 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002331 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002332}
2333
Kristian Høgsberg07045392012-02-19 18:52:44 -05002334struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002335 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002336 struct weston_surface *current;
2337 struct wl_listener listener;
2338 struct wl_keyboard_grab grab;
2339};
2340
2341static void
2342switcher_next(struct switcher *switcher)
2343{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002344 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002345 struct weston_surface *surface;
2346 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002347 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002348
2349 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002350 switch (get_shell_surface_type(surface)) {
2351 case SHELL_SURFACE_TOPLEVEL:
2352 case SHELL_SURFACE_FULLSCREEN:
2353 case SHELL_SURFACE_MAXIMIZED:
2354 if (first == NULL)
2355 first = surface;
2356 if (prev == switcher->current)
2357 next = surface;
2358 prev = surface;
2359 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002360 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002361 weston_surface_damage(surface);
2362 break;
2363 default:
2364 break;
2365 }
Alex Wu1659daa2012-04-01 20:13:09 +08002366
2367 if (is_black_surface(surface, NULL)) {
2368 surface->alpha = 64;
2369 surface->geometry.dirty = 1;
2370 weston_surface_damage(surface);
2371 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002372 }
2373
2374 if (next == NULL)
2375 next = first;
2376
Alex Wu07b26062012-03-12 16:06:01 +08002377 if (next == NULL)
2378 return;
2379
Kristian Høgsberg07045392012-02-19 18:52:44 -05002380 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002381 wl_signal_add(&next->surface.resource.destroy_signal,
2382 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002383
2384 switcher->current = next;
2385 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002386
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002387 shsurf = get_shell_surface(switcher->current);
2388 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2389 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002390}
2391
2392static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002393switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002394{
2395 struct switcher *switcher =
2396 container_of(listener, struct switcher, listener);
2397
2398 switcher_next(switcher);
2399}
2400
2401static void
2402switcher_destroy(struct switcher *switcher, uint32_t time)
2403{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002404 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002405 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002406 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002407
2408 wl_list_for_each(surface, &compositor->surface_list, link) {
2409 surface->alpha = 255;
2410 weston_surface_damage(surface);
2411 }
2412
Alex Wu07b26062012-03-12 16:06:01 +08002413 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002414 activate(switcher->shell, switcher->current,
2415 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002416 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002417 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002418 free(switcher);
2419}
2420
2421static void
2422switcher_key(struct wl_keyboard_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01002423 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002424{
2425 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002426 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002427
Daniel Stone37816df2012-05-16 18:45:18 +01002428 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002429 switcher_destroy(switcher, time);
2430 } else if (key == KEY_TAB && state) {
2431 switcher_next(switcher);
2432 }
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002433}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002434
2435static const struct wl_keyboard_grab_interface switcher_grab = {
2436 switcher_key
2437};
2438
2439static void
Daniel Stone37816df2012-05-16 18:45:18 +01002440switcher_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002441 uint32_t key, uint32_t button, uint32_t axis,
Daniel Stonee5a01202012-05-04 11:21:57 +01002442 int32_t value, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002443{
Tiago Vignattibe143262012-04-16 17:31:41 +03002444 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002445 struct switcher *switcher;
2446
2447 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002448 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002449 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002450 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002451 wl_list_init(&switcher->listener.link);
2452
2453 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002454 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2455 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002456 switcher_next(switcher);
2457}
2458
Pekka Paalanen3c647232011-12-22 13:43:43 +02002459static void
Daniel Stone37816df2012-05-16 18:45:18 +01002460backlight_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002461 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002462{
2463 struct weston_compositor *compositor = data;
2464 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002465 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466
2467 /* TODO: we're limiting to simple use cases, where we assume just
2468 * control on the primary display. We'd have to extend later if we
2469 * ever get support for setting backlights on random desktop LCD
2470 * panels though */
2471 output = get_default_output(compositor);
2472 if (!output)
2473 return;
2474
2475 if (!output->set_backlight)
2476 return;
2477
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002478 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2479 backlight_new = output->backlight_current - 25;
2480 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2481 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002482
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002483 if (backlight_new < 5)
2484 backlight_new = 5;
2485 if (backlight_new > 255)
2486 backlight_new = 255;
2487
2488 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002489 output->set_backlight(output, output->backlight_current);
2490}
2491
2492static void
Daniel Stone37816df2012-05-16 18:45:18 +01002493debug_repaint_binding(struct wl_seat *seat, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01002494 uint32_t key, uint32_t button, uint32_t axis,
2495 int32_t value, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002496{
Tiago Vignattibe143262012-04-16 17:31:41 +03002497 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002498 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002499 struct weston_surface *surface;
2500
2501 if (shell->debug_repaint_surface) {
2502 weston_surface_destroy(shell->debug_repaint_surface);
2503 shell->debug_repaint_surface = NULL;
2504 } else {
2505 surface = weston_surface_create(compositor);
2506 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2507 weston_surface_configure(surface, 0, 0, 8192, 8192);
2508 wl_list_insert(&compositor->fade_layer.surface_list,
2509 &surface->layer_link);
2510 weston_surface_assign_output(surface);
2511 pixman_region32_init(&surface->input);
2512
2513 /* Here's the dirty little trick that makes the
2514 * repaint debugging work: we force an
2515 * update_transform first to update dependent state
2516 * and clear the geometry.dirty bit. Then we clear
2517 * the surface damage so it only gets repainted
2518 * piecewise as we repaint other things. */
2519
2520 weston_surface_update_transform(surface);
2521 pixman_region32_fini(&surface->damage);
2522 pixman_region32_init(&surface->damage);
2523 shell->debug_repaint_surface = surface;
2524 }
2525}
2526
2527static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002528shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002529{
Tiago Vignattibe143262012-04-16 17:31:41 +03002530 struct desktop_shell *shell =
2531 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002532
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002533 if (shell->child.client)
2534 wl_client_destroy(shell->child.client);
2535
Kristian Høgsberg88c16072012-05-16 08:04:19 -04002536 wl_list_remove(&shell->lock_listener.link);
2537 wl_list_remove(&shell->unlock_listener.link);
2538
Pekka Paalanen3c647232011-12-22 13:43:43 +02002539 free(shell->screensaver.path);
2540 free(shell);
2541}
2542
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002543static void
2544shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2545{
2546 uint32_t mod;
2547
2548 /* fixed bindings */
2549 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2550 MODIFIER_CTRL | MODIFIER_ALT,
2551 terminate_binding, ec);
2552 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2553 click_to_activate_binding, shell);
2554 weston_compositor_add_binding(ec, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +01002555 WL_POINTER_AXIS_VERTICAL_SCROLL,
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002556 MODIFIER_SUPER | MODIFIER_ALT,
2557 surface_opacity_binding, NULL);
2558 weston_compositor_add_binding(ec, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +01002559 WL_POINTER_AXIS_VERTICAL_SCROLL,
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002560 MODIFIER_SUPER, zoom_binding, NULL);
2561
2562 /* configurable bindings */
2563 mod = shell->binding_modifier;
2564 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2565 move_binding, shell);
2566 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2567 resize_binding, shell);
2568 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2569 rotate_binding, NULL);
2570 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2571 switcher_binding, shell);
2572 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2573 backlight_binding, ec);
2574 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2575 backlight_binding, ec);
2576 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2577 backlight_binding, ec);
2578 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2579 backlight_binding, ec);
2580 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2581 debug_repaint_binding, shell);
2582}
2583
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002584int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002585shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002586
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002587WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002588shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002589{
Tiago Vignattibe143262012-04-16 17:31:41 +03002590 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002591
2592 shell = malloc(sizeof *shell);
2593 if (shell == NULL)
2594 return -1;
2595
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002596 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002597 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002598
2599 shell->destroy_listener.notify = shell_destroy;
2600 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2601 shell->lock_listener.notify = lock;
2602 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2603 shell->unlock_listener.notify = unlock;
2604 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002605 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002606 ec->shell_interface.create_shell_surface = create_shell_surface;
2607 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002608
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002609 wl_list_init(&shell->backgrounds);
2610 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002611 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002612
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002613 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2614 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2615 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2616 weston_layer_init(&shell->background_layer,
2617 &shell->toplevel_layer.link);
2618 wl_list_init(&shell->lock_layer.surface_list);
2619
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002620 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002621
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002622 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2623 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002624 return -1;
2625
Kristian Høgsberg75840622011-09-06 13:48:16 -04002626 if (wl_display_add_global(ec->wl_display,
2627 &desktop_shell_interface,
2628 shell, bind_desktop_shell) == NULL)
2629 return -1;
2630
Pekka Paalanen6e168112011-11-24 11:34:05 +02002631 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2632 shell, bind_screensaver) == NULL)
2633 return -1;
2634
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002635 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002636 if (launch_desktop_shell_process(shell) != 0)
2637 return -1;
2638
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002639 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002640
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002641 return 0;
2642}