blob: 9c844b5f62a93352a21016c157405f0430a29d18 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Juan Zhaoe10d2792012-04-25 19:09:52 +080039enum animation_type {
40 ANIMATION_NONE,
41
42 ANIMATION_ZOOM,
43 ANIMATION_FADE
44};
45
Tiago Vignattibe143262012-04-16 17:31:41 +030046struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050047 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040048
49 struct wl_listener lock_listener;
50 struct wl_listener unlock_listener;
51 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050053 struct weston_layer fullscreen_layer;
54 struct weston_layer panel_layer;
55 struct weston_layer toplevel_layer;
56 struct weston_layer background_layer;
57 struct weston_layer lock_layer;
58
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020059 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050060 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020063
64 unsigned deathcount;
65 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020066 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020067
68 bool locked;
69 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020070
Pekka Paalanen068ae942011-11-28 14:11:15 +020071 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050072 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073
74 struct wl_list backgrounds;
75 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020076
Pekka Paalanen77346a62011-11-30 16:26:35 +020077 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020078 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020079 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020080 struct wl_resource *binding;
81 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020083 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050084
Tiago Vignatti0b52d482012-04-20 18:54:25 +030085 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080086 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050087 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040088};
89
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020091 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050092
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093 SHELL_SURFACE_PANEL,
94 SHELL_SURFACE_BACKGROUND,
95 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020096 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050097
98 SHELL_SURFACE_TOPLEVEL,
99 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800101 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200103};
104
Scott Moreauff1db4a2012-04-17 19:06:18 -0600105struct ping_timer {
106 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600107 uint32_t serial;
108};
109
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_resource resource;
112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300116 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200117
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500118 enum shell_surface_type type;
119 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800120 bool saved_position_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600121 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100122
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500123 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200124 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500125 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126 } rotation;
127
128 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700129 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500130 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200131 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400133 struct wl_input_device *device;
134 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 } popup;
136
Alex Wu4539b082012-03-01 12:57:46 +0800137 struct {
138 enum wl_shell_surface_fullscreen_method type;
139 struct weston_transform transform; /* matrix from x, y */
140 uint32_t framerate;
141 struct weston_surface *black_surface;
142 } fullscreen;
143
Scott Moreauff1db4a2012-04-17 19:06:18 -0600144 struct ping_timer *ping_timer;
145
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600146 struct {
147 struct weston_animation current;
148 int exists;
149 int fading_in;
150 uint32_t timestamp;
151 } unresponsive_animation;
152
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500153 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100155 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300156
157 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200158};
159
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300160struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700161 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300162 struct shell_surface *shsurf;
163 struct wl_listener shsurf_destroy_listener;
164};
165
166struct weston_move_grab {
167 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500168 int32_t dx, dy;
169};
170
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300172 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500173 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174 struct {
175 int32_t x;
176 int32_t y;
177 } center;
178};
179
Alex Wubd3354b2012-04-17 17:20:49 +0800180static struct shell_surface *
181get_shell_surface(struct weston_surface *surface);
182
183static struct desktop_shell *
184shell_surface_get_shell(struct shell_surface *shsurf);
185
186static bool
187shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
188{
189 struct desktop_shell *shell;
190 struct weston_surface *top_fs_es;
191
192 shell = shell_surface_get_shell(shsurf);
193
194 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
195 return false;
196
197 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
198 struct weston_surface,
199 layer_link);
200 return (shsurf == get_shell_surface(top_fs_es));
201}
202
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500203static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400204destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205{
206 struct shell_grab *grab;
207
208 grab = container_of(listener, struct shell_grab,
209 shsurf_destroy_listener);
210
211 grab->shsurf = NULL;
212}
213
214static void
215shell_grab_init(struct shell_grab *grab,
216 const struct wl_pointer_grab_interface *interface,
217 struct shell_surface *shsurf)
218{
219 grab->grab.interface = interface;
220 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400221 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
222 wl_signal_add(&shsurf->resource.destroy_signal,
223 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300224
225}
226
227static void
228shell_grab_finish(struct shell_grab *grab)
229{
230 wl_list_remove(&grab->shsurf_destroy_listener.link);
231}
232
233static void
Alex Wu4539b082012-03-01 12:57:46 +0800234center_on_output(struct weston_surface *surface,
235 struct weston_output *output);
236
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300237static uint32_t
238get_modifier(char *modifier)
239{
240 if (!modifier)
241 return MODIFIER_SUPER;
242
243 if (!strcmp("ctrl", modifier))
244 return MODIFIER_CTRL;
245 else if (!strcmp("alt", modifier))
246 return MODIFIER_ALT;
247 else if (!strcmp("super", modifier))
248 return MODIFIER_SUPER;
249 else
250 return MODIFIER_SUPER;
251}
252
Juan Zhaoe10d2792012-04-25 19:09:52 +0800253static enum animation_type
254get_animation_type(char *animation)
255{
256 if (!animation)
257 return ANIMATION_NONE;
258
259 if (!strcmp("zoom", animation))
260 return ANIMATION_ZOOM;
261 else if (!strcmp("fade", animation))
262 return ANIMATION_FADE;
263 else
264 return ANIMATION_NONE;
265}
266
Alex Wu4539b082012-03-01 12:57:46 +0800267static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300268shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200269{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200270 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200271 char *path = NULL;
272 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300273 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800274 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200275
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400276 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300277 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800278 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200279 };
280
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400281 struct config_key saver_keys[] = {
282 { "path", CONFIG_KEY_STRING, &path },
283 { "duration", CONFIG_KEY_INTEGER, &duration },
284 };
285
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200286 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400287 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200288 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
289 };
290
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200291 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500292 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200293 free(config_file);
294
Pekka Paalanen7296e792011-12-07 16:22:00 +0200295 shell->screensaver.path = path;
296 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300297 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800298 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200299}
300
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200301static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400302noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500303 struct wl_surface *surface, int32_t x, int32_t y)
304{
305 grab->focus = NULL;
306}
307
308static void
Scott Moreau447013d2012-02-18 05:05:29 -0700309move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500310 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500312 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500313 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300314 struct shell_surface *shsurf = move->base.shsurf;
315 struct weston_surface *es;
316
317 if (!shsurf)
318 return;
319
320 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500321
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500322 weston_surface_configure(es,
323 device->x + move->dx,
324 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200325 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500326}
327
328static void
Scott Moreau447013d2012-02-18 05:05:29 -0700329move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400330 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500331{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
333 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500334 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500336 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400338 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500339 free(grab);
340 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500341}
342
Scott Moreau447013d2012-02-18 05:05:29 -0700343static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500344 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500345 move_grab_motion,
346 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500347};
348
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600349static void
350unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
351{
352 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
353
354 if(!shsurf->unresponsive_animation.exists) {
355 wl_list_insert(&shsurf->surface->compositor->animation_list,
356 &shsurf->unresponsive_animation.current.link);
357 shsurf->unresponsive_animation.exists = 1;
358 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
359 weston_surface_damage(shsurf->surface);
360 }
361}
362
363static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600364unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600365 struct weston_output *output, uint32_t msecs)
366{
367 struct shell_surface *shsurf =
368 container_of(animation, struct shell_surface, unresponsive_animation.current);
369 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600370 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600371
372 if (!surface || !shsurf)
373 return;
374
375 if (shsurf->unresponsive_animation.fading_in) {
376 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
377 if (surface->saturation > 1)
378 surface->saturation -= 5;
379 if (surface->brightness > 200)
380 surface->brightness--;
381
382 shsurf->unresponsive_animation.timestamp += step;
383 }
384
385 if (surface->saturation <= 1 && surface->brightness <= 200) {
386 wl_list_remove(&shsurf->unresponsive_animation.current.link);
387 shsurf->unresponsive_animation.exists = 0;
388 }
389 }
390 else {
391 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
392 if (surface->saturation < 255)
393 surface->saturation += 5;
394 if (surface->brightness < 255)
395 surface->brightness++;
396
397 shsurf->unresponsive_animation.timestamp += step;
398 }
399
400 if (surface->saturation >= 255 && surface->brightness >= 255) {
401 surface->saturation = surface->brightness = 255;
402 wl_list_remove(&shsurf->unresponsive_animation.current.link);
403 shsurf->unresponsive_animation.exists = 0;
404 }
405 }
406
407 surface->geometry.dirty = 1;
408 weston_surface_damage(surface);
409}
410
Scott Moreau9521d5e2012-04-19 13:06:17 -0600411static void
412ping_timer_destroy(struct shell_surface *shsurf)
413{
414 if (!shsurf || !shsurf->ping_timer)
415 return;
416
417 if (shsurf->ping_timer->source)
418 wl_event_source_remove(shsurf->ping_timer->source);
419
420 free(shsurf->ping_timer);
421 shsurf->ping_timer = NULL;
422}
423
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400424static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600425ping_timeout_handler(void *data)
426{
427 struct shell_surface *shsurf = data;
428
Scott Moreau9521d5e2012-04-19 13:06:17 -0600429 /* Client is not responding */
430 shsurf->unresponsive = 1;
431 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600432
433 return 1;
434}
435
436static void
437ping_handler(struct weston_surface *surface, uint32_t serial)
438{
439 struct shell_surface *shsurf;
440 shsurf = get_shell_surface(surface);
441 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600442 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600443
444 if (!shsurf)
445 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400446 if (!shsurf->resource.client)
447 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600448
449 if (!shsurf->ping_timer) {
450 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
451 if (!shsurf->ping_timer)
452 return;
453
454 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600455 loop = wl_display_get_event_loop(surface->compositor->wl_display);
456 shsurf->ping_timer->source =
457 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
458 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
459
460 wl_shell_surface_send_ping(&shsurf->resource, serial);
461 }
462}
463
464static void
465shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
466 uint32_t serial)
467{
468 struct shell_surface *shsurf = resource->data;
469
Scott Moreauff1db4a2012-04-17 19:06:18 -0600470 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600471 if (shsurf->unresponsive) {
472 /* Received pong from previously unresponsive client */
473 unresponsive_surface_fade(shsurf, true);
474 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600475 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600476 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600477 }
478}
479
480static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500481weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400482 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500484 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300485 struct shell_surface *shsurf = get_shell_surface(es);
486
487 if (!shsurf)
488 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500489
490 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400491 if (!move)
492 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500493
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300494 shell_grab_init(&move->base, &move_grab_interface, shsurf);
495
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200496 move->dx = es->geometry.x - wd->input_device.grab_x;
497 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500498
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300499 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400500 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500501
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400502 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400503
504 return 0;
505}
506
507static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200508shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400509 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400510{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500511 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200512 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400513
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500514 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400515 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500516 wd->input_device.pointer_focus != &shsurf->surface->surface)
517 return;
518
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400519 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400520 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500521}
522
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500523struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300524 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500525 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200526 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500527};
528
529static void
Scott Moreau447013d2012-02-18 05:05:29 -0700530resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500531 uint32_t time, int32_t x, int32_t y)
532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500533 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500534 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500535 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200536 int32_t from_x, from_y;
537 int32_t to_x, to_y;
538
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300539 if (!resize->base.shsurf)
540 return;
541
542 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200543 device->grab_x, device->grab_y,
544 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300545 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200546 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500547
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200548 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200549 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200550 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200551 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500552 } else {
553 width = resize->width;
554 }
555
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200556 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200557 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200558 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200559 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500560 } else {
561 height = resize->height;
562 }
563
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300564 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400565 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500566}
567
568static void
Scott Moreau447013d2012-02-18 05:05:29 -0700569resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400570 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500571{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300572 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500573 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500574
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500575 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300576 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400577 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500578 free(grab);
579 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500580}
581
Scott Moreau447013d2012-02-18 05:05:29 -0700582static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500583 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500584 resize_grab_motion,
585 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500586};
587
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400588static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500589weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400590 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500591{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500592 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500593
Alex Wu4539b082012-03-01 12:57:46 +0800594 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
595 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500596
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200597 if (edges == 0 || edges > 15 ||
598 (edges & 3) == 3 || (edges & 12) == 12)
599 return 0;
600
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500601 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400602 if (!resize)
603 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500604
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300605 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
606
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500607 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200608 resize->width = shsurf->surface->geometry.width;
609 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400610
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300611 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400612 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500613
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400614 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400615
616 return 0;
617}
618
619static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200620shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400621 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200622 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400623{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500624 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200625 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400626
Alex Wu4539b082012-03-01 12:57:46 +0800627 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
628 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400629
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500630 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400631 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500632 wd->input_device.pointer_focus != &shsurf->surface->surface)
633 return;
634
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400635 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400636 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500637}
638
Juan Zhao96879df2012-02-07 08:45:41 +0800639static struct weston_output *
640get_default_output(struct weston_compositor *compositor)
641{
642 return container_of(compositor->output_list.next,
643 struct weston_output, link);
644}
645
Alex Wu4539b082012-03-01 12:57:46 +0800646static void
647shell_unset_fullscreen(struct shell_surface *shsurf)
648{
649 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800650 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
651 shell_surface_is_top_fullscreen(shsurf)) {
652 weston_output_switch_mode(shsurf->fullscreen_output,
653 shsurf->fullscreen_output->origin);
654 }
Alex Wu4539b082012-03-01 12:57:46 +0800655 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
656 shsurf->fullscreen.framerate = 0;
657 wl_list_remove(&shsurf->fullscreen.transform.link);
658 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800659 if (shsurf->fullscreen.black_surface)
660 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800661 shsurf->fullscreen.black_surface = NULL;
662 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300663 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800664 weston_surface_set_position(shsurf->surface,
665 shsurf->saved_x, shsurf->saved_y);
666}
667
Pekka Paalanen98262232011-12-01 10:42:22 +0200668static int
669reset_shell_surface_type(struct shell_surface *surface)
670{
671 switch (surface->type) {
672 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800673 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200674 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800675 case SHELL_SURFACE_MAXIMIZED:
676 surface->output = get_default_output(surface->surface->compositor);
677 weston_surface_set_position(surface->surface,
678 surface->saved_x,
679 surface->saved_y);
680 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200681 case SHELL_SURFACE_PANEL:
682 case SHELL_SURFACE_BACKGROUND:
683 wl_list_remove(&surface->link);
684 wl_list_init(&surface->link);
685 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200686 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200687 case SHELL_SURFACE_LOCK:
688 wl_resource_post_error(&surface->resource,
689 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200690 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200691 return -1;
692 case SHELL_SURFACE_NONE:
693 case SHELL_SURFACE_TOPLEVEL:
694 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500695 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200696 break;
697 }
698
699 surface->type = SHELL_SURFACE_NONE;
700 return 0;
701}
702
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500703static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300704set_toplevel(struct shell_surface *shsurf)
705{
706 if (reset_shell_surface_type(shsurf))
707 return;
708
709 shsurf->type = SHELL_SURFACE_TOPLEVEL;
710}
711
712static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200713shell_surface_set_toplevel(struct wl_client *client,
714 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400715
716{
Pekka Paalanen98262232011-12-01 10:42:22 +0200717 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400718
Tiago Vignattibc052c92012-04-19 16:18:18 +0300719 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400720}
721
722static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200723shell_surface_set_transient(struct wl_client *client,
724 struct wl_resource *resource,
725 struct wl_resource *parent_resource,
726 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400727{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200728 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500729 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200730 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500731 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400732
Pekka Paalanen98262232011-12-01 10:42:22 +0200733 if (reset_shell_surface_type(shsurf))
734 return;
735
Alex Wu4539b082012-03-01 12:57:46 +0800736 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800737 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200738 weston_surface_set_position(es, pes->geometry.x + x,
739 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200741 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400742}
743
Tiago Vignattibe143262012-04-16 17:31:41 +0300744static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800745shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200746{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400747 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800748}
749
750static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300751get_output_panel_height(struct desktop_shell *shell,
752 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800753{
754 struct shell_surface *priv;
755 int panel_height = 0;
756
757 if (!output)
758 return 0;
759
Tiago Vignattibe143262012-04-16 17:31:41 +0300760 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800761 if (priv->output == output) {
762 panel_height = priv->surface->geometry.height;
763 break;
764 }
765 }
766 return panel_height;
767}
768
769static void
770shell_surface_set_maximized(struct wl_client *client,
771 struct wl_resource *resource,
772 struct wl_resource *output_resource )
773{
774 struct shell_surface *shsurf = resource->data;
775 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300776 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800777 uint32_t edges = 0, panel_height = 0;
778
779 /* get the default output, if the client set it as NULL
780 check whether the ouput is available */
781 if (output_resource)
782 shsurf->output = output_resource->data;
783 else
784 shsurf->output = get_default_output(es->compositor);
785
786 if (reset_shell_surface_type(shsurf))
787 return;
788
789 shsurf->saved_x = es->geometry.x;
790 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800791 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800792
Tiago Vignattibe143262012-04-16 17:31:41 +0300793 shell = shell_surface_get_shell(shsurf);
794 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800795 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500796
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400797 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500798 es->output->current->width,
799 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800800
801 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200802}
803
Alex Wu21858432012-04-01 20:13:08 +0800804static void
805black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
806
Alex Wu4539b082012-03-01 12:57:46 +0800807static struct weston_surface *
808create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800809 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800810 GLfloat x, GLfloat y, int w, int h)
811{
812 struct weston_surface *surface = NULL;
813
814 surface = weston_surface_create(ec);
815 if (surface == NULL) {
816 fprintf(stderr, "no memory\n");
817 return NULL;
818 }
819
Alex Wu21858432012-04-01 20:13:08 +0800820 surface->configure = black_surface_configure;
821 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800822 weston_surface_configure(surface, x, y, w, h);
823 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
824 return surface;
825}
826
827/* Create black surface and append it to the associated fullscreen surface.
828 * Handle size dismatch and positioning according to the method. */
829static void
830shell_configure_fullscreen(struct shell_surface *shsurf)
831{
832 struct weston_output *output = shsurf->fullscreen_output;
833 struct weston_surface *surface = shsurf->surface;
834 struct weston_matrix *matrix;
835 float scale;
836
837 center_on_output(surface, output);
838
839 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500840 shsurf->fullscreen.black_surface =
841 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800842 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500843 output->x, output->y,
844 output->current->width,
845 output->current->height);
846
847 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
848 wl_list_insert(&surface->layer_link,
849 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800850 shsurf->fullscreen.black_surface->output = output;
851
852 switch (shsurf->fullscreen.type) {
853 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
854 break;
855 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
856 matrix = &shsurf->fullscreen.transform.matrix;
857 weston_matrix_init(matrix);
858 scale = (float)output->current->width/(float)surface->geometry.width;
859 weston_matrix_scale(matrix, scale, scale, 1);
860 wl_list_remove(&shsurf->fullscreen.transform.link);
861 wl_list_insert(surface->geometry.transformation_list.prev,
862 &shsurf->fullscreen.transform.link);
863 weston_surface_set_position(surface, output->x, output->y);
864 break;
865 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800866 if (shell_surface_is_top_fullscreen(shsurf)) {
867 struct weston_mode mode = {0,
868 surface->geometry.width,
869 surface->geometry.height,
870 shsurf->fullscreen.framerate};
871
872 if (weston_output_switch_mode(output, &mode) == 0) {
873 weston_surface_configure(shsurf->fullscreen.black_surface,
874 output->x, output->y,
875 output->current->width,
876 output->current->height);
877 weston_surface_set_position(surface, output->x, output->y);
878 break;
879 }
880 }
Alex Wu4539b082012-03-01 12:57:46 +0800881 break;
882 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
883 break;
884 default:
885 break;
886 }
887}
888
889/* make the fullscreen and black surface at the top */
890static void
891shell_stack_fullscreen(struct shell_surface *shsurf)
892{
Alex Wubd3354b2012-04-17 17:20:49 +0800893 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800894 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300895 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800896
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500897 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500898 wl_list_insert(&shell->fullscreen_layer.surface_list,
899 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800900 weston_surface_damage(surface);
901
902 if (!shsurf->fullscreen.black_surface)
903 shsurf->fullscreen.black_surface =
904 create_black_surface(surface->compositor,
905 surface,
906 output->x, output->y,
907 output->current->width,
908 output->current->height);
909
910 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500911 wl_list_insert(&surface->layer_link,
912 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500913 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800914}
915
916static void
917shell_map_fullscreen(struct shell_surface *shsurf)
918{
Alex Wu4539b082012-03-01 12:57:46 +0800919 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800920 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800921}
922
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400923static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200924shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500925 struct wl_resource *resource,
926 uint32_t method,
927 uint32_t framerate,
928 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400929{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200930 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500931 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800932
933 if (output_resource)
934 shsurf->output = output_resource->data;
935 else
936 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400937
Pekka Paalanen98262232011-12-01 10:42:22 +0200938 if (reset_shell_surface_type(shsurf))
939 return;
940
Alex Wu4539b082012-03-01 12:57:46 +0800941 shsurf->fullscreen_output = shsurf->output;
942 shsurf->fullscreen.type = method;
943 shsurf->fullscreen.framerate = framerate;
944 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400945
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200946 shsurf->saved_x = es->geometry.x;
947 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800948 shsurf->saved_position_valid = true;
949
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300950 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300951 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500952
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400953 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500954 shsurf->output->current->width,
955 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400956}
957
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500958static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400959popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500960 struct wl_surface *surface, int32_t x, int32_t y)
961{
962 struct wl_input_device *device = grab->input_device;
963 struct shell_surface *priv =
964 container_of(grab, struct shell_surface, popup.grab);
965 struct wl_client *client = priv->surface->surface.resource.client;
966
Pekka Paalanencb108432012-01-19 16:25:40 +0200967 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400968 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500969 grab->focus = surface;
970 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400971 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500972 grab->focus = NULL;
973 }
974}
975
976static void
Scott Moreau447013d2012-02-18 05:05:29 -0700977popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200978 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500979{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500980 struct wl_resource *resource;
981
982 resource = grab->input_device->pointer_focus_resource;
983 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500984 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500985}
986
987static void
Scott Moreau447013d2012-02-18 05:05:29 -0700988popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400989 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500990{
991 struct wl_resource *resource;
992 struct shell_surface *shsurf =
993 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400994 struct wl_display *display;
995 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500996
997 resource = grab->input_device->pointer_focus_resource;
998 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400999 display = wl_client_get_display(resource->client);
1000 serial = wl_display_get_serial(display);
1001 wl_input_device_send_button(resource, serial,
1002 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001003 } else if (state == 0 &&
1004 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001005 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001006 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001007 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001008 shsurf->popup.grab.input_device = NULL;
1009 }
1010
1011 if (state == 0)
1012 shsurf->popup.initial_up = 1;
1013}
1014
Scott Moreau447013d2012-02-18 05:05:29 -07001015static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001016 popup_grab_focus,
1017 popup_grab_motion,
1018 popup_grab_button,
1019};
1020
1021static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001022shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001023{
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001024 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001025 struct weston_surface *es = shsurf->surface;
1026 struct weston_surface *parent = shsurf->parent->surface;
1027
1028 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001029 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001030
Pekka Paalanen938269a2012-02-07 14:19:01 +02001031 weston_surface_update_transform(parent);
1032 if (parent->transform.enabled) {
1033 shsurf->popup.parent_transform.matrix =
1034 parent->transform.matrix;
1035 } else {
1036 /* construct x, y translation matrix */
1037 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1038 shsurf->popup.parent_transform.matrix.d[12] =
1039 parent->geometry.x;
1040 shsurf->popup.parent_transform.matrix.d[13] =
1041 parent->geometry.y;
1042 }
1043 wl_list_insert(es->geometry.transformation_list.prev,
1044 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001045 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001046
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001047 shsurf->popup.initial_up = 0;
1048
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001049 /* We don't require the grab to still be active, but if another
1050 * grab has started in the meantime, we end the popup now. */
1051 if (device->grab_serial == shsurf->popup.serial) {
1052 wl_input_device_start_pointer_grab(device,
1053 &shsurf->popup.grab);
1054 } else {
1055 wl_shell_surface_send_popup_done(&shsurf->resource);
1056 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001057}
1058
1059static void
1060shell_surface_set_popup(struct wl_client *client,
1061 struct wl_resource *resource,
1062 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001063 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001064 struct wl_resource *parent_resource,
1065 int32_t x, int32_t y, uint32_t flags)
1066{
1067 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001068
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001069 shsurf->type = SHELL_SURFACE_POPUP;
1070 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001071 shsurf->popup.device = input_device_resource->data;
1072 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001073 shsurf->popup.x = x;
1074 shsurf->popup.y = y;
1075}
1076
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001078 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001079 shell_surface_move,
1080 shell_surface_resize,
1081 shell_surface_set_toplevel,
1082 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001083 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001084 shell_surface_set_popup,
1085 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001086};
1087
1088static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001089destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001090{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001091 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001092 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001093
Alex Wubd3354b2012-04-17 17:20:49 +08001094 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1095 shell_surface_is_top_fullscreen(shsurf)) {
1096 weston_output_switch_mode(shsurf->fullscreen_output,
1097 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001098 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001099
Alex Wuaa08e2d2012-03-05 11:01:40 +08001100 if (shsurf->fullscreen.black_surface)
1101 weston_surface_destroy(shsurf->fullscreen.black_surface);
1102
Alex Wubd3354b2012-04-17 17:20:49 +08001103 /* As destroy_resource() use wl_list_for_each_safe(),
1104 * we can always remove the listener.
1105 */
1106 wl_list_remove(&shsurf->surface_destroy_listener.link);
1107 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001108 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001109
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001110 wl_list_remove(&shsurf->link);
1111 free(shsurf);
1112}
1113
1114static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001115shell_destroy_shell_surface(struct wl_resource *resource)
1116{
1117 struct shell_surface *shsurf = resource->data;
1118
1119 destroy_shell_surface(shsurf);
1120}
1121
1122static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001123shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001124{
1125 struct shell_surface *shsurf = container_of(listener,
1126 struct shell_surface,
1127 surface_destroy_listener);
1128
Tiago Vignattibc052c92012-04-19 16:18:18 +03001129 /* tricky way to check if resource was in fact created */
1130 if (shsurf->resource.object.implementation != 0)
1131 wl_resource_destroy(&shsurf->resource);
1132 else
1133 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001134}
1135
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001136static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001137get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001138{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001139 struct wl_listener *listener;
1140
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001141 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1142 shell_handle_surface_destroy);
1143 if (listener)
1144 return container_of(listener, struct shell_surface,
1145 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001146
1147 return NULL;
1148}
1149
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001150static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001151shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1152
1153static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001154create_shell_surface(void *shell, struct weston_surface *surface,
1155 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001156{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001157 struct shell_surface *shsurf;
1158
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001159 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001160 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001161 return;
1162 }
1163
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001164 shsurf = calloc(1, sizeof *shsurf);
1165 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001166 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001167 return;
1168 }
1169
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001170 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001171 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001172
Tiago Vignattibc052c92012-04-19 16:18:18 +03001173 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001174 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001175 shsurf->unresponsive_animation.exists = 0;
1176 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001177 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001178 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001179 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001180 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1181 shsurf->fullscreen.framerate = 0;
1182 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001183 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001184 wl_list_init(&shsurf->fullscreen.transform.link);
1185
Tiago Vignattibc052c92012-04-19 16:18:18 +03001186 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001187 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1188 wl_signal_add(&surface->surface.resource.destroy_signal,
1189 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001190
1191 /* init link so its safe to always remove it in destroy_shell_surface */
1192 wl_list_init(&shsurf->link);
1193
Pekka Paalanen460099f2012-01-20 16:48:25 +02001194 /* empty when not in use */
1195 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001196 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001197
Pekka Paalanen98262232011-12-01 10:42:22 +02001198 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001199
Tiago Vignattibc052c92012-04-19 16:18:18 +03001200 *ret = shsurf;
1201}
1202
1203static void
1204shell_get_shell_surface(struct wl_client *client,
1205 struct wl_resource *resource,
1206 uint32_t id,
1207 struct wl_resource *surface_resource)
1208{
1209 struct weston_surface *surface = surface_resource->data;
1210 struct desktop_shell *shell = resource->data;
1211 struct shell_surface *shsurf;
1212
1213 if (get_shell_surface(surface)) {
1214 wl_resource_post_error(surface_resource,
1215 WL_DISPLAY_ERROR_INVALID_OBJECT,
1216 "desktop_shell::get_shell_surface already requested");
1217 return;
1218 }
1219
1220 create_shell_surface(shell, surface, &shsurf);
1221 if (!shsurf) {
1222 wl_resource_post_error(surface_resource,
1223 WL_DISPLAY_ERROR_INVALID_OBJECT,
1224 "surface->configure already set");
1225 return;
1226 }
1227
1228 shsurf->resource.destroy = shell_destroy_shell_surface;
1229 shsurf->resource.object.id = id;
1230 shsurf->resource.object.interface = &wl_shell_surface_interface;
1231 shsurf->resource.object.implementation =
1232 (void (**)(void)) &shell_surface_implementation;
1233 shsurf->resource.data = shsurf;
1234
1235 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001236}
1237
1238static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001239 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001240};
1241
Kristian Høgsberg07937562011-04-12 17:25:42 -04001242static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001243handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001244{
1245 proc->pid = 0;
1246}
1247
1248static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001249launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001250{
1251 if (shell->screensaver.binding)
1252 return;
1253
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001254 if (!shell->screensaver.path)
1255 return;
1256
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001257 if (shell->screensaver.process.pid != 0) {
1258 fprintf(stderr, "old screensaver still running\n");
1259 return;
1260 }
1261
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001262 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001263 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001264 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001265 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001266}
1267
1268static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001269terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001270{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001271 if (shell->screensaver.process.pid == 0)
1272 return;
1273
1274 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001275}
1276
1277static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001278show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001279{
1280 struct wl_list *list;
1281
1282 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001283 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001284 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001285 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001286
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001287 wl_list_remove(&surface->surface->layer_link);
1288 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001289 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001290 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001291}
1292
1293static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001294hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001295{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001296 wl_list_remove(&surface->surface->layer_link);
1297 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001298 surface->surface->output = NULL;
1299}
1300
1301static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001302desktop_shell_set_background(struct wl_client *client,
1303 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001304 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001305 struct wl_resource *surface_resource)
1306{
Tiago Vignattibe143262012-04-16 17:31:41 +03001307 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001308 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001309 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001310 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001311
Pekka Paalanen98262232011-12-01 10:42:22 +02001312 if (reset_shell_surface_type(shsurf))
1313 return;
1314
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001315 wl_list_for_each(priv, &shell->backgrounds, link) {
1316 if (priv->output == output_resource->data) {
1317 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001318 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001319 wl_list_remove(&priv->link);
1320 break;
1321 }
1322 }
1323
Pekka Paalanen068ae942011-11-28 14:11:15 +02001324 shsurf->type = SHELL_SURFACE_BACKGROUND;
1325 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001326
Pekka Paalanen068ae942011-11-28 14:11:15 +02001327 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001328
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001329 weston_surface_set_position(surface, shsurf->output->x,
1330 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001331
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001332 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001333 surface_resource,
1334 shsurf->output->current->width,
1335 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001336}
1337
1338static void
1339desktop_shell_set_panel(struct wl_client *client,
1340 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001341 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001342 struct wl_resource *surface_resource)
1343{
Tiago Vignattibe143262012-04-16 17:31:41 +03001344 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001345 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001346 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001347 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001348
Pekka Paalanen98262232011-12-01 10:42:22 +02001349 if (reset_shell_surface_type(shsurf))
1350 return;
1351
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001352 wl_list_for_each(priv, &shell->panels, link) {
1353 if (priv->output == output_resource->data) {
1354 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001355 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001356 wl_list_remove(&priv->link);
1357 break;
1358 }
1359 }
1360
Pekka Paalanen068ae942011-11-28 14:11:15 +02001361 shsurf->type = SHELL_SURFACE_PANEL;
1362 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001363
Pekka Paalanen068ae942011-11-28 14:11:15 +02001364 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001365
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001366 weston_surface_set_position(surface, shsurf->output->x,
1367 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001368
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001369 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001370 surface_resource,
1371 shsurf->output->current->width,
1372 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001373}
1374
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001375static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001376handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001377{
Tiago Vignattibe143262012-04-16 17:31:41 +03001378 struct desktop_shell *shell =
1379 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001380
1381 fprintf(stderr, "lock surface gone\n");
1382 shell->lock_surface = NULL;
1383}
1384
1385static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001386desktop_shell_set_lock_surface(struct wl_client *client,
1387 struct wl_resource *resource,
1388 struct wl_resource *surface_resource)
1389{
Tiago Vignattibe143262012-04-16 17:31:41 +03001390 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001391 struct shell_surface *surface = surface_resource->data;
1392
1393 if (reset_shell_surface_type(surface))
1394 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001395
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001396 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001397
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001398 if (!shell->locked)
1399 return;
1400
Pekka Paalanen98262232011-12-01 10:42:22 +02001401 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001402
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001403 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1404 wl_signal_add(&surface_resource->destroy_signal,
1405 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001406
Pekka Paalanen068ae942011-11-28 14:11:15 +02001407 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001408}
1409
1410static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001411resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001412{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001413 struct shell_surface *tmp;
1414
1415 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1416 hide_screensaver(shell, tmp);
1417
1418 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001419
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001420 wl_list_remove(&shell->lock_layer.link);
1421 wl_list_insert(&shell->compositor->cursor_layer.link,
1422 &shell->fullscreen_layer.link);
1423 wl_list_insert(&shell->fullscreen_layer.link,
1424 &shell->panel_layer.link);
1425 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001426
1427 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001428 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001430 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001431}
1432
1433static void
1434desktop_shell_unlock(struct wl_client *client,
1435 struct wl_resource *resource)
1436{
Tiago Vignattibe143262012-04-16 17:31:41 +03001437 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001438
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001439 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001440
1441 if (shell->locked)
1442 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001443}
1444
Kristian Høgsberg75840622011-09-06 13:48:16 -04001445static const struct desktop_shell_interface desktop_shell_implementation = {
1446 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001447 desktop_shell_set_panel,
1448 desktop_shell_set_lock_surface,
1449 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001450};
1451
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001452static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001453get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001454{
1455 struct shell_surface *shsurf;
1456
1457 shsurf = get_shell_surface(surface);
1458 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001459 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001460 return shsurf->type;
1461}
1462
Kristian Høgsberg75840622011-09-06 13:48:16 -04001463static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001464move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001465 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001466{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001467 struct weston_surface *surface =
1468 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001469
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001470 if (surface == NULL)
1471 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001472
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001473 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001474 case SHELL_SURFACE_PANEL:
1475 case SHELL_SURFACE_BACKGROUND:
1476 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001477 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001478 return;
1479 default:
1480 break;
1481 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001482
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001483 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001484}
1485
1486static void
1487resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001488 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001489{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490 struct weston_surface *surface =
1491 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001492 uint32_t edges = 0;
1493 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001494 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001495
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001496 if (surface == NULL)
1497 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001498
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001499 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001500 if (!shsurf)
1501 return;
1502
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001503 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001504 case SHELL_SURFACE_PANEL:
1505 case SHELL_SURFACE_BACKGROUND:
1506 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001507 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001508 return;
1509 default:
1510 break;
1511 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001512
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001513 weston_surface_from_global(surface,
1514 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001515
Pekka Paalanen60921e52012-01-25 15:55:43 +02001516 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001517 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001518 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001519 edges |= 0;
1520 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001521 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001522
Pekka Paalanen60921e52012-01-25 15:55:43 +02001523 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001524 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001525 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001526 edges |= 0;
1527 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001528 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001529
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001530 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001531 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001532}
1533
1534static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001535surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1536 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1537{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001538 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001539 struct shell_surface *shsurf;
1540 struct weston_surface *surface =
1541 (struct weston_surface *) device->pointer_focus;
1542
1543 if (surface == NULL)
1544 return;
1545
1546 shsurf = get_shell_surface(surface);
1547 if (!shsurf)
1548 return;
1549
1550 switch (shsurf->type) {
1551 case SHELL_SURFACE_BACKGROUND:
1552 case SHELL_SURFACE_SCREENSAVER:
1553 return;
1554 default:
1555 break;
1556 }
1557
1558 surface->alpha += value * step;
1559
1560 if (surface->alpha > 255)
1561 surface->alpha = 255;
1562 if (surface->alpha < step)
1563 surface->alpha = step;
1564
1565 surface->geometry.dirty = 1;
1566 weston_surface_damage(surface);
1567}
1568
1569static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001570zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001571 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001572{
1573 struct weston_input_device *wd = (struct weston_input_device *) device;
1574 struct weston_compositor *compositor = wd->compositor;
1575 struct weston_output *output;
1576
1577 wl_list_for_each(output, &compositor->output_list, link) {
1578 if (pixman_region32_contains_point(&output->region,
1579 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001580 output->zoom.active = 1;
1581 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001582
1583 if (output->zoom.level >= 1.0) {
1584 output->zoom.active = 0;
1585 output->zoom.level = 1.0;
1586 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001587
1588 if (output->zoom.level < output->zoom.increment)
1589 output->zoom.level = output->zoom.increment;
1590
1591 weston_output_update_zoom(output, device->x, device->y);
1592 }
1593 }
1594}
1595
Scott Moreauccbf29d2012-02-22 14:21:41 -07001596static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001597terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001598 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001599{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001600 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001601
1602 if (state)
1603 wl_display_terminate(compositor->wl_display);
1604}
1605
1606static void
Scott Moreau447013d2012-02-18 05:05:29 -07001607rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001608 uint32_t time, int32_t x, int32_t y)
1609{
1610 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001611 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001612 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001613 struct shell_surface *shsurf = rotate->base.shsurf;
1614 struct weston_surface *surface;
1615 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1616
1617 if (!shsurf)
1618 return;
1619
1620 surface = shsurf->surface;
1621
1622 cx = 0.5f * surface->geometry.width;
1623 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001624
1625 dx = device->x - rotate->center.x;
1626 dy = device->y - rotate->center.y;
1627 r = sqrtf(dx * dx + dy * dy);
1628
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001629 wl_list_remove(&shsurf->rotation.transform.link);
1630 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001631
1632 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001633 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001634 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001635
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001636 weston_matrix_init(&rotate->rotation);
1637 rotate->rotation.d[0] = dx / r;
1638 rotate->rotation.d[4] = -dy / r;
1639 rotate->rotation.d[1] = -rotate->rotation.d[4];
1640 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001641
1642 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001643 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001644 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001645 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001646 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001647
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001648 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001649 &shsurf->surface->geometry.transformation_list,
1650 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001651 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001652 wl_list_init(&shsurf->rotation.transform.link);
1653 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001654 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001655 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001656
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001657 /* We need to adjust the position of the surface
1658 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001659 cposx = surface->geometry.x + cx;
1660 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001661 dposx = rotate->center.x - cposx;
1662 dposy = rotate->center.y - cposy;
1663 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001664 weston_surface_set_position(surface,
1665 surface->geometry.x + dposx,
1666 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001667 }
1668
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001669 /* Repaint implies weston_surface_update_transform(), which
1670 * lazily applies the damage due to rotation update.
1671 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001672 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001673}
1674
1675static void
Scott Moreau447013d2012-02-18 05:05:29 -07001676rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001677 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001678{
1679 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001680 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001681 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001682 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001683
1684 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001685 if (shsurf)
1686 weston_matrix_multiply(&shsurf->rotation.rotation,
1687 &rotate->rotation);
1688 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001689 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001690 free(rotate);
1691 }
1692}
1693
Scott Moreau447013d2012-02-18 05:05:29 -07001694static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001695 noop_grab_focus,
1696 rotate_grab_motion,
1697 rotate_grab_button,
1698};
1699
1700static void
1701rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001702 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001703{
1704 struct weston_surface *base_surface =
1705 (struct weston_surface *) device->pointer_focus;
1706 struct shell_surface *surface;
1707 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001708 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001709 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001710
1711 if (base_surface == NULL)
1712 return;
1713
1714 surface = get_shell_surface(base_surface);
1715 if (!surface)
1716 return;
1717
1718 switch (surface->type) {
1719 case SHELL_SURFACE_PANEL:
1720 case SHELL_SURFACE_BACKGROUND:
1721 case SHELL_SURFACE_FULLSCREEN:
1722 case SHELL_SURFACE_SCREENSAVER:
1723 return;
1724 default:
1725 break;
1726 }
1727
Pekka Paalanen460099f2012-01-20 16:48:25 +02001728 rotate = malloc(sizeof *rotate);
1729 if (!rotate)
1730 return;
1731
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001732 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001733
1734 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001735 surface->surface->geometry.width / 2,
1736 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001737 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001738
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001739 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001740
1741 dx = device->x - rotate->center.x;
1742 dy = device->y - rotate->center.y;
1743 r = sqrtf(dx * dx + dy * dy);
1744 if (r > 20.0f) {
1745 struct weston_matrix inverse;
1746
1747 weston_matrix_init(&inverse);
1748 inverse.d[0] = dx / r;
1749 inverse.d[4] = dy / r;
1750 inverse.d[1] = -inverse.d[4];
1751 inverse.d[5] = inverse.d[0];
1752 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001753
1754 weston_matrix_init(&rotate->rotation);
1755 rotate->rotation.d[0] = dx / r;
1756 rotate->rotation.d[4] = -dy / r;
1757 rotate->rotation.d[1] = -rotate->rotation.d[4];
1758 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001759 } else {
1760 weston_matrix_init(&surface->rotation.rotation);
1761 weston_matrix_init(&rotate->rotation);
1762 }
1763
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001764 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001765}
1766
1767static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001768activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001769 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001770{
Alex Wu21858432012-04-01 20:13:08 +08001771 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001772
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001774
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001775 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001776 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001777 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001778 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001779 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001780
Pekka Paalanen77346a62011-11-30 16:26:35 +02001781 case SHELL_SURFACE_SCREENSAVER:
1782 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001783 if (shell->lock_surface)
1784 weston_surface_restack(es,
1785 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001786 break;
Alex Wu4539b082012-03-01 12:57:46 +08001787 case SHELL_SURFACE_FULLSCREEN:
1788 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001789 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001790 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001791 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001792 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001793 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001794 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1795 wl_list_for_each_reverse_safe(surf,
1796 prev,
1797 &shell->fullscreen_layer.surface_list,
1798 layer_link)
1799 weston_surface_restack(surf,
1800 &shell->toplevel_layer.surface_list);
1801 }
1802
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001803 weston_surface_restack(es,
1804 &shell->toplevel_layer.surface_list);
1805 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001806 }
1807}
1808
Alex Wu21858432012-04-01 20:13:08 +08001809/* no-op func for checking black surface */
1810static void
1811black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1812{
1813}
1814
1815static bool
1816is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1817{
1818 if (es->configure == black_surface_configure) {
1819 if (fs_surface)
1820 *fs_surface = (struct weston_surface *)es->private;
1821 return true;
1822 }
1823 return false;
1824}
1825
Kristian Høgsberg75840622011-09-06 13:48:16 -04001826static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001827click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001828 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001829 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001830{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001831 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001832 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001834 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001835
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001836 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001837 if (!focus)
1838 return;
1839
Alex Wu21858432012-04-01 20:13:08 +08001840 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001841 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001842
Alex Wu9c35e6b2012-03-05 14:13:13 +08001843 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001844 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001845}
1846
1847static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001848lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001849{
Tiago Vignattibe143262012-04-16 17:31:41 +03001850 struct desktop_shell *shell =
1851 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001852 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001853 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001854 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001855
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001856 if (shell->locked) {
1857 wl_list_for_each(output, &shell->compositor->output_list, link)
1858 /* TODO: find a way to jump to other DPMS levels */
1859 if (output->set_dpms)
1860 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001861 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001862 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001863
1864 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001865
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001866 /* Hide all surfaces by removing the fullscreen, panel and
1867 * toplevel layers. This way nothing else can show or receive
1868 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001869
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001870 wl_list_remove(&shell->panel_layer.link);
1871 wl_list_remove(&shell->toplevel_layer.link);
1872 wl_list_remove(&shell->fullscreen_layer.link);
1873 wl_list_insert(&shell->compositor->cursor_layer.link,
1874 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001875
Pekka Paalanen77346a62011-11-30 16:26:35 +02001876 launch_screensaver(shell);
1877
1878 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1879 show_screensaver(shell, shsurf);
1880
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001881 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001882 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001883 weston_compositor_wake(shell->compositor);
1884 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001885 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001886
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001887 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001888 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001889
1890 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001891 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1892 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001893 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001894 }
1895
1896 /* TODO: disable bindings that should not work while locked. */
1897
1898 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001899}
1900
1901static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001902unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001903{
Tiago Vignattibe143262012-04-16 17:31:41 +03001904 struct desktop_shell *shell =
1905 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001906
Pekka Paalanend81c2162011-11-16 13:47:34 +02001907 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001908 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001909 return;
1910 }
1911
1912 /* If desktop-shell client has gone away, unlock immediately. */
1913 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001914 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001915 return;
1916 }
1917
1918 if (shell->prepare_event_sent)
1919 return;
1920
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001921 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001922 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001923}
1924
1925static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001926center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001927{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001928 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001929 GLfloat x = (mode->width - surface->geometry.width) / 2;
1930 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001931
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001932 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001933}
1934
1935static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001936map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001937 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001938{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001939 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001940 struct shell_surface *shsurf;
1941 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001942 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001943 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001944
Pekka Paalanen77346a62011-11-30 16:26:35 +02001945 shsurf = get_shell_surface(surface);
1946 if (shsurf)
1947 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001948
Pekka Paalanen60921e52012-01-25 15:55:43 +02001949 surface->geometry.width = width;
1950 surface->geometry.height = height;
1951 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001952
1953 /* initial positioning, see also configure() */
1954 switch (surface_type) {
1955 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001956 weston_surface_set_position(surface, 10 + random() % 400,
1957 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001958 break;
1959 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001960 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001961 break;
Alex Wu4539b082012-03-01 12:57:46 +08001962 case SHELL_SURFACE_FULLSCREEN:
1963 shell_map_fullscreen(shsurf);
1964 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001965 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001966 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001967 panel_height = get_output_panel_height(shell,surface->output);
1968 weston_surface_set_position(surface, surface->output->x,
1969 surface->output->y + panel_height);
1970 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001971 case SHELL_SURFACE_LOCK:
1972 center_on_output(surface, get_default_output(compositor));
1973 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001974 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001975 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001976 case SHELL_SURFACE_NONE:
1977 weston_surface_set_position(surface,
1978 surface->geometry.x + sx,
1979 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001980 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001981 default:
1982 ;
1983 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001984
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001985 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001986 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001987 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001988 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001989 wl_list_insert(&shell->background_layer.surface_list,
1990 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001991 break;
1992 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001993 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001994 wl_list_insert(&shell->panel_layer.surface_list,
1995 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001996 break;
1997 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001998 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001999 wl_list_insert(&shell->lock_layer.surface_list,
2000 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002002 break;
2003 case SHELL_SURFACE_SCREENSAVER:
2004 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002005 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002006 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002007 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002008 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002009 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002010 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002011 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002012 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002013 case SHELL_SURFACE_POPUP:
2014 case SHELL_SURFACE_TRANSIENT:
2015 parent = shsurf->parent->surface;
2016 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2017 break;
Alex Wu4539b082012-03-01 12:57:46 +08002018 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002019 case SHELL_SURFACE_NONE:
2020 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002021 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002022 wl_list_insert(&shell->toplevel_layer.surface_list,
2023 &surface->layer_link);
2024 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002025 }
2026
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002027 if (surface_type != SHELL_SURFACE_NONE) {
2028 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002029 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2030 surface->output = shsurf->output;
2031 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002032
Juan Zhao7bb92f02011-12-15 11:31:51 -05002033 switch (surface_type) {
2034 case SHELL_SURFACE_TOPLEVEL:
2035 case SHELL_SURFACE_TRANSIENT:
2036 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002037 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002038 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002039 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002040 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002041 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002042 break;
2043 default:
2044 break;
2045 }
2046
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002047 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002048 {
2049 switch (shell->win_animation_type) {
2050 case ANIMATION_FADE:
2051 weston_fade_run(surface, NULL, NULL);
2052 break;
2053 case ANIMATION_ZOOM:
2054 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2055 break;
2056 default:
2057 break;
2058 }
2059 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002060}
2061
2062static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002063configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002064 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002065{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002066 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2067 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002068
Pekka Paalanen77346a62011-11-30 16:26:35 +02002069 shsurf = get_shell_surface(surface);
2070 if (shsurf)
2071 surface_type = shsurf->type;
2072
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002073 surface->geometry.x = x;
2074 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002075 surface->geometry.width = width;
2076 surface->geometry.height = height;
2077 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002078
2079 switch (surface_type) {
2080 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002081 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002082 break;
Alex Wu4539b082012-03-01 12:57:46 +08002083 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002084 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002085 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002086 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002087 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002088 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002089 surface->geometry.x = surface->output->x;
2090 surface->geometry.y = surface->output->y +
2091 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002092 break;
Alex Wu4539b082012-03-01 12:57:46 +08002093 case SHELL_SURFACE_TOPLEVEL:
2094 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002095 default:
2096 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002097 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002098
Alex Wu4539b082012-03-01 12:57:46 +08002099 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002100 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002101 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002102
2103 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2104 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002105 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2106 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002107 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002108}
2109
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002110static void
2111shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2112{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002113 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002114 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002115
2116 if (!weston_surface_is_mapped(es)) {
2117 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002118 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002119 es->geometry.width != es->buffer->width ||
2120 es->geometry.height != es->buffer->height) {
2121 GLfloat from_x, from_y;
2122 GLfloat to_x, to_y;
2123
2124 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2125 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2126 configure(shell, es,
2127 es->geometry.x + to_x - from_x,
2128 es->geometry.y + to_y - from_y,
2129 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002130 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002131 }
2132}
2133
Tiago Vignattibe143262012-04-16 17:31:41 +03002134static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002135
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002136static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002137desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002138{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002139 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002140 struct desktop_shell *shell =
2141 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002142
2143 shell->child.process.pid = 0;
2144 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002145
2146 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2147 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002148 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002149 shell->child.deathstamp = time;
2150 shell->child.deathcount = 0;
2151 }
2152
2153 shell->child.deathcount++;
2154 if (shell->child.deathcount > 5) {
2155 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2156 return;
2157 }
2158
2159 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2160 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002161}
2162
2163static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002164launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002165{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002166 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002167
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002168 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002169 &shell->child.process,
2170 shell_exe,
2171 desktop_shell_sigchld);
2172
2173 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002174 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002175 return 0;
2176}
2177
2178static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002179bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2180{
Tiago Vignattibe143262012-04-16 17:31:41 +03002181 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002182
2183 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002184 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002185}
2186
Kristian Høgsberg75840622011-09-06 13:48:16 -04002187static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002188unbind_desktop_shell(struct wl_resource *resource)
2189{
Tiago Vignattibe143262012-04-16 17:31:41 +03002190 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002191
2192 if (shell->locked)
2193 resume_desktop(shell);
2194
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002195 shell->child.desktop_shell = NULL;
2196 shell->prepare_event_sent = false;
2197 free(resource);
2198}
2199
2200static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002201bind_desktop_shell(struct wl_client *client,
2202 void *data, uint32_t version, uint32_t id)
2203{
Tiago Vignattibe143262012-04-16 17:31:41 +03002204 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002205 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002206
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002207 resource = wl_client_add_object(client, &desktop_shell_interface,
2208 &desktop_shell_implementation,
2209 id, shell);
2210
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002211 if (client == shell->child.client) {
2212 resource->destroy = unbind_desktop_shell;
2213 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002214 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002215 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002216
2217 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2218 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002219 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002220}
2221
Pekka Paalanen6e168112011-11-24 11:34:05 +02002222static void
2223screensaver_set_surface(struct wl_client *client,
2224 struct wl_resource *resource,
2225 struct wl_resource *shell_surface_resource,
2226 struct wl_resource *output_resource)
2227{
Tiago Vignattibe143262012-04-16 17:31:41 +03002228 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002229 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002230 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002231
Pekka Paalanen98262232011-12-01 10:42:22 +02002232 if (reset_shell_surface_type(surface))
2233 return;
2234
Pekka Paalanen77346a62011-11-30 16:26:35 +02002235 surface->type = SHELL_SURFACE_SCREENSAVER;
2236
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002237 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002238 surface->output = output;
2239 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002240}
2241
2242static const struct screensaver_interface screensaver_implementation = {
2243 screensaver_set_surface
2244};
2245
2246static void
2247unbind_screensaver(struct wl_resource *resource)
2248{
Tiago Vignattibe143262012-04-16 17:31:41 +03002249 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002250
Pekka Paalanen77346a62011-11-30 16:26:35 +02002251 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002252 free(resource);
2253}
2254
2255static void
2256bind_screensaver(struct wl_client *client,
2257 void *data, uint32_t version, uint32_t id)
2258{
Tiago Vignattibe143262012-04-16 17:31:41 +03002259 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002260 struct wl_resource *resource;
2261
2262 resource = wl_client_add_object(client, &screensaver_interface,
2263 &screensaver_implementation,
2264 id, shell);
2265
Pekka Paalanen77346a62011-11-30 16:26:35 +02002266 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002267 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002268 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002269 return;
2270 }
2271
2272 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2273 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002274 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002275}
2276
Kristian Høgsberg07045392012-02-19 18:52:44 -05002277struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002278 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002279 struct weston_surface *current;
2280 struct wl_listener listener;
2281 struct wl_keyboard_grab grab;
2282};
2283
2284static void
2285switcher_next(struct switcher *switcher)
2286{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002287 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002288 struct weston_surface *surface;
2289 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002290 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002291
2292 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002293 switch (get_shell_surface_type(surface)) {
2294 case SHELL_SURFACE_TOPLEVEL:
2295 case SHELL_SURFACE_FULLSCREEN:
2296 case SHELL_SURFACE_MAXIMIZED:
2297 if (first == NULL)
2298 first = surface;
2299 if (prev == switcher->current)
2300 next = surface;
2301 prev = surface;
2302 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002303 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002304 weston_surface_damage(surface);
2305 break;
2306 default:
2307 break;
2308 }
Alex Wu1659daa2012-04-01 20:13:09 +08002309
2310 if (is_black_surface(surface, NULL)) {
2311 surface->alpha = 64;
2312 surface->geometry.dirty = 1;
2313 weston_surface_damage(surface);
2314 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002315 }
2316
2317 if (next == NULL)
2318 next = first;
2319
Alex Wu07b26062012-03-12 16:06:01 +08002320 if (next == NULL)
2321 return;
2322
Kristian Høgsberg07045392012-02-19 18:52:44 -05002323 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002324 wl_signal_add(&next->surface.resource.destroy_signal,
2325 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002326
2327 switcher->current = next;
2328 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002329
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002330 shsurf = get_shell_surface(switcher->current);
2331 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2332 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002333}
2334
2335static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002336switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002337{
2338 struct switcher *switcher =
2339 container_of(listener, struct switcher, listener);
2340
2341 switcher_next(switcher);
2342}
2343
2344static void
2345switcher_destroy(struct switcher *switcher, uint32_t time)
2346{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002347 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002348 struct weston_surface *surface;
2349 struct weston_input_device *device =
2350 (struct weston_input_device *) switcher->grab.input_device;
2351
2352 wl_list_for_each(surface, &compositor->surface_list, link) {
2353 surface->alpha = 255;
2354 weston_surface_damage(surface);
2355 }
2356
Alex Wu07b26062012-03-12 16:06:01 +08002357 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002358 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002359 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002360 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002361 free(switcher);
2362}
2363
2364static void
2365switcher_key(struct wl_keyboard_grab *grab,
2366 uint32_t time, uint32_t key, int32_t state)
2367{
2368 struct switcher *switcher = container_of(grab, struct switcher, grab);
2369 struct weston_input_device *device =
2370 (struct weston_input_device *) grab->input_device;
2371
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002372 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002373 switcher_destroy(switcher, time);
2374 } else if (key == KEY_TAB && state) {
2375 switcher_next(switcher);
2376 }
2377};
2378
2379static const struct wl_keyboard_grab_interface switcher_grab = {
2380 switcher_key
2381};
2382
2383static void
2384switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002385 uint32_t key, uint32_t button, uint32_t axis,
2386 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002387{
Tiago Vignattibe143262012-04-16 17:31:41 +03002388 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002389 struct switcher *switcher;
2390
2391 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002392 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002393 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002394 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002395 wl_list_init(&switcher->listener.link);
2396
2397 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002398 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2399 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002400 switcher_next(switcher);
2401}
2402
Pekka Paalanen3c647232011-12-22 13:43:43 +02002403static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002404backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002405 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002406{
2407 struct weston_compositor *compositor = data;
2408 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002409 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002410
2411 /* TODO: we're limiting to simple use cases, where we assume just
2412 * control on the primary display. We'd have to extend later if we
2413 * ever get support for setting backlights on random desktop LCD
2414 * panels though */
2415 output = get_default_output(compositor);
2416 if (!output)
2417 return;
2418
2419 if (!output->set_backlight)
2420 return;
2421
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002422 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2423 backlight_new = output->backlight_current - 25;
2424 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2425 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002426
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002427 if (backlight_new < 5)
2428 backlight_new = 5;
2429 if (backlight_new > 255)
2430 backlight_new = 255;
2431
2432 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002433 output->set_backlight(output, output->backlight_current);
2434}
2435
2436static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002437debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002438 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002439{
Tiago Vignattibe143262012-04-16 17:31:41 +03002440 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002441 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002442 struct weston_surface *surface;
2443
2444 if (shell->debug_repaint_surface) {
2445 weston_surface_destroy(shell->debug_repaint_surface);
2446 shell->debug_repaint_surface = NULL;
2447 } else {
2448 surface = weston_surface_create(compositor);
2449 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2450 weston_surface_configure(surface, 0, 0, 8192, 8192);
2451 wl_list_insert(&compositor->fade_layer.surface_list,
2452 &surface->layer_link);
2453 weston_surface_assign_output(surface);
2454 pixman_region32_init(&surface->input);
2455
2456 /* Here's the dirty little trick that makes the
2457 * repaint debugging work: we force an
2458 * update_transform first to update dependent state
2459 * and clear the geometry.dirty bit. Then we clear
2460 * the surface damage so it only gets repainted
2461 * piecewise as we repaint other things. */
2462
2463 weston_surface_update_transform(surface);
2464 pixman_region32_fini(&surface->damage);
2465 pixman_region32_init(&surface->damage);
2466 shell->debug_repaint_surface = surface;
2467 }
2468}
2469
2470static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002471shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002472{
Tiago Vignattibe143262012-04-16 17:31:41 +03002473 struct desktop_shell *shell =
2474 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002475
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002476 if (shell->child.client)
2477 wl_client_destroy(shell->child.client);
2478
Pekka Paalanen3c647232011-12-22 13:43:43 +02002479 free(shell->screensaver.path);
2480 free(shell);
2481}
2482
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002483static void
2484shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2485{
2486 uint32_t mod;
2487
2488 /* fixed bindings */
2489 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2490 MODIFIER_CTRL | MODIFIER_ALT,
2491 terminate_binding, ec);
2492 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2493 click_to_activate_binding, shell);
2494 weston_compositor_add_binding(ec, 0, 0,
2495 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2496 MODIFIER_SUPER | MODIFIER_ALT,
2497 surface_opacity_binding, NULL);
2498 weston_compositor_add_binding(ec, 0, 0,
2499 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2500 MODIFIER_SUPER, zoom_binding, NULL);
2501
2502 /* configurable bindings */
2503 mod = shell->binding_modifier;
2504 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2505 move_binding, shell);
2506 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2507 resize_binding, shell);
2508 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2509 rotate_binding, NULL);
2510 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2511 switcher_binding, shell);
2512 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2513 backlight_binding, ec);
2514 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2515 backlight_binding, ec);
2516 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2517 backlight_binding, ec);
2518 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2519 backlight_binding, ec);
2520 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2521 debug_repaint_binding, shell);
2522}
2523
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002524int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002525shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002526
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002527WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002529{
Tiago Vignattibe143262012-04-16 17:31:41 +03002530 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002531
2532 shell = malloc(sizeof *shell);
2533 if (shell == NULL)
2534 return -1;
2535
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002536 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002537 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002538
2539 shell->destroy_listener.notify = shell_destroy;
2540 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2541 shell->lock_listener.notify = lock;
2542 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2543 shell->unlock_listener.notify = unlock;
2544 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002545 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002546 ec->shell_interface.create_shell_surface = create_shell_surface;
2547 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002548
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002549 wl_list_init(&shell->backgrounds);
2550 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002551 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002552
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002553 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2554 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2555 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2556 weston_layer_init(&shell->background_layer,
2557 &shell->toplevel_layer.link);
2558 wl_list_init(&shell->lock_layer.surface_list);
2559
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002560 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002561
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002562 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2563 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002564 return -1;
2565
Kristian Høgsberg75840622011-09-06 13:48:16 -04002566 if (wl_display_add_global(ec->wl_display,
2567 &desktop_shell_interface,
2568 shell, bind_desktop_shell) == NULL)
2569 return -1;
2570
Pekka Paalanen6e168112011-11-24 11:34:05 +02002571 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2572 shell, bind_screensaver) == NULL)
2573 return -1;
2574
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002575 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002576 if (launch_desktop_shell_process(shell) != 0)
2577 return -1;
2578
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002579 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002580
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002581 return 0;
2582}