blob: 879433c86f7ba9d0f5b07293537a7f6263387cd4 [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øgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050039
Juan Zhaoe10d2792012-04-25 19:09:52 +080040enum animation_type {
41 ANIMATION_NONE,
42
43 ANIMATION_ZOOM,
44 ANIMATION_FADE
45};
46
Tiago Vignattibe143262012-04-16 17:31:41 +030047struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050048 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040049
50 struct wl_listener lock_listener;
51 struct wl_listener unlock_listener;
52 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020053
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050054 struct weston_layer fullscreen_layer;
55 struct weston_layer panel_layer;
56 struct weston_layer toplevel_layer;
57 struct weston_layer background_layer;
58 struct weston_layer lock_layer;
59
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040060 struct wl_listener pointer_focus_listener;
61 struct weston_surface *busy_surface;
62
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020063 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050064 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020065 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020066 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020067
68 unsigned deathcount;
69 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020070 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020071
72 bool locked;
73 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020074
Pekka Paalanen068ae942011-11-28 14:11:15 +020075 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050076 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010077
78 struct wl_list backgrounds;
79 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020080
Pekka Paalanen77346a62011-11-30 16:26:35 +020081 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020082 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020083 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020084 struct wl_resource *binding;
85 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050086 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020087 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050088
Tiago Vignatti0b52d482012-04-20 18:54:25 +030089 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080090 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050091 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040092};
93
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050094enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020095 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050096
Pekka Paalanen57da4a82011-11-23 16:42:16 +020097 SHELL_SURFACE_PANEL,
98 SHELL_SURFACE_BACKGROUND,
99 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200100 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500101
102 SHELL_SURFACE_TOPLEVEL,
103 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500104 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800105 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500106 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200107};
108
Scott Moreauff1db4a2012-04-17 19:06:18 -0600109struct ping_timer {
110 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600111 uint32_t serial;
112};
113
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200114struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200115 struct wl_resource resource;
116
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500117 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200118 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500119 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400122 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800125 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800126 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600127 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200130 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500131 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200132 } rotation;
133
134 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700135 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200137 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500138 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100139 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400140 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 } popup;
142
Alex Wu4539b082012-03-01 12:57:46 +0800143 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300144 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400145 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300146 } transient;
147
148 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800149 enum wl_shell_surface_fullscreen_method type;
150 struct weston_transform transform; /* matrix from x, y */
151 uint32_t framerate;
152 struct weston_surface *black_surface;
153 } fullscreen;
154
Scott Moreauff1db4a2012-04-17 19:06:18 -0600155 struct ping_timer *ping_timer;
156
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500157 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500158 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100159 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400160
161 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200162};
163
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300164struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700165 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300166 struct shell_surface *shsurf;
167 struct wl_listener shsurf_destroy_listener;
168};
169
170struct weston_move_grab {
171 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100172 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500173};
174
Pekka Paalanen460099f2012-01-20 16:48:25 +0200175struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500177 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200178 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400179 GLfloat x;
180 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200181 } center;
182};
183
Alex Wubd3354b2012-04-17 17:20:49 +0800184static struct shell_surface *
185get_shell_surface(struct weston_surface *surface);
186
187static struct desktop_shell *
188shell_surface_get_shell(struct shell_surface *shsurf);
189
190static bool
191shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
192{
193 struct desktop_shell *shell;
194 struct weston_surface *top_fs_es;
195
196 shell = shell_surface_get_shell(shsurf);
197
198 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
199 return false;
200
201 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
202 struct weston_surface,
203 layer_link);
204 return (shsurf == get_shell_surface(top_fs_es));
205}
206
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500207static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400208destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209{
210 struct shell_grab *grab;
211
212 grab = container_of(listener, struct shell_grab,
213 shsurf_destroy_listener);
214
215 grab->shsurf = NULL;
216}
217
218static void
219shell_grab_init(struct shell_grab *grab,
220 const struct wl_pointer_grab_interface *interface,
221 struct shell_surface *shsurf)
222{
223 grab->grab.interface = interface;
224 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400225 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
226 wl_signal_add(&shsurf->resource.destroy_signal,
227 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228
229}
230
231static void
232shell_grab_finish(struct shell_grab *grab)
233{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400234 if (grab->shsurf)
235 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300236}
237
238static void
Alex Wu4539b082012-03-01 12:57:46 +0800239center_on_output(struct weston_surface *surface,
240 struct weston_output *output);
241
Daniel Stone496ca172012-05-30 16:31:42 +0100242static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300243get_modifier(char *modifier)
244{
245 if (!modifier)
246 return MODIFIER_SUPER;
247
248 if (!strcmp("ctrl", modifier))
249 return MODIFIER_CTRL;
250 else if (!strcmp("alt", modifier))
251 return MODIFIER_ALT;
252 else if (!strcmp("super", modifier))
253 return MODIFIER_SUPER;
254 else
255 return MODIFIER_SUPER;
256}
257
Juan Zhaoe10d2792012-04-25 19:09:52 +0800258static enum animation_type
259get_animation_type(char *animation)
260{
261 if (!animation)
262 return ANIMATION_NONE;
263
264 if (!strcmp("zoom", animation))
265 return ANIMATION_ZOOM;
266 else if (!strcmp("fade", animation))
267 return ANIMATION_FADE;
268 else
269 return ANIMATION_NONE;
270}
271
Alex Wu4539b082012-03-01 12:57:46 +0800272static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300273shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200274{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200275 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200276 char *path = NULL;
277 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300278 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800279 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200280
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400281 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300282 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800283 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200284 };
285
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400286 struct config_key saver_keys[] = {
287 { "path", CONFIG_KEY_STRING, &path },
288 { "duration", CONFIG_KEY_INTEGER, &duration },
289 };
290
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200291 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400292 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200293 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
294 };
295
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200296 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500297 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200298 free(config_file);
299
Pekka Paalanen7296e792011-12-07 16:22:00 +0200300 shell->screensaver.path = path;
301 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300302 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800303 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200304}
305
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200306static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400307noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100308 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500309{
310 grab->focus = NULL;
311}
312
313static void
Scott Moreau447013d2012-02-18 05:05:29 -0700314move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100315 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500316{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100318 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319 struct shell_surface *shsurf = move->base.shsurf;
320 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100321 int dx = wl_fixed_to_int(pointer->x + move->dx);
322 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300323
324 if (!shsurf)
325 return;
326
327 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500328
Daniel Stone103db7f2012-05-08 17:17:55 +0100329 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200330 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500331}
332
333static void
Scott Moreau447013d2012-02-18 05:05:29 -0700334move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100335 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500336{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
338 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100339 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100340 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500341
Daniel Stone4dbadb12012-05-30 16:31:51 +0100342 if (pointer->button_count == 0 &&
343 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300344 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100345 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500346 free(grab);
347 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500348}
349
Scott Moreau447013d2012-02-18 05:05:29 -0700350static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500351 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500352 move_grab_motion,
353 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500354};
355
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600356static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400357busy_cursor_grab_focus(struct wl_pointer_grab *base,
358 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600359{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400360 struct shell_grab *grab = (struct shell_grab *) base;
361 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600362
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400363 if (grab->grab.focus != surface) {
364 shell_grab_finish(grab);
365 wl_pointer_end_grab(pointer);
366 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600367 }
368}
369
370static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400371busy_cursor_grab_motion(struct wl_pointer_grab *grab,
372 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600373{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400374}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600375
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400376static void
377busy_cursor_grab_button(struct wl_pointer_grab *grab,
378 uint32_t time, uint32_t button, uint32_t state)
379{
380}
381
382static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
383 busy_cursor_grab_focus,
384 busy_cursor_grab_motion,
385 busy_cursor_grab_button,
386};
387
388static void
389set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
390{
391 struct shell_grab *grab;
392 struct desktop_shell *shell = shsurf->shell;
393 struct weston_seat *seat = (struct weston_seat *) pointer->seat;
394 struct weston_surface *sprite;
395
396 grab = malloc(sizeof *grab);
397 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600398 return;
399
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400400 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
401 grab->grab.focus = &shsurf->surface->surface;
402 wl_pointer_start_grab(pointer, &grab->grab);
403 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600404
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400405 sprite = (struct weston_surface *) seat->sprite;
406 shell->busy_surface->output = sprite->output;
407}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600408
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400409static void
410end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
411{
412 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
413
414 if (grab->grab.interface == &busy_cursor_grab_interface) {
415 shell_grab_finish(grab);
416 wl_pointer_end_grab(pointer);
417 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600418 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600419}
420
Scott Moreau9521d5e2012-04-19 13:06:17 -0600421static void
422ping_timer_destroy(struct shell_surface *shsurf)
423{
424 if (!shsurf || !shsurf->ping_timer)
425 return;
426
427 if (shsurf->ping_timer->source)
428 wl_event_source_remove(shsurf->ping_timer->source);
429
430 free(shsurf->ping_timer);
431 shsurf->ping_timer = NULL;
432}
433
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400434static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600435ping_timeout_handler(void *data)
436{
437 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400438 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600439
Scott Moreau9521d5e2012-04-19 13:06:17 -0600440 /* Client is not responding */
441 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400442
443 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
444 if (seat->seat.pointer->focus == &shsurf->surface->surface)
445 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600446
447 return 1;
448}
449
450static void
451ping_handler(struct weston_surface *surface, uint32_t serial)
452{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400453 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600454 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400455 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600456
457 if (!shsurf)
458 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400459 if (!shsurf->resource.client)
460 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600461
462 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300463 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600464 if (!shsurf->ping_timer)
465 return;
466
467 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600468 loop = wl_display_get_event_loop(surface->compositor->wl_display);
469 shsurf->ping_timer->source =
470 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
471 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
472
473 wl_shell_surface_send_ping(&shsurf->resource, serial);
474 }
475}
476
477static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400478handle_pointer_focus(struct wl_listener *listener, void *data)
479{
480 struct wl_pointer *pointer = data;
481 struct weston_surface *surface =
482 (struct weston_surface *) pointer->focus;
483 struct weston_compositor *compositor;
484 struct shell_surface *shsurf;
485 uint32_t serial;
486
487 if (!surface)
488 return;
489
490 compositor = surface->compositor;
491 shsurf = get_shell_surface(surface);
492
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300493 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400494 set_busy_cursor(shsurf, pointer);
495 } else {
496 serial = wl_display_next_serial(compositor->wl_display);
497 ping_handler(surface, serial);
498 }
499}
500
501static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600502shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
503 uint32_t serial)
504{
505 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400506 struct desktop_shell *shell = shsurf->shell;
507 struct weston_seat *seat;
508 struct weston_compositor *ec = shsurf->surface->compositor;
509 struct wl_pointer *pointer;
510 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600511
Scott Moreauff1db4a2012-04-17 19:06:18 -0600512 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400513 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600514 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400515 if (was_unresponsive) {
516 /* Received pong from previously unresponsive client */
517 wl_list_for_each(seat, &ec->seat_list, link) {
518 pointer = seat->seat.pointer;
519 if (pointer->focus ==
520 &shell->busy_surface->surface &&
521 pointer->current ==
522 &shsurf->surface->surface)
523 end_busy_cursor(shsurf, pointer);
524 }
525 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600526 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600527 }
528}
529
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400530static void
531shell_surface_set_title(struct wl_client *client,
532 struct wl_resource *resource, const char *title)
533{
534 struct shell_surface *shsurf = resource->data;
535
536 free(shsurf->title);
537 shsurf->title = strdup(title);
538}
539
540static void
541shell_surface_set_class(struct wl_client *client,
542 struct wl_resource *resource, const char *class)
543{
544 struct shell_surface *shsurf = resource->data;
545
546 free(shsurf->class);
547 shsurf->class = strdup(class);
548}
549
Scott Moreauff1db4a2012-04-17 19:06:18 -0600550static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400551surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500553 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300554
555 if (!shsurf)
556 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500557
558 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400559 if (!move)
560 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500561
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300562 shell_grab_init(&move->base, &move_grab_interface, shsurf);
563
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400564 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100565 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400566 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100567 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500568
Daniel Stone37816df2012-05-16 18:45:18 +0100569 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500570
Daniel Stone37816df2012-05-16 18:45:18 +0100571 wl_pointer_set_focus(ws->seat.pointer, NULL,
572 wl_fixed_from_int(0),
573 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400574
575 return 0;
576}
577
578static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200579shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100580 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400581{
Daniel Stone37816df2012-05-16 18:45:18 +0100582 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200583 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400584
Daniel Stone37816df2012-05-16 18:45:18 +0100585 if (ws->seat.pointer->button_count == 0 ||
586 ws->seat.pointer->grab_serial != serial ||
587 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500588 return;
589
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400590 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400591 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500592}
593
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500594struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300595 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500596 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200597 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500598};
599
600static void
Scott Moreau447013d2012-02-18 05:05:29 -0700601resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100602 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500603{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500604 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100605 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400606 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500607 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100608 wl_fixed_t from_x, from_y;
609 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200610
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400611 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300612 return;
613
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400614 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100615 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100616 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400617 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100618 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500619
Daniel Stone103db7f2012-05-08 17:17:55 +0100620 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200621 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100622 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200623 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100624 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500625 }
626
Daniel Stone103db7f2012-05-08 17:17:55 +0100627 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200628 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100629 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200630 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100631 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500632 }
633
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400634 shsurf->client->send_configure(shsurf->surface,
635 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500636}
637
638static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400639send_configure(struct weston_surface *surface,
640 uint32_t edges, int32_t width, int32_t height)
641{
642 struct shell_surface *shsurf = get_shell_surface(surface);
643
644 wl_shell_surface_send_configure(&shsurf->resource,
645 edges, width, height);
646}
647
648static const struct weston_shell_client shell_client = {
649 send_configure
650};
651
652static void
Scott Moreau447013d2012-02-18 05:05:29 -0700653resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100654 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500655{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300656 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100657 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100658 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500659
Daniel Stone4dbadb12012-05-30 16:31:51 +0100660 if (pointer->button_count == 0 &&
661 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300662 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100663 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500664 free(grab);
665 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500666}
667
Scott Moreau447013d2012-02-18 05:05:29 -0700668static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500669 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500670 resize_grab_motion,
671 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500672};
673
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400674static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400675surface_resize(struct shell_surface *shsurf,
676 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500677{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500678 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500679
Alex Wu4539b082012-03-01 12:57:46 +0800680 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
681 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500682
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200683 if (edges == 0 || edges > 15 ||
684 (edges & 3) == 3 || (edges & 12) == 12)
685 return 0;
686
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500687 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400688 if (!resize)
689 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500690
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300691 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
692
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500693 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200694 resize->width = shsurf->surface->geometry.width;
695 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400696
Daniel Stone37816df2012-05-16 18:45:18 +0100697 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500698
Daniel Stone37816df2012-05-16 18:45:18 +0100699 wl_pointer_set_focus(ws->seat.pointer, NULL,
700 wl_fixed_from_int(0),
701 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400702
703 return 0;
704}
705
706static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200707shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100708 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200709 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400710{
Daniel Stone37816df2012-05-16 18:45:18 +0100711 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200712 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400713
Alex Wu4539b082012-03-01 12:57:46 +0800714 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
715 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400716
Daniel Stone37816df2012-05-16 18:45:18 +0100717 if (ws->seat.pointer->button_count == 0 ||
718 ws->seat.pointer->grab_serial != serial ||
719 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500720 return;
721
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400722 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400723 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500724}
725
Juan Zhao96879df2012-02-07 08:45:41 +0800726static struct weston_output *
727get_default_output(struct weston_compositor *compositor)
728{
729 return container_of(compositor->output_list.next,
730 struct weston_output, link);
731}
732
Alex Wu4539b082012-03-01 12:57:46 +0800733static void
734shell_unset_fullscreen(struct shell_surface *shsurf)
735{
736 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800737 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
738 shell_surface_is_top_fullscreen(shsurf)) {
739 weston_output_switch_mode(shsurf->fullscreen_output,
740 shsurf->fullscreen_output->origin);
741 }
Alex Wu4539b082012-03-01 12:57:46 +0800742 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
743 shsurf->fullscreen.framerate = 0;
744 wl_list_remove(&shsurf->fullscreen.transform.link);
745 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800746 if (shsurf->fullscreen.black_surface)
747 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800748 shsurf->fullscreen.black_surface = NULL;
749 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800750 weston_surface_set_position(shsurf->surface,
751 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800752 if (shsurf->saved_rotation_valid) {
753 wl_list_insert(&shsurf->surface->geometry.transformation_list,
754 &shsurf->rotation.transform.link);
755 shsurf->saved_rotation_valid = false;
756 }
Alex Wu4539b082012-03-01 12:57:46 +0800757}
758
Pekka Paalanen98262232011-12-01 10:42:22 +0200759static int
760reset_shell_surface_type(struct shell_surface *surface)
761{
762 switch (surface->type) {
763 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800764 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200765 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800766 case SHELL_SURFACE_MAXIMIZED:
767 surface->output = get_default_output(surface->surface->compositor);
768 weston_surface_set_position(surface->surface,
769 surface->saved_x,
770 surface->saved_y);
771 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200772 case SHELL_SURFACE_PANEL:
773 case SHELL_SURFACE_BACKGROUND:
774 wl_list_remove(&surface->link);
775 wl_list_init(&surface->link);
776 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200777 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200778 case SHELL_SURFACE_LOCK:
779 wl_resource_post_error(&surface->resource,
780 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200781 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200782 return -1;
783 case SHELL_SURFACE_NONE:
784 case SHELL_SURFACE_TOPLEVEL:
785 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500786 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200787 break;
788 }
789
790 surface->type = SHELL_SURFACE_NONE;
791 return 0;
792}
793
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500794static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400795set_surface_type(struct shell_surface *shsurf)
796{
797 struct weston_surface *surface = shsurf->surface;
798 struct shell_surface *pshsurf = shsurf->parent;
799 struct weston_surface *pes;
800 struct shell_surface *priv;
801 struct desktop_shell *shell = shsurf->shell;
802
803 reset_shell_surface_type(shsurf);
804
805 shsurf->type = shsurf->next_type;
806 shsurf->next_type = SHELL_SURFACE_NONE;
807
808 switch (shsurf->type) {
809 case SHELL_SURFACE_TOPLEVEL:
810 break;
811 case SHELL_SURFACE_TRANSIENT:
812 pes = pshsurf->surface;
813 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300814 pes->geometry.x + shsurf->transient.x,
815 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400816 break;
817
818 case SHELL_SURFACE_MAXIMIZED:
819 shsurf->saved_x = surface->geometry.x;
820 shsurf->saved_y = surface->geometry.y;
821 shsurf->saved_position_valid = true;
822 break;
823
824 case SHELL_SURFACE_FULLSCREEN:
825 shsurf->saved_x = surface->geometry.x;
826 shsurf->saved_y = surface->geometry.y;
827 shsurf->saved_position_valid = true;
828
829 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
830 wl_list_remove(&shsurf->rotation.transform.link);
831 wl_list_init(&shsurf->rotation.transform.link);
832 shsurf->surface->geometry.dirty = 1;
833 shsurf->saved_rotation_valid = true;
834 }
835 break;
836
837 case SHELL_SURFACE_BACKGROUND:
838 wl_list_for_each(priv, &shell->backgrounds, link) {
839 if (priv->output == shsurf->output) {
840 priv->surface->output = NULL;
841 wl_list_remove(&priv->surface->layer_link);
842 wl_list_remove(&priv->link);
843 break;
844 }
845 }
846
847 wl_list_insert(&shell->backgrounds, &shsurf->link);
848
849 weston_surface_set_position(surface, shsurf->output->x,
850 shsurf->output->y);
851 break;
852
853 case SHELL_SURFACE_PANEL:
854 wl_list_for_each(priv, &shell->panels, link) {
855 if (priv->output == shsurf->output) {
856 priv->surface->output = NULL;
857 wl_list_remove(&priv->surface->layer_link);
858 wl_list_remove(&priv->link);
859 break;
860 }
861 }
862
863 wl_list_insert(&shell->panels, &shsurf->link);
864
865 weston_surface_set_position(surface, shsurf->output->x,
866 shsurf->output->y);
867 break;
868
869 default:
870 break;
871 }
872}
873
874static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300875set_toplevel(struct shell_surface *shsurf)
876{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400877 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300878}
879
880static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200881shell_surface_set_toplevel(struct wl_client *client,
882 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400883{
Pekka Paalanen98262232011-12-01 10:42:22 +0200884 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400885
Tiago Vignattibc052c92012-04-19 16:18:18 +0300886 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400887}
888
889static void
Tiago Vignatti491bac12012-05-18 16:37:43 -0400890set_transient(struct shell_surface *shsurf,
891 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
892{
893 /* assign to parents output */
894 shsurf->parent = pshsurf;
895 shsurf->transient.x = x;
896 shsurf->transient.y = y;
897 shsurf->transient.flags = flags;
898 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
899}
900
901static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200902shell_surface_set_transient(struct wl_client *client,
903 struct wl_resource *resource,
904 struct wl_resource *parent_resource,
905 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400906{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200907 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400908 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200909
Tiago Vignatti491bac12012-05-18 16:37:43 -0400910 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400911}
912
Tiago Vignattibe143262012-04-16 17:31:41 +0300913static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800914shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200915{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400916 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800917}
918
919static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300920get_output_panel_height(struct desktop_shell *shell,
921 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800922{
923 struct shell_surface *priv;
924 int panel_height = 0;
925
926 if (!output)
927 return 0;
928
Tiago Vignattibe143262012-04-16 17:31:41 +0300929 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800930 if (priv->output == output) {
931 panel_height = priv->surface->geometry.height;
932 break;
933 }
934 }
935 return panel_height;
936}
937
938static void
939shell_surface_set_maximized(struct wl_client *client,
940 struct wl_resource *resource,
941 struct wl_resource *output_resource )
942{
943 struct shell_surface *shsurf = resource->data;
944 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300945 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800946 uint32_t edges = 0, panel_height = 0;
947
948 /* get the default output, if the client set it as NULL
949 check whether the ouput is available */
950 if (output_resource)
951 shsurf->output = output_resource->data;
952 else
953 shsurf->output = get_default_output(es->compositor);
954
Tiago Vignattibe143262012-04-16 17:31:41 +0300955 shell = shell_surface_get_shell(shsurf);
956 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800957 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500958
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400959 shsurf->client->send_configure(shsurf->surface, edges,
960 es->output->current->width,
961 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800962
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400963 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200964}
965
Alex Wu21858432012-04-01 20:13:08 +0800966static void
967black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
968
Alex Wu4539b082012-03-01 12:57:46 +0800969static struct weston_surface *
970create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800971 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800972 GLfloat x, GLfloat y, int w, int h)
973{
974 struct weston_surface *surface = NULL;
975
976 surface = weston_surface_create(ec);
977 if (surface == NULL) {
978 fprintf(stderr, "no memory\n");
979 return NULL;
980 }
981
Alex Wu21858432012-04-01 20:13:08 +0800982 surface->configure = black_surface_configure;
983 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800984 weston_surface_configure(surface, x, y, w, h);
985 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
986 return surface;
987}
988
989/* Create black surface and append it to the associated fullscreen surface.
990 * Handle size dismatch and positioning according to the method. */
991static void
992shell_configure_fullscreen(struct shell_surface *shsurf)
993{
994 struct weston_output *output = shsurf->fullscreen_output;
995 struct weston_surface *surface = shsurf->surface;
996 struct weston_matrix *matrix;
997 float scale;
998
999 center_on_output(surface, output);
1000
1001 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001002 shsurf->fullscreen.black_surface =
1003 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001004 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001005 output->x, output->y,
1006 output->current->width,
1007 output->current->height);
1008
1009 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1010 wl_list_insert(&surface->layer_link,
1011 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001012 shsurf->fullscreen.black_surface->output = output;
1013
1014 switch (shsurf->fullscreen.type) {
1015 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1016 break;
1017 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1018 matrix = &shsurf->fullscreen.transform.matrix;
1019 weston_matrix_init(matrix);
1020 scale = (float)output->current->width/(float)surface->geometry.width;
1021 weston_matrix_scale(matrix, scale, scale, 1);
1022 wl_list_remove(&shsurf->fullscreen.transform.link);
1023 wl_list_insert(surface->geometry.transformation_list.prev,
1024 &shsurf->fullscreen.transform.link);
1025 weston_surface_set_position(surface, output->x, output->y);
1026 break;
1027 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001028 if (shell_surface_is_top_fullscreen(shsurf)) {
1029 struct weston_mode mode = {0,
1030 surface->geometry.width,
1031 surface->geometry.height,
1032 shsurf->fullscreen.framerate};
1033
1034 if (weston_output_switch_mode(output, &mode) == 0) {
1035 weston_surface_configure(shsurf->fullscreen.black_surface,
1036 output->x, output->y,
1037 output->current->width,
1038 output->current->height);
1039 weston_surface_set_position(surface, output->x, output->y);
1040 break;
1041 }
1042 }
Alex Wu4539b082012-03-01 12:57:46 +08001043 break;
1044 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1045 break;
1046 default:
1047 break;
1048 }
1049}
1050
1051/* make the fullscreen and black surface at the top */
1052static void
1053shell_stack_fullscreen(struct shell_surface *shsurf)
1054{
Alex Wubd3354b2012-04-17 17:20:49 +08001055 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001056 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001057 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001058
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001059 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001060 wl_list_insert(&shell->fullscreen_layer.surface_list,
1061 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001062 weston_surface_damage(surface);
1063
1064 if (!shsurf->fullscreen.black_surface)
1065 shsurf->fullscreen.black_surface =
1066 create_black_surface(surface->compositor,
1067 surface,
1068 output->x, output->y,
1069 output->current->width,
1070 output->current->height);
1071
1072 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001073 wl_list_insert(&surface->layer_link,
1074 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001075 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001076}
1077
1078static void
1079shell_map_fullscreen(struct shell_surface *shsurf)
1080{
Alex Wu4539b082012-03-01 12:57:46 +08001081 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001082 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001083}
1084
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001085static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001086shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001087 struct wl_resource *resource,
1088 uint32_t method,
1089 uint32_t framerate,
1090 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001091{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001092 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001093 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001094
1095 if (output_resource)
1096 shsurf->output = output_resource->data;
1097 else
1098 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001099
Alex Wu4539b082012-03-01 12:57:46 +08001100 shsurf->fullscreen_output = shsurf->output;
1101 shsurf->fullscreen.type = method;
1102 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001103 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001104
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001105 shsurf->client->send_configure(shsurf->surface, 0,
1106 shsurf->output->current->width,
1107 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001108}
1109
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001110static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001111popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001112 struct wl_surface *surface,
1113 wl_fixed_t x,
1114 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001115{
Daniel Stone37816df2012-05-16 18:45:18 +01001116 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001117 struct shell_surface *priv =
1118 container_of(grab, struct shell_surface, popup.grab);
1119 struct wl_client *client = priv->surface->surface.resource.client;
1120
Pekka Paalanencb108432012-01-19 16:25:40 +02001121 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001122 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001123 grab->focus = surface;
1124 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001125 wl_pointer_set_focus(pointer, NULL,
1126 wl_fixed_from_int(0),
1127 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001128 grab->focus = NULL;
1129 }
1130}
1131
1132static void
Scott Moreau447013d2012-02-18 05:05:29 -07001133popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001134 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001135{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001136 struct wl_resource *resource;
1137
Daniel Stone37816df2012-05-16 18:45:18 +01001138 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001139 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001140 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001141}
1142
1143static void
Scott Moreau447013d2012-02-18 05:05:29 -07001144popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001145 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001146{
1147 struct wl_resource *resource;
1148 struct shell_surface *shsurf =
1149 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001150 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001151 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001152 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001153
Daniel Stone37816df2012-05-16 18:45:18 +01001154 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001155 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001156 display = wl_client_get_display(resource->client);
1157 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001158 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001159 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001160 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001161 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001162 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001163 wl_pointer_end_grab(grab->pointer);
1164 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001165 }
1166
Daniel Stone4dbadb12012-05-30 16:31:51 +01001167 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001168 shsurf->popup.initial_up = 1;
1169}
1170
Scott Moreau447013d2012-02-18 05:05:29 -07001171static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001172 popup_grab_focus,
1173 popup_grab_motion,
1174 popup_grab_button,
1175};
1176
1177static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001178shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001179{
Daniel Stone37816df2012-05-16 18:45:18 +01001180 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001181 struct weston_surface *es = shsurf->surface;
1182 struct weston_surface *parent = shsurf->parent->surface;
1183
1184 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001185 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001186
Pekka Paalanen938269a2012-02-07 14:19:01 +02001187 weston_surface_update_transform(parent);
1188 if (parent->transform.enabled) {
1189 shsurf->popup.parent_transform.matrix =
1190 parent->transform.matrix;
1191 } else {
1192 /* construct x, y translation matrix */
1193 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1194 shsurf->popup.parent_transform.matrix.d[12] =
1195 parent->geometry.x;
1196 shsurf->popup.parent_transform.matrix.d[13] =
1197 parent->geometry.y;
1198 }
1199 wl_list_insert(es->geometry.transformation_list.prev,
1200 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001201 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001202
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001203 shsurf->popup.initial_up = 0;
1204
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001205 /* We don't require the grab to still be active, but if another
1206 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001207 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1208 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001209 } else {
1210 wl_shell_surface_send_popup_done(&shsurf->resource);
1211 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001212}
1213
1214static void
1215shell_surface_set_popup(struct wl_client *client,
1216 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001217 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001218 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001219 struct wl_resource *parent_resource,
1220 int32_t x, int32_t y, uint32_t flags)
1221{
1222 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001223
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001224 shsurf->type = SHELL_SURFACE_POPUP;
1225 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001226 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001227 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001228 shsurf->popup.x = x;
1229 shsurf->popup.y = y;
1230}
1231
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001232static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001233 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001234 shell_surface_move,
1235 shell_surface_resize,
1236 shell_surface_set_toplevel,
1237 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001238 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001239 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001240 shell_surface_set_maximized,
1241 shell_surface_set_title,
1242 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001243};
1244
1245static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001246destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001247{
Daniel Stone37816df2012-05-16 18:45:18 +01001248 if (shsurf->popup.grab.pointer)
1249 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001250
Alex Wubd3354b2012-04-17 17:20:49 +08001251 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1252 shell_surface_is_top_fullscreen(shsurf)) {
1253 weston_output_switch_mode(shsurf->fullscreen_output,
1254 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001255 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001256
Alex Wuaa08e2d2012-03-05 11:01:40 +08001257 if (shsurf->fullscreen.black_surface)
1258 weston_surface_destroy(shsurf->fullscreen.black_surface);
1259
Alex Wubd3354b2012-04-17 17:20:49 +08001260 /* As destroy_resource() use wl_list_for_each_safe(),
1261 * we can always remove the listener.
1262 */
1263 wl_list_remove(&shsurf->surface_destroy_listener.link);
1264 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001265 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001266
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001267 wl_list_remove(&shsurf->link);
1268 free(shsurf);
1269}
1270
1271static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001272shell_destroy_shell_surface(struct wl_resource *resource)
1273{
1274 struct shell_surface *shsurf = resource->data;
1275
1276 destroy_shell_surface(shsurf);
1277}
1278
1279static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001280shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001281{
1282 struct shell_surface *shsurf = container_of(listener,
1283 struct shell_surface,
1284 surface_destroy_listener);
1285
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001286 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001287 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001288 } else {
1289 wl_signal_emit(&shsurf->resource.destroy_signal,
1290 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001291 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001292 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001293}
1294
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001295static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001296get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001297{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001298 struct wl_listener *listener;
1299
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001300 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1301 shell_handle_surface_destroy);
1302 if (listener)
1303 return container_of(listener, struct shell_surface,
1304 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001305
1306 return NULL;
1307}
1308
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001309static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001310shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1311
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001312static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001313create_shell_surface(void *shell, struct weston_surface *surface,
1314 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001315{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001316 struct shell_surface *shsurf;
1317
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001318 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001319 fprintf(stderr, "surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001320 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001321 }
1322
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001323 shsurf = calloc(1, sizeof *shsurf);
1324 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001325 fprintf(stderr, "no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001326 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001327 }
1328
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001329 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001330 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001331
Tiago Vignattibc052c92012-04-19 16:18:18 +03001332 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001333 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001334 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001335 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001336 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001337 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1338 shsurf->fullscreen.framerate = 0;
1339 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001340 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001341 wl_list_init(&shsurf->fullscreen.transform.link);
1342
Tiago Vignattibc052c92012-04-19 16:18:18 +03001343 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001344 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1345 wl_signal_add(&surface->surface.resource.destroy_signal,
1346 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001347
1348 /* init link so its safe to always remove it in destroy_shell_surface */
1349 wl_list_init(&shsurf->link);
1350
Pekka Paalanen460099f2012-01-20 16:48:25 +02001351 /* empty when not in use */
1352 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001353 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001354
Pekka Paalanen98262232011-12-01 10:42:22 +02001355 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001356 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001357
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001358 shsurf->client = client;
1359
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001360 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001361}
1362
1363static void
1364shell_get_shell_surface(struct wl_client *client,
1365 struct wl_resource *resource,
1366 uint32_t id,
1367 struct wl_resource *surface_resource)
1368{
1369 struct weston_surface *surface = surface_resource->data;
1370 struct desktop_shell *shell = resource->data;
1371 struct shell_surface *shsurf;
1372
1373 if (get_shell_surface(surface)) {
1374 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001375 WL_DISPLAY_ERROR_INVALID_OBJECT,
1376 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001377 return;
1378 }
1379
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001380 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001381 if (!shsurf) {
1382 wl_resource_post_error(surface_resource,
1383 WL_DISPLAY_ERROR_INVALID_OBJECT,
1384 "surface->configure already set");
1385 return;
1386 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001387
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001388 shsurf->resource.destroy = shell_destroy_shell_surface;
1389 shsurf->resource.object.id = id;
1390 shsurf->resource.object.interface = &wl_shell_surface_interface;
1391 shsurf->resource.object.implementation =
1392 (void (**)(void)) &shell_surface_implementation;
1393 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001394
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001395 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001396}
1397
1398static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001399 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001400};
1401
Kristian Høgsberg07937562011-04-12 17:25:42 -04001402static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001403handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001404{
1405 proc->pid = 0;
1406}
1407
1408static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001409launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001410{
1411 if (shell->screensaver.binding)
1412 return;
1413
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001414 if (!shell->screensaver.path)
1415 return;
1416
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001417 if (shell->screensaver.process.pid != 0) {
1418 fprintf(stderr, "old screensaver still running\n");
1419 return;
1420 }
1421
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001423 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001424 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001425 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001426}
1427
1428static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001429terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001430{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001431 if (shell->screensaver.process.pid == 0)
1432 return;
1433
1434 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001435}
1436
1437static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001438show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001439{
1440 struct wl_list *list;
1441
1442 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001443 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001444 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001445 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001446
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001447 wl_list_remove(&surface->surface->layer_link);
1448 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001449 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001450 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001451}
1452
1453static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001454hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001455{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001456 wl_list_remove(&surface->surface->layer_link);
1457 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001458 surface->surface->output = NULL;
1459}
1460
1461static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001462desktop_shell_set_background(struct wl_client *client,
1463 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001464 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001465 struct wl_resource *surface_resource)
1466{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001467 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001468
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001469 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001470 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001471
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001472 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001473 surface_resource,
1474 shsurf->output->current->width,
1475 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001476}
1477
1478static void
1479desktop_shell_set_panel(struct wl_client *client,
1480 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001481 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001482 struct wl_resource *surface_resource)
1483{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001484 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001485
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001486 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001487 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001488
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001489 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001490 surface_resource,
1491 shsurf->output->current->width,
1492 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001493}
1494
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001495static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001496handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001497{
Tiago Vignattibe143262012-04-16 17:31:41 +03001498 struct desktop_shell *shell =
1499 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001500
1501 fprintf(stderr, "lock surface gone\n");
1502 shell->lock_surface = NULL;
1503}
1504
1505static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001506desktop_shell_set_lock_surface(struct wl_client *client,
1507 struct wl_resource *resource,
1508 struct wl_resource *surface_resource)
1509{
Tiago Vignattibe143262012-04-16 17:31:41 +03001510 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001511 struct shell_surface *surface = surface_resource->data;
1512
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001513 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001514
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001515 if (!shell->locked)
1516 return;
1517
Pekka Paalanen98262232011-12-01 10:42:22 +02001518 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001519
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001520 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1521 wl_signal_add(&surface_resource->destroy_signal,
1522 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001523
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001524 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001525}
1526
1527static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001528resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001529{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001530 struct shell_surface *tmp;
1531
1532 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1533 hide_screensaver(shell, tmp);
1534
1535 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001536
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001537 wl_list_remove(&shell->lock_layer.link);
1538 wl_list_insert(&shell->compositor->cursor_layer.link,
1539 &shell->fullscreen_layer.link);
1540 wl_list_insert(&shell->fullscreen_layer.link,
1541 &shell->panel_layer.link);
1542 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001543
1544 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001545 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001547 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001548}
1549
1550static void
1551desktop_shell_unlock(struct wl_client *client,
1552 struct wl_resource *resource)
1553{
Tiago Vignattibe143262012-04-16 17:31:41 +03001554 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001555
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001556 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001557
1558 if (shell->locked)
1559 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001560}
1561
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001562static void
1563desktop_shell_set_busy_surface(struct wl_client *client,
1564 struct wl_resource *resource,
1565 struct wl_resource *surface_resource)
1566{
1567 struct desktop_shell *shell = resource->data;
1568
1569 shell->busy_surface = surface_resource->data;
1570}
1571
Kristian Høgsberg75840622011-09-06 13:48:16 -04001572static const struct desktop_shell_interface desktop_shell_implementation = {
1573 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001574 desktop_shell_set_panel,
1575 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001576 desktop_shell_unlock,
1577 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04001578};
1579
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001580static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001581get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001582{
1583 struct shell_surface *shsurf;
1584
1585 shsurf = get_shell_surface(surface);
1586 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001587 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001588 return shsurf->type;
1589}
1590
Kristian Høgsberg75840622011-09-06 13:48:16 -04001591static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001592move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001593{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001594 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001595 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001596 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001597
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001598 if (surface == NULL)
1599 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001600
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001601 shsurf = get_shell_surface(surface);
1602 if (shsurf == NULL)
1603 return;
1604
1605 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001606 case SHELL_SURFACE_PANEL:
1607 case SHELL_SURFACE_BACKGROUND:
1608 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001609 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001610 return;
1611 default:
1612 break;
1613 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001614
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001615 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001616}
1617
1618static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001619resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001620{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001621 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001622 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001623 uint32_t edges = 0;
1624 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001625 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001626
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001627 if (surface == NULL)
1628 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001629
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001630 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001631 if (!shsurf)
1632 return;
1633
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001634 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001635 case SHELL_SURFACE_PANEL:
1636 case SHELL_SURFACE_BACKGROUND:
1637 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001638 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001639 return;
1640 default:
1641 break;
1642 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001643
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001644 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001645 wl_fixed_to_int(seat->pointer->grab_x),
1646 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001647 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001648
Pekka Paalanen60921e52012-01-25 15:55:43 +02001649 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001650 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001651 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001652 edges |= 0;
1653 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001654 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001655
Pekka Paalanen60921e52012-01-25 15:55:43 +02001656 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001657 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001658 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001659 edges |= 0;
1660 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001662
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001663 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001664}
1665
1666static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001667surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001668 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001669{
Scott Moreau02709af2012-05-22 01:54:10 -06001670 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001671 struct shell_surface *shsurf;
1672 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001673 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001674
1675 if (surface == NULL)
1676 return;
1677
1678 shsurf = get_shell_surface(surface);
1679 if (!shsurf)
1680 return;
1681
1682 switch (shsurf->type) {
1683 case SHELL_SURFACE_BACKGROUND:
1684 case SHELL_SURFACE_SCREENSAVER:
1685 return;
1686 default:
1687 break;
1688 }
1689
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001690 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001691
Scott Moreau02709af2012-05-22 01:54:10 -06001692 if (surface->alpha > 1.0)
1693 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001694 if (surface->alpha < step)
1695 surface->alpha = step;
1696
1697 surface->geometry.dirty = 1;
1698 weston_surface_damage(surface);
1699}
1700
1701static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001702do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001703 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001704{
Daniel Stone37816df2012-05-16 18:45:18 +01001705 struct weston_seat *ws = (struct weston_seat *) seat;
1706 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001707 struct weston_output *output;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001708 float maximum_level, increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001709
1710 wl_list_for_each(output, &compositor->output_list, link) {
1711 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01001712 wl_fixed_to_double(seat->pointer->x),
1713 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001714 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01001715 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001716 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01001717 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001718 increment = -output->zoom.increment;
1719 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001720 increment = output->zoom.increment *
1721 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001722 else
1723 increment = 0;
1724
Scott Moreau1b45a792012-03-22 10:58:23 -06001725 output->zoom.active = 1;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001726 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001727
Scott Moreau850ca422012-05-21 15:21:25 -06001728 if (output->zoom.level <= 0.0) {
Scott Moreauc6d7f602012-02-23 22:28:37 -07001729 output->zoom.active = 0;
Scott Moreau850ca422012-05-21 15:21:25 -06001730 output->zoom.level = 0.0;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001731 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001732
Scott Moreau850ca422012-05-21 15:21:25 -06001733 maximum_level = 1 - output->zoom.increment;
1734
1735 if (output->zoom.level > maximum_level)
1736 output->zoom.level = maximum_level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001737
Daniel Stone37816df2012-05-16 18:45:18 +01001738 weston_output_update_zoom(output,
1739 seat->pointer->x,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001740 seat->pointer->y,
1741 ZOOM_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001742 }
1743 }
1744}
1745
Scott Moreauccbf29d2012-02-22 14:21:41 -07001746static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001747zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001748 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001749{
1750 do_zoom(seat, time, 0, axis, value);
1751}
1752
1753static void
1754zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1755 void *data)
1756{
1757 do_zoom(seat, time, key, 0, 0);
1758}
1759
1760static void
1761terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1762 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001763{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001765
Daniel Stone325fc2d2012-05-30 16:31:58 +01001766 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001767}
1768
1769static void
Scott Moreau447013d2012-02-18 05:05:29 -07001770rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001771 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001772{
1773 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001774 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001775 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001776 struct shell_surface *shsurf = rotate->base.shsurf;
1777 struct weston_surface *surface;
1778 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1779
1780 if (!shsurf)
1781 return;
1782
1783 surface = shsurf->surface;
1784
1785 cx = 0.5f * surface->geometry.width;
1786 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001787
Daniel Stone37816df2012-05-16 18:45:18 +01001788 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1789 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001790 r = sqrtf(dx * dx + dy * dy);
1791
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001792 wl_list_remove(&shsurf->rotation.transform.link);
1793 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001794
1795 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001796 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001797 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001798
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001799 weston_matrix_init(&rotate->rotation);
1800 rotate->rotation.d[0] = dx / r;
1801 rotate->rotation.d[4] = -dy / r;
1802 rotate->rotation.d[1] = -rotate->rotation.d[4];
1803 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001804
1805 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001806 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001807 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001808 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001809 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001810
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001811 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001812 &shsurf->surface->geometry.transformation_list,
1813 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001814 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001815 wl_list_init(&shsurf->rotation.transform.link);
1816 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001817 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001818 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001819
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001820 /* We need to adjust the position of the surface
1821 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001822 cposx = surface->geometry.x + cx;
1823 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001824 dposx = rotate->center.x - cposx;
1825 dposy = rotate->center.y - cposy;
1826 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001827 weston_surface_set_position(surface,
1828 surface->geometry.x + dposx,
1829 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001830 }
1831
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001832 /* Repaint implies weston_surface_update_transform(), which
1833 * lazily applies the damage due to rotation update.
1834 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001835 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001836}
1837
1838static void
Scott Moreau447013d2012-02-18 05:05:29 -07001839rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001840 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001841{
1842 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001843 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001844 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001845 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001846 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001847
Daniel Stone4dbadb12012-05-30 16:31:51 +01001848 if (pointer->button_count == 0 &&
1849 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001850 if (shsurf)
1851 weston_matrix_multiply(&shsurf->rotation.rotation,
1852 &rotate->rotation);
1853 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001854 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001855 free(rotate);
1856 }
1857}
1858
Scott Moreau447013d2012-02-18 05:05:29 -07001859static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001860 noop_grab_focus,
1861 rotate_grab_motion,
1862 rotate_grab_button,
1863};
1864
1865static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001866rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01001867 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001868{
1869 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001870 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001871 struct shell_surface *surface;
1872 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001873 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001874 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001875
1876 if (base_surface == NULL)
1877 return;
1878
1879 surface = get_shell_surface(base_surface);
1880 if (!surface)
1881 return;
1882
1883 switch (surface->type) {
1884 case SHELL_SURFACE_PANEL:
1885 case SHELL_SURFACE_BACKGROUND:
1886 case SHELL_SURFACE_FULLSCREEN:
1887 case SHELL_SURFACE_SCREENSAVER:
1888 return;
1889 default:
1890 break;
1891 }
1892
Pekka Paalanen460099f2012-01-20 16:48:25 +02001893 rotate = malloc(sizeof *rotate);
1894 if (!rotate)
1895 return;
1896
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001897 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001898
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04001899 weston_surface_to_global_float(surface->surface,
1900 surface->surface->geometry.width / 2,
1901 surface->surface->geometry.height / 2,
1902 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001903
Daniel Stone37816df2012-05-16 18:45:18 +01001904 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001905
Daniel Stone37816df2012-05-16 18:45:18 +01001906 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1907 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001908 r = sqrtf(dx * dx + dy * dy);
1909 if (r > 20.0f) {
1910 struct weston_matrix inverse;
1911
1912 weston_matrix_init(&inverse);
1913 inverse.d[0] = dx / r;
1914 inverse.d[4] = dy / r;
1915 inverse.d[1] = -inverse.d[4];
1916 inverse.d[5] = inverse.d[0];
1917 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001918
1919 weston_matrix_init(&rotate->rotation);
1920 rotate->rotation.d[0] = dx / r;
1921 rotate->rotation.d[4] = -dy / r;
1922 rotate->rotation.d[1] = -rotate->rotation.d[4];
1923 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001924 } else {
1925 weston_matrix_init(&surface->rotation.rotation);
1926 weston_matrix_init(&rotate->rotation);
1927 }
1928
Daniel Stone37816df2012-05-16 18:45:18 +01001929 wl_pointer_set_focus(seat->pointer, NULL,
1930 wl_fixed_from_int(0),
1931 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02001932}
1933
1934static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001935activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01001936 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001937{
Alex Wu21858432012-04-01 20:13:08 +08001938 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001939
Daniel Stone37816df2012-05-16 18:45:18 +01001940 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001941
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001942 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001943 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001944 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001945 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001946 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001947
Pekka Paalanen77346a62011-11-30 16:26:35 +02001948 case SHELL_SURFACE_SCREENSAVER:
1949 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001950 if (shell->lock_surface)
1951 weston_surface_restack(es,
1952 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001953 break;
Alex Wu4539b082012-03-01 12:57:46 +08001954 case SHELL_SURFACE_FULLSCREEN:
1955 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001956 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001957 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001958 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001959 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001960 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001961 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1962 wl_list_for_each_reverse_safe(surf,
1963 prev,
1964 &shell->fullscreen_layer.surface_list,
1965 layer_link)
1966 weston_surface_restack(surf,
1967 &shell->toplevel_layer.surface_list);
1968 }
1969
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001970 weston_surface_restack(es,
1971 &shell->toplevel_layer.surface_list);
1972 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001973 }
1974}
1975
Alex Wu21858432012-04-01 20:13:08 +08001976/* no-op func for checking black surface */
1977static void
1978black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1979{
1980}
1981
1982static bool
1983is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1984{
1985 if (es->configure == black_surface_configure) {
1986 if (fs_surface)
1987 *fs_surface = (struct weston_surface *)es->private;
1988 return true;
1989 }
1990 return false;
1991}
1992
Kristian Høgsberg75840622011-09-06 13:48:16 -04001993static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001994click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001995 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001996{
Daniel Stone37816df2012-05-16 18:45:18 +01001997 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03001998 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001999 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002000 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002001
Daniel Stone37816df2012-05-16 18:45:18 +01002002 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002003 if (!focus)
2004 return;
2005
Alex Wu21858432012-04-01 20:13:08 +08002006 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002007 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002008
Daniel Stone325fc2d2012-05-30 16:31:58 +01002009 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002010 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002011}
2012
2013static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002014lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002015{
Tiago Vignattibe143262012-04-16 17:31:41 +03002016 struct desktop_shell *shell =
2017 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01002018 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002019 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002020 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002021
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002022 if (shell->locked) {
2023 wl_list_for_each(output, &shell->compositor->output_list, link)
2024 /* TODO: find a way to jump to other DPMS levels */
2025 if (output->set_dpms)
2026 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002027 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002028 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002029
2030 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002031
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002032 /* Hide all surfaces by removing the fullscreen, panel and
2033 * toplevel layers. This way nothing else can show or receive
2034 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002035
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002036 wl_list_remove(&shell->panel_layer.link);
2037 wl_list_remove(&shell->toplevel_layer.link);
2038 wl_list_remove(&shell->fullscreen_layer.link);
2039 wl_list_insert(&shell->compositor->cursor_layer.link,
2040 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002041
Pekka Paalanen77346a62011-11-30 16:26:35 +02002042 launch_screensaver(shell);
2043
2044 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2045 show_screensaver(shell, shsurf);
2046
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002047 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002048 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002049 weston_compositor_wake(shell->compositor);
2050 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002051 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002052
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002053 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002054 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002055
2056 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01002057 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2058 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002059 }
2060
2061 /* TODO: disable bindings that should not work while locked. */
2062
2063 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002064}
2065
2066static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002067unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002068{
Tiago Vignattibe143262012-04-16 17:31:41 +03002069 struct desktop_shell *shell =
2070 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002071
Pekka Paalanend81c2162011-11-16 13:47:34 +02002072 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002073 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002074 return;
2075 }
2076
2077 /* If desktop-shell client has gone away, unlock immediately. */
2078 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002079 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002080 return;
2081 }
2082
2083 if (shell->prepare_event_sent)
2084 return;
2085
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002086 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002087 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002088}
2089
2090static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002091center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002092{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002093 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002094 GLfloat x = (mode->width - surface->geometry.width) / 2;
2095 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002096
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002097 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002098}
2099
2100static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002101map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002102 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002103{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002104 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002105 struct shell_surface *shsurf;
2106 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002107 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002108 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08002109 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002110
Pekka Paalanen77346a62011-11-30 16:26:35 +02002111 shsurf = get_shell_surface(surface);
2112 if (shsurf)
2113 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002114
Pekka Paalanen60921e52012-01-25 15:55:43 +02002115 surface->geometry.width = width;
2116 surface->geometry.height = height;
2117 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002118
2119 /* initial positioning, see also configure() */
2120 switch (surface_type) {
2121 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002122 weston_surface_set_position(surface, 10 + random() % 400,
2123 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002124 break;
2125 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002126 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002127 break;
Alex Wu4539b082012-03-01 12:57:46 +08002128 case SHELL_SURFACE_FULLSCREEN:
2129 shell_map_fullscreen(shsurf);
2130 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002131 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002132 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002133 panel_height = get_output_panel_height(shell,surface->output);
2134 weston_surface_set_position(surface, surface->output->x,
2135 surface->output->y + panel_height);
2136 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002137 case SHELL_SURFACE_LOCK:
2138 center_on_output(surface, get_default_output(compositor));
2139 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002140 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002141 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002142 case SHELL_SURFACE_NONE:
2143 weston_surface_set_position(surface,
2144 surface->geometry.x + sx,
2145 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002146 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002147 default:
2148 ;
2149 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002150
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002151 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002152 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002153 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002154 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002155 wl_list_insert(&shell->background_layer.surface_list,
2156 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002157 break;
2158 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002159 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002160 wl_list_insert(&shell->panel_layer.surface_list,
2161 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002162 break;
2163 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002164 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002165 wl_list_insert(&shell->lock_layer.surface_list,
2166 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002167 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002168 break;
2169 case SHELL_SURFACE_SCREENSAVER:
2170 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002171 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002172 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002173 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002174 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002175 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002176 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002177 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002178 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002179 case SHELL_SURFACE_POPUP:
2180 case SHELL_SURFACE_TRANSIENT:
2181 parent = shsurf->parent->surface;
2182 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2183 break;
Alex Wu4539b082012-03-01 12:57:46 +08002184 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002185 case SHELL_SURFACE_NONE:
2186 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002187 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002188 wl_list_insert(&shell->toplevel_layer.surface_list,
2189 &surface->layer_link);
2190 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002191 }
2192
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002193 if (surface_type != SHELL_SURFACE_NONE) {
2194 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002195 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2196 surface->output = shsurf->output;
2197 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002198
Juan Zhao7bb92f02011-12-15 11:31:51 -05002199 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002200 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002201 if (shsurf->transient.flags ==
2202 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2203 break;
2204 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002205 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002206 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002207 if (!shell->locked) {
2208 wl_list_for_each(seat, &compositor->seat_list, link)
2209 activate(shell, surface, seat);
2210 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002211 break;
2212 default:
2213 break;
2214 }
2215
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002216 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002217 {
2218 switch (shell->win_animation_type) {
2219 case ANIMATION_FADE:
2220 weston_fade_run(surface, NULL, NULL);
2221 break;
2222 case ANIMATION_ZOOM:
2223 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2224 break;
2225 default:
2226 break;
2227 }
2228 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002229}
2230
2231static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002232configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002233 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002234{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002235 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2236 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002237
Pekka Paalanen77346a62011-11-30 16:26:35 +02002238 shsurf = get_shell_surface(surface);
2239 if (shsurf)
2240 surface_type = shsurf->type;
2241
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002242 surface->geometry.x = x;
2243 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002244 surface->geometry.width = width;
2245 surface->geometry.height = height;
2246 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002247
2248 switch (surface_type) {
2249 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002250 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002251 break;
Alex Wu4539b082012-03-01 12:57:46 +08002252 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002253 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002254 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002255 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002256 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002257 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002258 surface->geometry.x = surface->output->x;
2259 surface->geometry.y = surface->output->y +
2260 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002261 break;
Alex Wu4539b082012-03-01 12:57:46 +08002262 case SHELL_SURFACE_TOPLEVEL:
2263 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002264 default:
2265 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002266 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002267
Alex Wu4539b082012-03-01 12:57:46 +08002268 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002269 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002270 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002271
2272 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2273 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002274 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2275 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002276 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002277}
2278
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002279static void
2280shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2281{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002282 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002283 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002284 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002285
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002286 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002287 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002288 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002289 type_changed = 1;
2290 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002291
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002292 if (!weston_surface_is_mapped(es)) {
2293 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002294 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002295 es->geometry.width != es->buffer->width ||
2296 es->geometry.height != es->buffer->height) {
2297 GLfloat from_x, from_y;
2298 GLfloat to_x, to_y;
2299
2300 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2301 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2302 configure(shell, es,
2303 es->geometry.x + to_x - from_x,
2304 es->geometry.y + to_y - from_y,
2305 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002306 }
2307}
2308
Tiago Vignattibe143262012-04-16 17:31:41 +03002309static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002310
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002311static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002312desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002313{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002314 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002315 struct desktop_shell *shell =
2316 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002317
2318 shell->child.process.pid = 0;
2319 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002320
2321 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2322 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002323 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002324 shell->child.deathstamp = time;
2325 shell->child.deathcount = 0;
2326 }
2327
2328 shell->child.deathcount++;
2329 if (shell->child.deathcount > 5) {
2330 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2331 return;
2332 }
2333
2334 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2335 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002336}
2337
2338static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002339launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002340{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002341 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002342
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002343 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002344 &shell->child.process,
2345 shell_exe,
2346 desktop_shell_sigchld);
2347
2348 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002349 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002350 return 0;
2351}
2352
2353static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002354bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2355{
Tiago Vignattibe143262012-04-16 17:31:41 +03002356 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002357
2358 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002359 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002360}
2361
Kristian Høgsberg75840622011-09-06 13:48:16 -04002362static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002363unbind_desktop_shell(struct wl_resource *resource)
2364{
Tiago Vignattibe143262012-04-16 17:31:41 +03002365 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002366
2367 if (shell->locked)
2368 resume_desktop(shell);
2369
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002370 shell->child.desktop_shell = NULL;
2371 shell->prepare_event_sent = false;
2372 free(resource);
2373}
2374
2375static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002376bind_desktop_shell(struct wl_client *client,
2377 void *data, uint32_t version, uint32_t id)
2378{
Tiago Vignattibe143262012-04-16 17:31:41 +03002379 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002380 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002381
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002382 resource = wl_client_add_object(client, &desktop_shell_interface,
2383 &desktop_shell_implementation,
2384 id, shell);
2385
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002386 if (client == shell->child.client) {
2387 resource->destroy = unbind_desktop_shell;
2388 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002389 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002390 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002391
2392 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2393 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002394 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002395}
2396
Pekka Paalanen6e168112011-11-24 11:34:05 +02002397static void
2398screensaver_set_surface(struct wl_client *client,
2399 struct wl_resource *resource,
2400 struct wl_resource *shell_surface_resource,
2401 struct wl_resource *output_resource)
2402{
Tiago Vignattibe143262012-04-16 17:31:41 +03002403 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002404 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002405 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002406
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002407 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002408
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002409 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002410 surface->output = output;
2411 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002412}
2413
2414static const struct screensaver_interface screensaver_implementation = {
2415 screensaver_set_surface
2416};
2417
2418static void
2419unbind_screensaver(struct wl_resource *resource)
2420{
Tiago Vignattibe143262012-04-16 17:31:41 +03002421 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002422
Pekka Paalanen77346a62011-11-30 16:26:35 +02002423 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002424 free(resource);
2425}
2426
2427static void
2428bind_screensaver(struct wl_client *client,
2429 void *data, uint32_t version, uint32_t id)
2430{
Tiago Vignattibe143262012-04-16 17:31:41 +03002431 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002432 struct wl_resource *resource;
2433
2434 resource = wl_client_add_object(client, &screensaver_interface,
2435 &screensaver_implementation,
2436 id, shell);
2437
Pekka Paalanen77346a62011-11-30 16:26:35 +02002438 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002439 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002440 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002441 return;
2442 }
2443
2444 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2445 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002446 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002447}
2448
Kristian Høgsberg07045392012-02-19 18:52:44 -05002449struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002450 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002451 struct weston_surface *current;
2452 struct wl_listener listener;
2453 struct wl_keyboard_grab grab;
2454};
2455
2456static void
2457switcher_next(struct switcher *switcher)
2458{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002459 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002460 struct weston_surface *surface;
2461 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002462 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002463
2464 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002465 switch (get_shell_surface_type(surface)) {
2466 case SHELL_SURFACE_TOPLEVEL:
2467 case SHELL_SURFACE_FULLSCREEN:
2468 case SHELL_SURFACE_MAXIMIZED:
2469 if (first == NULL)
2470 first = surface;
2471 if (prev == switcher->current)
2472 next = surface;
2473 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002474 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002475 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002476 weston_surface_damage(surface);
2477 break;
2478 default:
2479 break;
2480 }
Alex Wu1659daa2012-04-01 20:13:09 +08002481
2482 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002483 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002484 surface->geometry.dirty = 1;
2485 weston_surface_damage(surface);
2486 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002487 }
2488
2489 if (next == NULL)
2490 next = first;
2491
Alex Wu07b26062012-03-12 16:06:01 +08002492 if (next == NULL)
2493 return;
2494
Kristian Høgsberg07045392012-02-19 18:52:44 -05002495 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002496 wl_signal_add(&next->surface.resource.destroy_signal,
2497 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002498
2499 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002500 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002501
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002502 shsurf = get_shell_surface(switcher->current);
2503 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002504 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002505}
2506
2507static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002508switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002509{
2510 struct switcher *switcher =
2511 container_of(listener, struct switcher, listener);
2512
2513 switcher_next(switcher);
2514}
2515
2516static void
Daniel Stone351eb612012-05-31 15:27:47 -04002517switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002518{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002519 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002520 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002521 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002522
2523 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002524 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002525 weston_surface_damage(surface);
2526 }
2527
Alex Wu07b26062012-03-12 16:06:01 +08002528 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002529 activate(switcher->shell, switcher->current,
2530 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002531 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002532 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002533 free(switcher);
2534}
2535
2536static void
2537switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002538 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002539{
2540 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002541 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002542
Daniel Stonec9785ea2012-05-30 16:31:52 +01002543 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002544 switcher_next(switcher);
2545}
2546
2547static void
2548switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2549 uint32_t mods_depressed, uint32_t mods_latched,
2550 uint32_t mods_locked, uint32_t group)
2551{
2552 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002553 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002554
Daniel Stone351eb612012-05-31 15:27:47 -04002555 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
2556 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002557}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002558
2559static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04002560 switcher_key,
2561 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05002562};
2563
2564static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002565switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2566 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002567{
Tiago Vignattibe143262012-04-16 17:31:41 +03002568 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002569 struct switcher *switcher;
2570
2571 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002572 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002573 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002574 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002575 wl_list_init(&switcher->listener.link);
2576
2577 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002578 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2579 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002580 switcher_next(switcher);
2581}
2582
Pekka Paalanen3c647232011-12-22 13:43:43 +02002583static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002584backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2585 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002586{
2587 struct weston_compositor *compositor = data;
2588 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002589 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002590
2591 /* TODO: we're limiting to simple use cases, where we assume just
2592 * control on the primary display. We'd have to extend later if we
2593 * ever get support for setting backlights on random desktop LCD
2594 * panels though */
2595 output = get_default_output(compositor);
2596 if (!output)
2597 return;
2598
2599 if (!output->set_backlight)
2600 return;
2601
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002602 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2603 backlight_new = output->backlight_current - 25;
2604 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2605 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002606
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002607 if (backlight_new < 5)
2608 backlight_new = 5;
2609 if (backlight_new > 255)
2610 backlight_new = 255;
2611
2612 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002613 output->set_backlight(output, output->backlight_current);
2614}
2615
2616static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002617debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2618 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002619{
Tiago Vignattibe143262012-04-16 17:31:41 +03002620 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002621 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002622 struct weston_surface *surface;
2623
2624 if (shell->debug_repaint_surface) {
2625 weston_surface_destroy(shell->debug_repaint_surface);
2626 shell->debug_repaint_surface = NULL;
2627 } else {
2628 surface = weston_surface_create(compositor);
2629 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2630 weston_surface_configure(surface, 0, 0, 8192, 8192);
2631 wl_list_insert(&compositor->fade_layer.surface_list,
2632 &surface->layer_link);
2633 weston_surface_assign_output(surface);
2634 pixman_region32_init(&surface->input);
2635
2636 /* Here's the dirty little trick that makes the
2637 * repaint debugging work: we force an
2638 * update_transform first to update dependent state
2639 * and clear the geometry.dirty bit. Then we clear
2640 * the surface damage so it only gets repainted
2641 * piecewise as we repaint other things. */
2642
2643 weston_surface_update_transform(surface);
2644 pixman_region32_fini(&surface->damage);
2645 pixman_region32_init(&surface->damage);
2646 shell->debug_repaint_surface = surface;
2647 }
2648}
2649
2650static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002651force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2652 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002653{
2654 struct wl_client *client;
2655 pid_t pid;
2656 uid_t uid;
2657 gid_t gid;
2658
Daniel Stone325fc2d2012-05-30 16:31:58 +01002659 client = seat->keyboard->focus->resource.client;
2660 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002661
Daniel Stone325fc2d2012-05-30 16:31:58 +01002662 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002663}
2664
2665static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002666shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002667{
Tiago Vignattibe143262012-04-16 17:31:41 +03002668 struct desktop_shell *shell =
2669 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002670
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002671 if (shell->child.client)
2672 wl_client_destroy(shell->child.client);
2673
Kristian Høgsberg88c16072012-05-16 08:04:19 -04002674 wl_list_remove(&shell->lock_listener.link);
2675 wl_list_remove(&shell->unlock_listener.link);
2676
Pekka Paalanen3c647232011-12-22 13:43:43 +02002677 free(shell->screensaver.path);
2678 free(shell);
2679}
2680
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002681static void
2682shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2683{
2684 uint32_t mod;
2685
2686 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01002687 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
2688 MODIFIER_CTRL | MODIFIER_ALT,
2689 terminate_binding, ec);
2690 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
2691 click_to_activate_binding,
2692 shell);
2693 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2694 MODIFIER_SUPER | MODIFIER_ALT,
2695 surface_opacity_binding, NULL);
2696 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2697 MODIFIER_SUPER, zoom_axis_binding,
2698 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002699
2700 /* configurable bindings */
2701 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002702 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
2703 zoom_key_binding, NULL);
2704 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
2705 zoom_key_binding, NULL);
2706 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
2707 shell);
2708 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
2709 resize_binding, shell);
2710 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
2711 rotate_binding, NULL);
2712 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
2713 shell);
2714 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
2715 ec);
2716 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
2717 backlight_binding, ec);
2718 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
2719 ec);
2720 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
2721 backlight_binding, ec);
2722 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
2723 debug_repaint_binding, shell);
2724 weston_compositor_add_key_binding(ec, KEY_K, mod,
2725 force_kill_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002726}
2727
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002728int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002729shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002730
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002731WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002732shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002733{
Tiago Vignattibe143262012-04-16 17:31:41 +03002734 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002735
2736 shell = malloc(sizeof *shell);
2737 if (shell == NULL)
2738 return -1;
2739
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002740 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002741 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002742
2743 shell->destroy_listener.notify = shell_destroy;
2744 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2745 shell->lock_listener.notify = lock;
2746 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2747 shell->unlock_listener.notify = unlock;
2748 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002749 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002750 ec->shell_interface.create_shell_surface = create_shell_surface;
2751 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002752 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002753 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002754 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002755
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002756 wl_list_init(&shell->backgrounds);
2757 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002758 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002759
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002760 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2761 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2762 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2763 weston_layer_init(&shell->background_layer,
2764 &shell->toplevel_layer.link);
2765 wl_list_init(&shell->lock_layer.surface_list);
2766
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002767 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002768
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002769 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2770 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002771 return -1;
2772
Kristian Høgsberg75840622011-09-06 13:48:16 -04002773 if (wl_display_add_global(ec->wl_display,
2774 &desktop_shell_interface,
2775 shell, bind_desktop_shell) == NULL)
2776 return -1;
2777
Pekka Paalanen6e168112011-11-24 11:34:05 +02002778 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2779 shell, bind_screensaver) == NULL)
2780 return -1;
2781
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002782 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002783 if (launch_desktop_shell_process(shell) != 0)
2784 return -1;
2785
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002786 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03002787 if (ec->seat->seat.pointer)
2788 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
2789 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002790
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002791 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002792
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002793 return 0;
2794}