blob: 36bea520f05490cd7a8a5db63408f75a7267018a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Juan Zhaoe10d2792012-04-25 19:09:52 +080039enum animation_type {
40 ANIMATION_NONE,
41
42 ANIMATION_ZOOM,
43 ANIMATION_FADE
44};
45
Tiago Vignattibe143262012-04-16 17:31:41 +030046struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050047 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040048
49 struct wl_listener lock_listener;
50 struct wl_listener unlock_listener;
51 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050053 struct weston_layer fullscreen_layer;
54 struct weston_layer panel_layer;
55 struct weston_layer toplevel_layer;
56 struct weston_layer background_layer;
57 struct weston_layer lock_layer;
58
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020059 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050060 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020063
64 unsigned deathcount;
65 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020066 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020067
68 bool locked;
69 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020070
Pekka Paalanen068ae942011-11-28 14:11:15 +020071 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050072 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073
74 struct wl_list backgrounds;
75 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020076
Pekka Paalanen77346a62011-11-30 16:26:35 +020077 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020078 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020079 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020080 struct wl_resource *binding;
81 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020083 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050084
Tiago Vignatti0b52d482012-04-20 18:54:25 +030085 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080086 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050087 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040088};
89
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020091 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050092
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093 SHELL_SURFACE_PANEL,
94 SHELL_SURFACE_BACKGROUND,
95 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020096 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050097
98 SHELL_SURFACE_TOPLEVEL,
99 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800101 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200103};
104
Scott Moreauff1db4a2012-04-17 19:06:18 -0600105struct ping_timer {
106 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600107 uint32_t serial;
108};
109
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_resource resource;
112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300116 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200117
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400118 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400119 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500120 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800121 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800122 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600123 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100124
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500125 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500127 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200128 } rotation;
129
130 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700131 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200133 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400135 struct wl_input_device *device;
136 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 } popup;
138
Alex Wu4539b082012-03-01 12:57:46 +0800139 struct {
140 enum wl_shell_surface_fullscreen_method type;
141 struct weston_transform transform; /* matrix from x, y */
142 uint32_t framerate;
143 struct weston_surface *black_surface;
144 } fullscreen;
145
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146 struct ping_timer *ping_timer;
147
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600148 struct {
149 struct weston_animation current;
150 int exists;
151 int fading_in;
152 uint32_t timestamp;
153 } unresponsive_animation;
154
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500155 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100157 struct wl_list link;
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,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100330 uint32_t time, uint32_t button, uint32_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) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300450 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600451 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
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400480static void
481shell_surface_set_title(struct wl_client *client,
482 struct wl_resource *resource, const char *title)
483{
484 struct shell_surface *shsurf = resource->data;
485
486 free(shsurf->title);
487 shsurf->title = strdup(title);
488}
489
490static void
491shell_surface_set_class(struct wl_client *client,
492 struct wl_resource *resource, const char *class)
493{
494 struct shell_surface *shsurf = resource->data;
495
496 free(shsurf->class);
497 shsurf->class = strdup(class);
498}
499
Scott Moreauff1db4a2012-04-17 19:06:18 -0600500static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400502 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500504 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300505 struct shell_surface *shsurf = get_shell_surface(es);
506
507 if (!shsurf)
508 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500509
510 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400511 if (!move)
512 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500513
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300514 shell_grab_init(&move->base, &move_grab_interface, shsurf);
515
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200516 move->dx = es->geometry.x - wd->input_device.grab_x;
517 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500518
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300519 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400520 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500521
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400522 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400523
524 return 0;
525}
526
527static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200528shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400529 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500531 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200532 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400533
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500534 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400535 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500536 wd->input_device.pointer_focus != &shsurf->surface->surface)
537 return;
538
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400539 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400540 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500541}
542
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500543struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300544 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500545 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200546 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500547};
548
549static void
Scott Moreau447013d2012-02-18 05:05:29 -0700550resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500551 uint32_t time, int32_t x, int32_t y)
552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500553 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500554 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200556 int32_t from_x, from_y;
557 int32_t to_x, to_y;
558
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300559 if (!resize->base.shsurf)
560 return;
561
562 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200563 device->grab_x, device->grab_y,
564 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300565 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200566 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500567
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200568 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200569 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200570 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200571 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500572 } else {
573 width = resize->width;
574 }
575
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200576 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200577 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200578 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200579 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500580 } else {
581 height = resize->height;
582 }
583
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300584 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500586}
587
588static void
Scott Moreau447013d2012-02-18 05:05:29 -0700589resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100590 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500591{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300592 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500593 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500594
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500595 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300596 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400597 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500598 free(grab);
599 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500600}
601
Scott Moreau447013d2012-02-18 05:05:29 -0700602static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500603 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500604 resize_grab_motion,
605 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500606};
607
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400608static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500609weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400610 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500611{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500613
Alex Wu4539b082012-03-01 12:57:46 +0800614 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
615 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500616
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200617 if (edges == 0 || edges > 15 ||
618 (edges & 3) == 3 || (edges & 12) == 12)
619 return 0;
620
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500621 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400622 if (!resize)
623 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500624
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300625 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
626
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500627 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200628 resize->width = shsurf->surface->geometry.width;
629 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400630
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300631 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400632 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500633
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400634 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400635
636 return 0;
637}
638
639static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200640shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400641 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200642 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400643{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500644 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200645 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400646
Alex Wu4539b082012-03-01 12:57:46 +0800647 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
648 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400649
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500650 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400651 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500652 wd->input_device.pointer_focus != &shsurf->surface->surface)
653 return;
654
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400655 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400656 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500657}
658
Juan Zhao96879df2012-02-07 08:45:41 +0800659static struct weston_output *
660get_default_output(struct weston_compositor *compositor)
661{
662 return container_of(compositor->output_list.next,
663 struct weston_output, link);
664}
665
Alex Wu4539b082012-03-01 12:57:46 +0800666static void
667shell_unset_fullscreen(struct shell_surface *shsurf)
668{
669 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800670 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
671 shell_surface_is_top_fullscreen(shsurf)) {
672 weston_output_switch_mode(shsurf->fullscreen_output,
673 shsurf->fullscreen_output->origin);
674 }
Alex Wu4539b082012-03-01 12:57:46 +0800675 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
676 shsurf->fullscreen.framerate = 0;
677 wl_list_remove(&shsurf->fullscreen.transform.link);
678 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800679 if (shsurf->fullscreen.black_surface)
680 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800681 shsurf->fullscreen.black_surface = NULL;
682 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800683 weston_surface_set_position(shsurf->surface,
684 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800685 if (shsurf->saved_rotation_valid) {
686 wl_list_insert(&shsurf->surface->geometry.transformation_list,
687 &shsurf->rotation.transform.link);
688 shsurf->saved_rotation_valid = false;
689 }
Alex Wu4539b082012-03-01 12:57:46 +0800690}
691
Pekka Paalanen98262232011-12-01 10:42:22 +0200692static int
693reset_shell_surface_type(struct shell_surface *surface)
694{
695 switch (surface->type) {
696 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800697 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200698 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800699 case SHELL_SURFACE_MAXIMIZED:
700 surface->output = get_default_output(surface->surface->compositor);
701 weston_surface_set_position(surface->surface,
702 surface->saved_x,
703 surface->saved_y);
704 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200705 case SHELL_SURFACE_PANEL:
706 case SHELL_SURFACE_BACKGROUND:
707 wl_list_remove(&surface->link);
708 wl_list_init(&surface->link);
709 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200710 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200711 case SHELL_SURFACE_LOCK:
712 wl_resource_post_error(&surface->resource,
713 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200714 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200715 return -1;
716 case SHELL_SURFACE_NONE:
717 case SHELL_SURFACE_TOPLEVEL:
718 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500719 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200720 break;
721 }
722
723 surface->type = SHELL_SURFACE_NONE;
724 return 0;
725}
726
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500727static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400728set_surface_type(struct shell_surface *shsurf)
729{
730 struct weston_surface *surface = shsurf->surface;
731 struct shell_surface *pshsurf = shsurf->parent;
732 struct weston_surface *pes;
733 struct shell_surface *priv;
734 struct desktop_shell *shell = shsurf->shell;
735
736 reset_shell_surface_type(shsurf);
737
738 shsurf->type = shsurf->next_type;
739 shsurf->next_type = SHELL_SURFACE_NONE;
740
741 switch (shsurf->type) {
742 case SHELL_SURFACE_TOPLEVEL:
743 break;
744 case SHELL_SURFACE_TRANSIENT:
745 pes = pshsurf->surface;
746 weston_surface_set_position(surface,
747 pes->geometry.x + shsurf->popup.x,
748 pes->geometry.y + shsurf->popup.y);
749 break;
750
751 case SHELL_SURFACE_MAXIMIZED:
752 shsurf->saved_x = surface->geometry.x;
753 shsurf->saved_y = surface->geometry.y;
754 shsurf->saved_position_valid = true;
755 break;
756
757 case SHELL_SURFACE_FULLSCREEN:
758 shsurf->saved_x = surface->geometry.x;
759 shsurf->saved_y = surface->geometry.y;
760 shsurf->saved_position_valid = true;
761
762 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
763 wl_list_remove(&shsurf->rotation.transform.link);
764 wl_list_init(&shsurf->rotation.transform.link);
765 shsurf->surface->geometry.dirty = 1;
766 shsurf->saved_rotation_valid = true;
767 }
768 break;
769
770 case SHELL_SURFACE_BACKGROUND:
771 wl_list_for_each(priv, &shell->backgrounds, link) {
772 if (priv->output == shsurf->output) {
773 priv->surface->output = NULL;
774 wl_list_remove(&priv->surface->layer_link);
775 wl_list_remove(&priv->link);
776 break;
777 }
778 }
779
780 wl_list_insert(&shell->backgrounds, &shsurf->link);
781
782 weston_surface_set_position(surface, shsurf->output->x,
783 shsurf->output->y);
784 break;
785
786 case SHELL_SURFACE_PANEL:
787 wl_list_for_each(priv, &shell->panels, link) {
788 if (priv->output == shsurf->output) {
789 priv->surface->output = NULL;
790 wl_list_remove(&priv->surface->layer_link);
791 wl_list_remove(&priv->link);
792 break;
793 }
794 }
795
796 wl_list_insert(&shell->panels, &shsurf->link);
797
798 weston_surface_set_position(surface, shsurf->output->x,
799 shsurf->output->y);
800 break;
801
802 default:
803 break;
804 }
805}
806
807static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300808set_toplevel(struct shell_surface *shsurf)
809{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400810 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300811}
812
813static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200814shell_surface_set_toplevel(struct wl_client *client,
815 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400816{
Pekka Paalanen98262232011-12-01 10:42:22 +0200817 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400818
Tiago Vignattibc052c92012-04-19 16:18:18 +0300819 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400820}
821
822static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200823shell_surface_set_transient(struct wl_client *client,
824 struct wl_resource *resource,
825 struct wl_resource *parent_resource,
826 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400827{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200828 struct shell_surface *shsurf = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200829
Alex Wu4539b082012-03-01 12:57:46 +0800830 /* assign to parents output */
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400831 shsurf->parent = parent_resource->data;
832 shsurf->popup.x = x;
833 shsurf->popup.y = y;
834 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400835}
836
Tiago Vignattibe143262012-04-16 17:31:41 +0300837static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800838shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200839{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400840 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800841}
842
843static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300844get_output_panel_height(struct desktop_shell *shell,
845 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800846{
847 struct shell_surface *priv;
848 int panel_height = 0;
849
850 if (!output)
851 return 0;
852
Tiago Vignattibe143262012-04-16 17:31:41 +0300853 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800854 if (priv->output == output) {
855 panel_height = priv->surface->geometry.height;
856 break;
857 }
858 }
859 return panel_height;
860}
861
862static void
863shell_surface_set_maximized(struct wl_client *client,
864 struct wl_resource *resource,
865 struct wl_resource *output_resource )
866{
867 struct shell_surface *shsurf = resource->data;
868 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300869 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800870 uint32_t edges = 0, panel_height = 0;
871
872 /* get the default output, if the client set it as NULL
873 check whether the ouput is available */
874 if (output_resource)
875 shsurf->output = output_resource->data;
876 else
877 shsurf->output = get_default_output(es->compositor);
878
Tiago Vignattibe143262012-04-16 17:31:41 +0300879 shell = shell_surface_get_shell(shsurf);
880 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800881 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500882
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400883 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500884 es->output->current->width,
885 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800886
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400887 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200888}
889
Alex Wu21858432012-04-01 20:13:08 +0800890static void
891black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
892
Alex Wu4539b082012-03-01 12:57:46 +0800893static struct weston_surface *
894create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800895 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800896 GLfloat x, GLfloat y, int w, int h)
897{
898 struct weston_surface *surface = NULL;
899
900 surface = weston_surface_create(ec);
901 if (surface == NULL) {
902 fprintf(stderr, "no memory\n");
903 return NULL;
904 }
905
Alex Wu21858432012-04-01 20:13:08 +0800906 surface->configure = black_surface_configure;
907 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800908 weston_surface_configure(surface, x, y, w, h);
909 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
910 return surface;
911}
912
913/* Create black surface and append it to the associated fullscreen surface.
914 * Handle size dismatch and positioning according to the method. */
915static void
916shell_configure_fullscreen(struct shell_surface *shsurf)
917{
918 struct weston_output *output = shsurf->fullscreen_output;
919 struct weston_surface *surface = shsurf->surface;
920 struct weston_matrix *matrix;
921 float scale;
922
923 center_on_output(surface, output);
924
925 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500926 shsurf->fullscreen.black_surface =
927 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800928 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500929 output->x, output->y,
930 output->current->width,
931 output->current->height);
932
933 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
934 wl_list_insert(&surface->layer_link,
935 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800936 shsurf->fullscreen.black_surface->output = output;
937
938 switch (shsurf->fullscreen.type) {
939 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
940 break;
941 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
942 matrix = &shsurf->fullscreen.transform.matrix;
943 weston_matrix_init(matrix);
944 scale = (float)output->current->width/(float)surface->geometry.width;
945 weston_matrix_scale(matrix, scale, scale, 1);
946 wl_list_remove(&shsurf->fullscreen.transform.link);
947 wl_list_insert(surface->geometry.transformation_list.prev,
948 &shsurf->fullscreen.transform.link);
949 weston_surface_set_position(surface, output->x, output->y);
950 break;
951 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800952 if (shell_surface_is_top_fullscreen(shsurf)) {
953 struct weston_mode mode = {0,
954 surface->geometry.width,
955 surface->geometry.height,
956 shsurf->fullscreen.framerate};
957
958 if (weston_output_switch_mode(output, &mode) == 0) {
959 weston_surface_configure(shsurf->fullscreen.black_surface,
960 output->x, output->y,
961 output->current->width,
962 output->current->height);
963 weston_surface_set_position(surface, output->x, output->y);
964 break;
965 }
966 }
Alex Wu4539b082012-03-01 12:57:46 +0800967 break;
968 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
969 break;
970 default:
971 break;
972 }
973}
974
975/* make the fullscreen and black surface at the top */
976static void
977shell_stack_fullscreen(struct shell_surface *shsurf)
978{
Alex Wubd3354b2012-04-17 17:20:49 +0800979 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800980 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300981 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800982
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500983 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500984 wl_list_insert(&shell->fullscreen_layer.surface_list,
985 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800986 weston_surface_damage(surface);
987
988 if (!shsurf->fullscreen.black_surface)
989 shsurf->fullscreen.black_surface =
990 create_black_surface(surface->compositor,
991 surface,
992 output->x, output->y,
993 output->current->width,
994 output->current->height);
995
996 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500997 wl_list_insert(&surface->layer_link,
998 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500999 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001000}
1001
1002static void
1003shell_map_fullscreen(struct shell_surface *shsurf)
1004{
Alex Wu4539b082012-03-01 12:57:46 +08001005 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001006 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001007}
1008
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001009static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001010shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001011 struct wl_resource *resource,
1012 uint32_t method,
1013 uint32_t framerate,
1014 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001015{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001016 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001017 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001018
1019 if (output_resource)
1020 shsurf->output = output_resource->data;
1021 else
1022 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001023
Alex Wu4539b082012-03-01 12:57:46 +08001024 shsurf->fullscreen_output = shsurf->output;
1025 shsurf->fullscreen.type = method;
1026 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001027 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001028
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001029 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001030 shsurf->output->current->width,
1031 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001032}
1033
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001034static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001035popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001036 struct wl_surface *surface, int32_t x, int32_t y)
1037{
1038 struct wl_input_device *device = grab->input_device;
1039 struct shell_surface *priv =
1040 container_of(grab, struct shell_surface, popup.grab);
1041 struct wl_client *client = priv->surface->surface.resource.client;
1042
Pekka Paalanencb108432012-01-19 16:25:40 +02001043 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001044 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001045 grab->focus = surface;
1046 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001047 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001048 grab->focus = NULL;
1049 }
1050}
1051
1052static void
Scott Moreau447013d2012-02-18 05:05:29 -07001053popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001054 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001055{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001056 struct wl_resource *resource;
1057
1058 resource = grab->input_device->pointer_focus_resource;
1059 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001060 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001061}
1062
1063static void
Scott Moreau447013d2012-02-18 05:05:29 -07001064popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001065 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001066{
1067 struct wl_resource *resource;
1068 struct shell_surface *shsurf =
1069 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001070 struct wl_display *display;
1071 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001072
1073 resource = grab->input_device->pointer_focus_resource;
1074 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001075 display = wl_client_get_display(resource->client);
1076 serial = wl_display_get_serial(display);
1077 wl_input_device_send_button(resource, serial,
1078 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001079 } else if (state == 0 &&
1080 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001081 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001082 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001083 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001084 shsurf->popup.grab.input_device = NULL;
1085 }
1086
1087 if (state == 0)
1088 shsurf->popup.initial_up = 1;
1089}
1090
Scott Moreau447013d2012-02-18 05:05:29 -07001091static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001092 popup_grab_focus,
1093 popup_grab_motion,
1094 popup_grab_button,
1095};
1096
1097static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001098shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001099{
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001100 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001101 struct weston_surface *es = shsurf->surface;
1102 struct weston_surface *parent = shsurf->parent->surface;
1103
1104 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001105 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001106
Pekka Paalanen938269a2012-02-07 14:19:01 +02001107 weston_surface_update_transform(parent);
1108 if (parent->transform.enabled) {
1109 shsurf->popup.parent_transform.matrix =
1110 parent->transform.matrix;
1111 } else {
1112 /* construct x, y translation matrix */
1113 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1114 shsurf->popup.parent_transform.matrix.d[12] =
1115 parent->geometry.x;
1116 shsurf->popup.parent_transform.matrix.d[13] =
1117 parent->geometry.y;
1118 }
1119 wl_list_insert(es->geometry.transformation_list.prev,
1120 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001121 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001122
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001123 shsurf->popup.initial_up = 0;
1124
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001125 /* We don't require the grab to still be active, but if another
1126 * grab has started in the meantime, we end the popup now. */
1127 if (device->grab_serial == shsurf->popup.serial) {
1128 wl_input_device_start_pointer_grab(device,
1129 &shsurf->popup.grab);
1130 } else {
1131 wl_shell_surface_send_popup_done(&shsurf->resource);
1132 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001133}
1134
1135static void
1136shell_surface_set_popup(struct wl_client *client,
1137 struct wl_resource *resource,
1138 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001139 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001140 struct wl_resource *parent_resource,
1141 int32_t x, int32_t y, uint32_t flags)
1142{
1143 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001144
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001145 shsurf->type = SHELL_SURFACE_POPUP;
1146 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001147 shsurf->popup.device = input_device_resource->data;
1148 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001149 shsurf->popup.x = x;
1150 shsurf->popup.y = y;
1151}
1152
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001153static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001154 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001155 shell_surface_move,
1156 shell_surface_resize,
1157 shell_surface_set_toplevel,
1158 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001159 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001160 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001161 shell_surface_set_maximized,
1162 shell_surface_set_title,
1163 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001164};
1165
1166static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001167destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001169 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001170 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001171
Alex Wubd3354b2012-04-17 17:20:49 +08001172 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1173 shell_surface_is_top_fullscreen(shsurf)) {
1174 weston_output_switch_mode(shsurf->fullscreen_output,
1175 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001176 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177
Alex Wuaa08e2d2012-03-05 11:01:40 +08001178 if (shsurf->fullscreen.black_surface)
1179 weston_surface_destroy(shsurf->fullscreen.black_surface);
1180
Alex Wubd3354b2012-04-17 17:20:49 +08001181 /* As destroy_resource() use wl_list_for_each_safe(),
1182 * we can always remove the listener.
1183 */
1184 wl_list_remove(&shsurf->surface_destroy_listener.link);
1185 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001186 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001187
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001188 wl_list_remove(&shsurf->link);
1189 free(shsurf);
1190}
1191
1192static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001193shell_destroy_shell_surface(struct wl_resource *resource)
1194{
1195 struct shell_surface *shsurf = resource->data;
1196
1197 destroy_shell_surface(shsurf);
1198}
1199
1200static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001201shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001202{
1203 struct shell_surface *shsurf = container_of(listener,
1204 struct shell_surface,
1205 surface_destroy_listener);
1206
Tiago Vignattibc052c92012-04-19 16:18:18 +03001207 /* tricky way to check if resource was in fact created */
1208 if (shsurf->resource.object.implementation != 0)
1209 wl_resource_destroy(&shsurf->resource);
1210 else
1211 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001212}
1213
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001214static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001215get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001216{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001217 struct wl_listener *listener;
1218
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001219 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1220 shell_handle_surface_destroy);
1221 if (listener)
1222 return container_of(listener, struct shell_surface,
1223 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001224
1225 return NULL;
1226}
1227
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001228static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001229shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1230
1231static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001232create_shell_surface(void *shell, struct weston_surface *surface,
1233 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001234{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001235 struct shell_surface *shsurf;
1236
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001237 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001238 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001239 return;
1240 }
1241
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001242 shsurf = calloc(1, sizeof *shsurf);
1243 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001244 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001245 return;
1246 }
1247
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001248 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001249 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001250
Tiago Vignattibc052c92012-04-19 16:18:18 +03001251 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001252 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001253 shsurf->unresponsive_animation.exists = 0;
1254 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001255 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001256 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001257 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001258 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001259 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1260 shsurf->fullscreen.framerate = 0;
1261 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001262 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001263 wl_list_init(&shsurf->fullscreen.transform.link);
1264
Tiago Vignattibc052c92012-04-19 16:18:18 +03001265 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001266 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1267 wl_signal_add(&surface->surface.resource.destroy_signal,
1268 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001269
1270 /* init link so its safe to always remove it in destroy_shell_surface */
1271 wl_list_init(&shsurf->link);
1272
Pekka Paalanen460099f2012-01-20 16:48:25 +02001273 /* empty when not in use */
1274 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001275 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001276
Pekka Paalanen98262232011-12-01 10:42:22 +02001277 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001278 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001279
Tiago Vignattibc052c92012-04-19 16:18:18 +03001280 *ret = shsurf;
1281}
1282
1283static void
1284shell_get_shell_surface(struct wl_client *client,
1285 struct wl_resource *resource,
1286 uint32_t id,
1287 struct wl_resource *surface_resource)
1288{
1289 struct weston_surface *surface = surface_resource->data;
1290 struct desktop_shell *shell = resource->data;
1291 struct shell_surface *shsurf;
1292
1293 if (get_shell_surface(surface)) {
1294 wl_resource_post_error(surface_resource,
1295 WL_DISPLAY_ERROR_INVALID_OBJECT,
1296 "desktop_shell::get_shell_surface already requested");
1297 return;
1298 }
1299
1300 create_shell_surface(shell, surface, &shsurf);
1301 if (!shsurf) {
1302 wl_resource_post_error(surface_resource,
1303 WL_DISPLAY_ERROR_INVALID_OBJECT,
1304 "surface->configure already set");
1305 return;
1306 }
1307
1308 shsurf->resource.destroy = shell_destroy_shell_surface;
1309 shsurf->resource.object.id = id;
1310 shsurf->resource.object.interface = &wl_shell_surface_interface;
1311 shsurf->resource.object.implementation =
1312 (void (**)(void)) &shell_surface_implementation;
1313 shsurf->resource.data = shsurf;
1314
1315 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001316}
1317
1318static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001319 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001320};
1321
Kristian Høgsberg07937562011-04-12 17:25:42 -04001322static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001323handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001324{
1325 proc->pid = 0;
1326}
1327
1328static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001329launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001330{
1331 if (shell->screensaver.binding)
1332 return;
1333
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001334 if (!shell->screensaver.path)
1335 return;
1336
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001337 if (shell->screensaver.process.pid != 0) {
1338 fprintf(stderr, "old screensaver still running\n");
1339 return;
1340 }
1341
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001342 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001343 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001344 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001345 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001346}
1347
1348static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001349terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001350{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001351 if (shell->screensaver.process.pid == 0)
1352 return;
1353
1354 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001355}
1356
1357static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001358show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001359{
1360 struct wl_list *list;
1361
1362 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001363 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001364 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001365 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001366
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001367 wl_list_remove(&surface->surface->layer_link);
1368 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001369 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001370 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001371}
1372
1373static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001374hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001375{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001376 wl_list_remove(&surface->surface->layer_link);
1377 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001378 surface->surface->output = NULL;
1379}
1380
1381static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001382desktop_shell_set_background(struct wl_client *client,
1383 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001384 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001385 struct wl_resource *surface_resource)
1386{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001387 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001388
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001389 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001390 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001391
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001392 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001393 surface_resource,
1394 shsurf->output->current->width,
1395 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001396}
1397
1398static void
1399desktop_shell_set_panel(struct wl_client *client,
1400 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001401 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001402 struct wl_resource *surface_resource)
1403{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001404 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001405
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001406 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001407 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001408
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001409 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001410 surface_resource,
1411 shsurf->output->current->width,
1412 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001413}
1414
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001415static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001416handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001417{
Tiago Vignattibe143262012-04-16 17:31:41 +03001418 struct desktop_shell *shell =
1419 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001420
1421 fprintf(stderr, "lock surface gone\n");
1422 shell->lock_surface = NULL;
1423}
1424
1425static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001426desktop_shell_set_lock_surface(struct wl_client *client,
1427 struct wl_resource *resource,
1428 struct wl_resource *surface_resource)
1429{
Tiago Vignattibe143262012-04-16 17:31:41 +03001430 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001431 struct shell_surface *surface = surface_resource->data;
1432
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001433 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001434
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001435 if (!shell->locked)
1436 return;
1437
Pekka Paalanen98262232011-12-01 10:42:22 +02001438 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001439
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001440 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1441 wl_signal_add(&surface_resource->destroy_signal,
1442 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001443
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001444 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001445}
1446
1447static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001448resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001449{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001450 struct shell_surface *tmp;
1451
1452 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1453 hide_screensaver(shell, tmp);
1454
1455 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001456
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001457 wl_list_remove(&shell->lock_layer.link);
1458 wl_list_insert(&shell->compositor->cursor_layer.link,
1459 &shell->fullscreen_layer.link);
1460 wl_list_insert(&shell->fullscreen_layer.link,
1461 &shell->panel_layer.link);
1462 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001463
1464 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001465 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001467 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001468}
1469
1470static void
1471desktop_shell_unlock(struct wl_client *client,
1472 struct wl_resource *resource)
1473{
Tiago Vignattibe143262012-04-16 17:31:41 +03001474 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001475
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001476 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001477
1478 if (shell->locked)
1479 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001480}
1481
Kristian Høgsberg75840622011-09-06 13:48:16 -04001482static const struct desktop_shell_interface desktop_shell_implementation = {
1483 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001484 desktop_shell_set_panel,
1485 desktop_shell_set_lock_surface,
1486 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001487};
1488
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001489static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001491{
1492 struct shell_surface *shsurf;
1493
1494 shsurf = get_shell_surface(surface);
1495 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001496 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001497 return shsurf->type;
1498}
1499
Kristian Høgsberg75840622011-09-06 13:48:16 -04001500static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001501move_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001502 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1503 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001504{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001505 struct weston_surface *surface =
1506 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001507
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001508 if (surface == NULL)
1509 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001510
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001511 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001512 case SHELL_SURFACE_PANEL:
1513 case SHELL_SURFACE_BACKGROUND:
1514 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001515 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001516 return;
1517 default:
1518 break;
1519 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001520
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001521 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001522}
1523
1524static void
1525resize_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001526 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1527 void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001528{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529 struct weston_surface *surface =
1530 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001531 uint32_t edges = 0;
1532 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001533 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001534
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001535 if (surface == NULL)
1536 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001537
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001538 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001539 if (!shsurf)
1540 return;
1541
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001542 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001543 case SHELL_SURFACE_PANEL:
1544 case SHELL_SURFACE_BACKGROUND:
1545 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001546 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001547 return;
1548 default:
1549 break;
1550 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001551
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001552 weston_surface_from_global(surface,
1553 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001554
Pekka Paalanen60921e52012-01-25 15:55:43 +02001555 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001556 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001557 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001558 edges |= 0;
1559 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001560 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001561
Pekka Paalanen60921e52012-01-25 15:55:43 +02001562 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001563 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001564 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001565 edges |= 0;
1566 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001567 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001568
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001569 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001570 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001571}
1572
1573static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001574surface_opacity_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001575 uint32_t key, uint32_t button, uint32_t axis,
1576 int32_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001577{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001578 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001579 struct shell_surface *shsurf;
1580 struct weston_surface *surface =
1581 (struct weston_surface *) device->pointer_focus;
1582
1583 if (surface == NULL)
1584 return;
1585
1586 shsurf = get_shell_surface(surface);
1587 if (!shsurf)
1588 return;
1589
1590 switch (shsurf->type) {
1591 case SHELL_SURFACE_BACKGROUND:
1592 case SHELL_SURFACE_SCREENSAVER:
1593 return;
1594 default:
1595 break;
1596 }
1597
1598 surface->alpha += value * step;
1599
1600 if (surface->alpha > 255)
1601 surface->alpha = 255;
1602 if (surface->alpha < step)
1603 surface->alpha = step;
1604
1605 surface->geometry.dirty = 1;
1606 weston_surface_damage(surface);
1607}
1608
1609static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001610zoom_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001611 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1612 void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001613{
1614 struct weston_input_device *wd = (struct weston_input_device *) device;
1615 struct weston_compositor *compositor = wd->compositor;
1616 struct weston_output *output;
1617
1618 wl_list_for_each(output, &compositor->output_list, link) {
1619 if (pixman_region32_contains_point(&output->region,
1620 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001621 output->zoom.active = 1;
1622 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001623
1624 if (output->zoom.level >= 1.0) {
1625 output->zoom.active = 0;
1626 output->zoom.level = 1.0;
1627 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001628
1629 if (output->zoom.level < output->zoom.increment)
1630 output->zoom.level = output->zoom.increment;
1631
1632 weston_output_update_zoom(output, device->x, device->y);
1633 }
1634 }
1635}
1636
Scott Moreauccbf29d2012-02-22 14:21:41 -07001637static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001638terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001639 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001640{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001641 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001642
1643 if (state)
1644 wl_display_terminate(compositor->wl_display);
1645}
1646
1647static void
Scott Moreau447013d2012-02-18 05:05:29 -07001648rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001649 uint32_t time, int32_t x, int32_t y)
1650{
1651 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001652 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001653 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001654 struct shell_surface *shsurf = rotate->base.shsurf;
1655 struct weston_surface *surface;
1656 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1657
1658 if (!shsurf)
1659 return;
1660
1661 surface = shsurf->surface;
1662
1663 cx = 0.5f * surface->geometry.width;
1664 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001665
1666 dx = device->x - rotate->center.x;
1667 dy = device->y - rotate->center.y;
1668 r = sqrtf(dx * dx + dy * dy);
1669
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001670 wl_list_remove(&shsurf->rotation.transform.link);
1671 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001672
1673 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001674 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001675 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001676
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001677 weston_matrix_init(&rotate->rotation);
1678 rotate->rotation.d[0] = dx / r;
1679 rotate->rotation.d[4] = -dy / r;
1680 rotate->rotation.d[1] = -rotate->rotation.d[4];
1681 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001682
1683 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001684 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001685 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001686 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001687 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001688
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001689 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001690 &shsurf->surface->geometry.transformation_list,
1691 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001692 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001693 wl_list_init(&shsurf->rotation.transform.link);
1694 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001695 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001696 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001697
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001698 /* We need to adjust the position of the surface
1699 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001700 cposx = surface->geometry.x + cx;
1701 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001702 dposx = rotate->center.x - cposx;
1703 dposy = rotate->center.y - cposy;
1704 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001705 weston_surface_set_position(surface,
1706 surface->geometry.x + dposx,
1707 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001708 }
1709
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001710 /* Repaint implies weston_surface_update_transform(), which
1711 * lazily applies the damage due to rotation update.
1712 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001713 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001714}
1715
1716static void
Scott Moreau447013d2012-02-18 05:05:29 -07001717rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001718 uint32_t time, uint32_t button, uint32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001719{
1720 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001721 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001722 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001723 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001724
1725 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001726 if (shsurf)
1727 weston_matrix_multiply(&shsurf->rotation.rotation,
1728 &rotate->rotation);
1729 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001730 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001731 free(rotate);
1732 }
1733}
1734
Scott Moreau447013d2012-02-18 05:05:29 -07001735static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001736 noop_grab_focus,
1737 rotate_grab_motion,
1738 rotate_grab_button,
1739};
1740
1741static void
1742rotate_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01001743 uint32_t key, uint32_t button, uint32_t axis, int32_t value,
1744 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001745{
1746 struct weston_surface *base_surface =
1747 (struct weston_surface *) device->pointer_focus;
1748 struct shell_surface *surface;
1749 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001750 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001751 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001752
1753 if (base_surface == NULL)
1754 return;
1755
1756 surface = get_shell_surface(base_surface);
1757 if (!surface)
1758 return;
1759
1760 switch (surface->type) {
1761 case SHELL_SURFACE_PANEL:
1762 case SHELL_SURFACE_BACKGROUND:
1763 case SHELL_SURFACE_FULLSCREEN:
1764 case SHELL_SURFACE_SCREENSAVER:
1765 return;
1766 default:
1767 break;
1768 }
1769
Pekka Paalanen460099f2012-01-20 16:48:25 +02001770 rotate = malloc(sizeof *rotate);
1771 if (!rotate)
1772 return;
1773
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001774 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001775
1776 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001777 surface->surface->geometry.width / 2,
1778 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001779 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001780
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001781 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001782
1783 dx = device->x - rotate->center.x;
1784 dy = device->y - rotate->center.y;
1785 r = sqrtf(dx * dx + dy * dy);
1786 if (r > 20.0f) {
1787 struct weston_matrix inverse;
1788
1789 weston_matrix_init(&inverse);
1790 inverse.d[0] = dx / r;
1791 inverse.d[4] = dy / r;
1792 inverse.d[1] = -inverse.d[4];
1793 inverse.d[5] = inverse.d[0];
1794 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001795
1796 weston_matrix_init(&rotate->rotation);
1797 rotate->rotation.d[0] = dx / r;
1798 rotate->rotation.d[4] = -dy / r;
1799 rotate->rotation.d[1] = -rotate->rotation.d[4];
1800 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001801 } else {
1802 weston_matrix_init(&surface->rotation.rotation);
1803 weston_matrix_init(&rotate->rotation);
1804 }
1805
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001807}
1808
1809static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001810activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001811 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001812{
Alex Wu21858432012-04-01 20:13:08 +08001813 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001814
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001815 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001816
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001817 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001818 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001819 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001820 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001821 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001822
Pekka Paalanen77346a62011-11-30 16:26:35 +02001823 case SHELL_SURFACE_SCREENSAVER:
1824 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001825 if (shell->lock_surface)
1826 weston_surface_restack(es,
1827 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001828 break;
Alex Wu4539b082012-03-01 12:57:46 +08001829 case SHELL_SURFACE_FULLSCREEN:
1830 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001831 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001832 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001833 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001834 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001835 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001836 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1837 wl_list_for_each_reverse_safe(surf,
1838 prev,
1839 &shell->fullscreen_layer.surface_list,
1840 layer_link)
1841 weston_surface_restack(surf,
1842 &shell->toplevel_layer.surface_list);
1843 }
1844
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001845 weston_surface_restack(es,
1846 &shell->toplevel_layer.surface_list);
1847 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001848 }
1849}
1850
Alex Wu21858432012-04-01 20:13:08 +08001851/* no-op func for checking black surface */
1852static void
1853black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1854{
1855}
1856
1857static bool
1858is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1859{
1860 if (es->configure == black_surface_configure) {
1861 if (fs_surface)
1862 *fs_surface = (struct weston_surface *)es->private;
1863 return true;
1864 }
1865 return false;
1866}
1867
Kristian Høgsberg75840622011-09-06 13:48:16 -04001868static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001869click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001870 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001871 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001874 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001875 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001876 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001877
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001878 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001879 if (!focus)
1880 return;
1881
Alex Wu21858432012-04-01 20:13:08 +08001882 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001883 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001884
Alex Wu9c35e6b2012-03-05 14:13:13 +08001885 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001886 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001887}
1888
1889static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001890lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001891{
Tiago Vignattibe143262012-04-16 17:31:41 +03001892 struct desktop_shell *shell =
1893 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001894 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001895 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001896 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001897
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001898 if (shell->locked) {
1899 wl_list_for_each(output, &shell->compositor->output_list, link)
1900 /* TODO: find a way to jump to other DPMS levels */
1901 if (output->set_dpms)
1902 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001903 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001904 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001905
1906 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001907
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001908 /* Hide all surfaces by removing the fullscreen, panel and
1909 * toplevel layers. This way nothing else can show or receive
1910 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001911
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001912 wl_list_remove(&shell->panel_layer.link);
1913 wl_list_remove(&shell->toplevel_layer.link);
1914 wl_list_remove(&shell->fullscreen_layer.link);
1915 wl_list_insert(&shell->compositor->cursor_layer.link,
1916 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001917
Pekka Paalanen77346a62011-11-30 16:26:35 +02001918 launch_screensaver(shell);
1919
1920 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1921 show_screensaver(shell, shsurf);
1922
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001923 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001924 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001925 weston_compositor_wake(shell->compositor);
1926 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001927 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001928
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001929 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001930 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001931
1932 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001933 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1934 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001935 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001936 }
1937
1938 /* TODO: disable bindings that should not work while locked. */
1939
1940 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001941}
1942
1943static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001944unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001945{
Tiago Vignattibe143262012-04-16 17:31:41 +03001946 struct desktop_shell *shell =
1947 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001948
Pekka Paalanend81c2162011-11-16 13:47:34 +02001949 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001950 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001951 return;
1952 }
1953
1954 /* If desktop-shell client has gone away, unlock immediately. */
1955 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001956 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001957 return;
1958 }
1959
1960 if (shell->prepare_event_sent)
1961 return;
1962
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001963 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001964 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001965}
1966
1967static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001968center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001969{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001970 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001971 GLfloat x = (mode->width - surface->geometry.width) / 2;
1972 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001973
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001974 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001975}
1976
1977static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001978map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001979 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001980{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001981 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001982 struct shell_surface *shsurf;
1983 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001984 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001985 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001986
Pekka Paalanen77346a62011-11-30 16:26:35 +02001987 shsurf = get_shell_surface(surface);
1988 if (shsurf)
1989 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001990
Pekka Paalanen60921e52012-01-25 15:55:43 +02001991 surface->geometry.width = width;
1992 surface->geometry.height = height;
1993 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001994
1995 /* initial positioning, see also configure() */
1996 switch (surface_type) {
1997 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001998 weston_surface_set_position(surface, 10 + random() % 400,
1999 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002000 break;
2001 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002002 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002003 break;
Alex Wu4539b082012-03-01 12:57:46 +08002004 case SHELL_SURFACE_FULLSCREEN:
2005 shell_map_fullscreen(shsurf);
2006 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002007 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002008 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002009 panel_height = get_output_panel_height(shell,surface->output);
2010 weston_surface_set_position(surface, surface->output->x,
2011 surface->output->y + panel_height);
2012 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002013 case SHELL_SURFACE_LOCK:
2014 center_on_output(surface, get_default_output(compositor));
2015 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002016 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002017 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002018 case SHELL_SURFACE_NONE:
2019 weston_surface_set_position(surface,
2020 surface->geometry.x + sx,
2021 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002022 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002023 default:
2024 ;
2025 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002026
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002027 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002028 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002029 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002030 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002031 wl_list_insert(&shell->background_layer.surface_list,
2032 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002033 break;
2034 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002035 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002036 wl_list_insert(&shell->panel_layer.surface_list,
2037 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002038 break;
2039 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002040 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002041 wl_list_insert(&shell->lock_layer.surface_list,
2042 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002043 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002044 break;
2045 case SHELL_SURFACE_SCREENSAVER:
2046 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002047 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002048 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002049 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002051 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002052 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002053 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002054 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002055 case SHELL_SURFACE_POPUP:
2056 case SHELL_SURFACE_TRANSIENT:
2057 parent = shsurf->parent->surface;
2058 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2059 break;
Alex Wu4539b082012-03-01 12:57:46 +08002060 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002061 case SHELL_SURFACE_NONE:
2062 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002063 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002064 wl_list_insert(&shell->toplevel_layer.surface_list,
2065 &surface->layer_link);
2066 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002067 }
2068
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002069 if (surface_type != SHELL_SURFACE_NONE) {
2070 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002071 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2072 surface->output = shsurf->output;
2073 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002074
Juan Zhao7bb92f02011-12-15 11:31:51 -05002075 switch (surface_type) {
2076 case SHELL_SURFACE_TOPLEVEL:
2077 case SHELL_SURFACE_TRANSIENT:
2078 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002079 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002080 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002081 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002082 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002083 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002084 break;
2085 default:
2086 break;
2087 }
2088
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002089 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002090 {
2091 switch (shell->win_animation_type) {
2092 case ANIMATION_FADE:
2093 weston_fade_run(surface, NULL, NULL);
2094 break;
2095 case ANIMATION_ZOOM:
2096 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2097 break;
2098 default:
2099 break;
2100 }
2101 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002102}
2103
2104static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002105configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002106 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002107{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002108 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2109 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002110
Pekka Paalanen77346a62011-11-30 16:26:35 +02002111 shsurf = get_shell_surface(surface);
2112 if (shsurf)
2113 surface_type = shsurf->type;
2114
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002115 surface->geometry.x = x;
2116 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002117 surface->geometry.width = width;
2118 surface->geometry.height = height;
2119 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002120
2121 switch (surface_type) {
2122 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002123 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002124 break;
Alex Wu4539b082012-03-01 12:57:46 +08002125 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002126 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002127 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002128 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002129 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002130 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002131 surface->geometry.x = surface->output->x;
2132 surface->geometry.y = surface->output->y +
2133 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002134 break;
Alex Wu4539b082012-03-01 12:57:46 +08002135 case SHELL_SURFACE_TOPLEVEL:
2136 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002137 default:
2138 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002139 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002140
Alex Wu4539b082012-03-01 12:57:46 +08002141 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002142 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002143 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002144
2145 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2146 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002147 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2148 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002149 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002150}
2151
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002152static void
2153shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2154{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002155 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002156 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002157 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002158
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002159 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002160 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002161 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002162 type_changed = 1;
2163 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002164
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002165 if (!weston_surface_is_mapped(es)) {
2166 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002167 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002168 es->geometry.width != es->buffer->width ||
2169 es->geometry.height != es->buffer->height) {
2170 GLfloat from_x, from_y;
2171 GLfloat to_x, to_y;
2172
2173 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2174 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2175 configure(shell, es,
2176 es->geometry.x + to_x - from_x,
2177 es->geometry.y + to_y - from_y,
2178 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002179 }
2180}
2181
Tiago Vignattibe143262012-04-16 17:31:41 +03002182static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002183
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002184static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002185desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002186{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002187 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002188 struct desktop_shell *shell =
2189 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002190
2191 shell->child.process.pid = 0;
2192 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002193
2194 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2195 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002196 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002197 shell->child.deathstamp = time;
2198 shell->child.deathcount = 0;
2199 }
2200
2201 shell->child.deathcount++;
2202 if (shell->child.deathcount > 5) {
2203 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2204 return;
2205 }
2206
2207 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2208 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002209}
2210
2211static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002212launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002213{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002214 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002215
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002216 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002217 &shell->child.process,
2218 shell_exe,
2219 desktop_shell_sigchld);
2220
2221 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002222 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002223 return 0;
2224}
2225
2226static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002227bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2228{
Tiago Vignattibe143262012-04-16 17:31:41 +03002229 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002230
2231 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002232 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002233}
2234
Kristian Høgsberg75840622011-09-06 13:48:16 -04002235static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002236unbind_desktop_shell(struct wl_resource *resource)
2237{
Tiago Vignattibe143262012-04-16 17:31:41 +03002238 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002239
2240 if (shell->locked)
2241 resume_desktop(shell);
2242
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002243 shell->child.desktop_shell = NULL;
2244 shell->prepare_event_sent = false;
2245 free(resource);
2246}
2247
2248static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002249bind_desktop_shell(struct wl_client *client,
2250 void *data, uint32_t version, uint32_t id)
2251{
Tiago Vignattibe143262012-04-16 17:31:41 +03002252 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002253 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002254
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002255 resource = wl_client_add_object(client, &desktop_shell_interface,
2256 &desktop_shell_implementation,
2257 id, shell);
2258
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002259 if (client == shell->child.client) {
2260 resource->destroy = unbind_desktop_shell;
2261 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002262 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002263 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002264
2265 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2266 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002267 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002268}
2269
Pekka Paalanen6e168112011-11-24 11:34:05 +02002270static void
2271screensaver_set_surface(struct wl_client *client,
2272 struct wl_resource *resource,
2273 struct wl_resource *shell_surface_resource,
2274 struct wl_resource *output_resource)
2275{
Tiago Vignattibe143262012-04-16 17:31:41 +03002276 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002277 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002278 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002279
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002280 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002281
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002282 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002283 surface->output = output;
2284 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002285}
2286
2287static const struct screensaver_interface screensaver_implementation = {
2288 screensaver_set_surface
2289};
2290
2291static void
2292unbind_screensaver(struct wl_resource *resource)
2293{
Tiago Vignattibe143262012-04-16 17:31:41 +03002294 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002295
Pekka Paalanen77346a62011-11-30 16:26:35 +02002296 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002297 free(resource);
2298}
2299
2300static void
2301bind_screensaver(struct wl_client *client,
2302 void *data, uint32_t version, uint32_t id)
2303{
Tiago Vignattibe143262012-04-16 17:31:41 +03002304 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002305 struct wl_resource *resource;
2306
2307 resource = wl_client_add_object(client, &screensaver_interface,
2308 &screensaver_implementation,
2309 id, shell);
2310
Pekka Paalanen77346a62011-11-30 16:26:35 +02002311 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002312 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002313 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002314 return;
2315 }
2316
2317 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2318 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002319 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002320}
2321
Kristian Høgsberg07045392012-02-19 18:52:44 -05002322struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002323 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002324 struct weston_surface *current;
2325 struct wl_listener listener;
2326 struct wl_keyboard_grab grab;
2327};
2328
2329static void
2330switcher_next(struct switcher *switcher)
2331{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002332 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002333 struct weston_surface *surface;
2334 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002335 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002336
2337 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002338 switch (get_shell_surface_type(surface)) {
2339 case SHELL_SURFACE_TOPLEVEL:
2340 case SHELL_SURFACE_FULLSCREEN:
2341 case SHELL_SURFACE_MAXIMIZED:
2342 if (first == NULL)
2343 first = surface;
2344 if (prev == switcher->current)
2345 next = surface;
2346 prev = surface;
2347 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002348 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002349 weston_surface_damage(surface);
2350 break;
2351 default:
2352 break;
2353 }
Alex Wu1659daa2012-04-01 20:13:09 +08002354
2355 if (is_black_surface(surface, NULL)) {
2356 surface->alpha = 64;
2357 surface->geometry.dirty = 1;
2358 weston_surface_damage(surface);
2359 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002360 }
2361
2362 if (next == NULL)
2363 next = first;
2364
Alex Wu07b26062012-03-12 16:06:01 +08002365 if (next == NULL)
2366 return;
2367
Kristian Høgsberg07045392012-02-19 18:52:44 -05002368 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002369 wl_signal_add(&next->surface.resource.destroy_signal,
2370 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002371
2372 switcher->current = next;
2373 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002374
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002375 shsurf = get_shell_surface(switcher->current);
2376 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2377 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002378}
2379
2380static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002381switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002382{
2383 struct switcher *switcher =
2384 container_of(listener, struct switcher, listener);
2385
2386 switcher_next(switcher);
2387}
2388
2389static void
2390switcher_destroy(struct switcher *switcher, uint32_t time)
2391{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002392 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002393 struct weston_surface *surface;
2394 struct weston_input_device *device =
2395 (struct weston_input_device *) switcher->grab.input_device;
2396
2397 wl_list_for_each(surface, &compositor->surface_list, link) {
2398 surface->alpha = 255;
2399 weston_surface_damage(surface);
2400 }
2401
Alex Wu07b26062012-03-12 16:06:01 +08002402 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002403 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002404 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002405 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002406 free(switcher);
2407}
2408
2409static void
2410switcher_key(struct wl_keyboard_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01002411 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002412{
2413 struct switcher *switcher = container_of(grab, struct switcher, grab);
2414 struct weston_input_device *device =
2415 (struct weston_input_device *) grab->input_device;
2416
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002417 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002418 switcher_destroy(switcher, time);
2419 } else if (key == KEY_TAB && state) {
2420 switcher_next(switcher);
2421 }
2422};
2423
2424static const struct wl_keyboard_grab_interface switcher_grab = {
2425 switcher_key
2426};
2427
2428static void
2429switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002430 uint32_t key, uint32_t button, uint32_t axis,
Daniel Stonee5a01202012-05-04 11:21:57 +01002431 int32_t value, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002432{
Tiago Vignattibe143262012-04-16 17:31:41 +03002433 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002434 struct switcher *switcher;
2435
2436 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002437 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002438 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002439 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002440 wl_list_init(&switcher->listener.link);
2441
2442 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002443 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2444 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002445 switcher_next(switcher);
2446}
2447
Pekka Paalanen3c647232011-12-22 13:43:43 +02002448static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002449backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002450 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002451{
2452 struct weston_compositor *compositor = data;
2453 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002454 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002455
2456 /* TODO: we're limiting to simple use cases, where we assume just
2457 * control on the primary display. We'd have to extend later if we
2458 * ever get support for setting backlights on random desktop LCD
2459 * panels though */
2460 output = get_default_output(compositor);
2461 if (!output)
2462 return;
2463
2464 if (!output->set_backlight)
2465 return;
2466
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002467 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2468 backlight_new = output->backlight_current - 25;
2469 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2470 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002471
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002472 if (backlight_new < 5)
2473 backlight_new = 5;
2474 if (backlight_new > 255)
2475 backlight_new = 255;
2476
2477 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002478 output->set_backlight(output, output->backlight_current);
2479}
2480
2481static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002482debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Daniel Stonee5a01202012-05-04 11:21:57 +01002483 uint32_t key, uint32_t button, uint32_t axis,
2484 int32_t value, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002485{
Tiago Vignattibe143262012-04-16 17:31:41 +03002486 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002487 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002488 struct weston_surface *surface;
2489
2490 if (shell->debug_repaint_surface) {
2491 weston_surface_destroy(shell->debug_repaint_surface);
2492 shell->debug_repaint_surface = NULL;
2493 } else {
2494 surface = weston_surface_create(compositor);
2495 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2496 weston_surface_configure(surface, 0, 0, 8192, 8192);
2497 wl_list_insert(&compositor->fade_layer.surface_list,
2498 &surface->layer_link);
2499 weston_surface_assign_output(surface);
2500 pixman_region32_init(&surface->input);
2501
2502 /* Here's the dirty little trick that makes the
2503 * repaint debugging work: we force an
2504 * update_transform first to update dependent state
2505 * and clear the geometry.dirty bit. Then we clear
2506 * the surface damage so it only gets repainted
2507 * piecewise as we repaint other things. */
2508
2509 weston_surface_update_transform(surface);
2510 pixman_region32_fini(&surface->damage);
2511 pixman_region32_init(&surface->damage);
2512 shell->debug_repaint_surface = surface;
2513 }
2514}
2515
2516static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002517shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002518{
Tiago Vignattibe143262012-04-16 17:31:41 +03002519 struct desktop_shell *shell =
2520 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002521
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002522 if (shell->child.client)
2523 wl_client_destroy(shell->child.client);
2524
Pekka Paalanen3c647232011-12-22 13:43:43 +02002525 free(shell->screensaver.path);
2526 free(shell);
2527}
2528
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002529static void
2530shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2531{
2532 uint32_t mod;
2533
2534 /* fixed bindings */
2535 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2536 MODIFIER_CTRL | MODIFIER_ALT,
2537 terminate_binding, ec);
2538 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2539 click_to_activate_binding, shell);
2540 weston_compositor_add_binding(ec, 0, 0,
2541 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2542 MODIFIER_SUPER | MODIFIER_ALT,
2543 surface_opacity_binding, NULL);
2544 weston_compositor_add_binding(ec, 0, 0,
2545 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2546 MODIFIER_SUPER, zoom_binding, NULL);
2547
2548 /* configurable bindings */
2549 mod = shell->binding_modifier;
2550 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2551 move_binding, shell);
2552 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2553 resize_binding, shell);
2554 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2555 rotate_binding, NULL);
2556 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2557 switcher_binding, shell);
2558 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2559 backlight_binding, ec);
2560 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2561 backlight_binding, ec);
2562 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2563 backlight_binding, ec);
2564 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2565 backlight_binding, ec);
2566 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2567 debug_repaint_binding, shell);
2568}
2569
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002570int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002571shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002572
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002573WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002574shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002575{
Tiago Vignattibe143262012-04-16 17:31:41 +03002576 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002577
2578 shell = malloc(sizeof *shell);
2579 if (shell == NULL)
2580 return -1;
2581
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002582 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002583 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002584
2585 shell->destroy_listener.notify = shell_destroy;
2586 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2587 shell->lock_listener.notify = lock;
2588 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2589 shell->unlock_listener.notify = unlock;
2590 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002591 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002592 ec->shell_interface.create_shell_surface = create_shell_surface;
2593 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002594
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002595 wl_list_init(&shell->backgrounds);
2596 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002597 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002598
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002599 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2600 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2601 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2602 weston_layer_init(&shell->background_layer,
2603 &shell->toplevel_layer.link);
2604 wl_list_init(&shell->lock_layer.surface_list);
2605
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002606 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002607
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002608 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2609 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002610 return -1;
2611
Kristian Høgsberg75840622011-09-06 13:48:16 -04002612 if (wl_display_add_global(ec->wl_display,
2613 &desktop_shell_interface,
2614 shell, bind_desktop_shell) == NULL)
2615 return -1;
2616
Pekka Paalanen6e168112011-11-24 11:34:05 +02002617 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2618 shell, bind_screensaver) == NULL)
2619 return -1;
2620
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002621 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002622 if (launch_desktop_shell_process(shell) != 0)
2623 return -1;
2624
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002625 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002626
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002627 return 0;
2628}