blob: ba421b566e5f72cfe31d60d77a2ed79606772de7 [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 {
179 int32_t x;
180 int32_t y;
181 } center;
182};
183
Alex Wubd3354b2012-04-17 17:20:49 +0800184static struct shell_surface *
185get_shell_surface(struct weston_surface *surface);
186
187static struct desktop_shell *
188shell_surface_get_shell(struct shell_surface *shsurf);
189
190static bool
191shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
192{
193 struct desktop_shell *shell;
194 struct weston_surface *top_fs_es;
195
196 shell = shell_surface_get_shell(shsurf);
197
198 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
199 return false;
200
201 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
202 struct weston_surface,
203 layer_link);
204 return (shsurf == get_shell_surface(top_fs_es));
205}
206
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500207static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400208destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209{
210 struct shell_grab *grab;
211
212 grab = container_of(listener, struct shell_grab,
213 shsurf_destroy_listener);
214
215 grab->shsurf = NULL;
216}
217
218static void
219shell_grab_init(struct shell_grab *grab,
220 const struct wl_pointer_grab_interface *interface,
221 struct shell_surface *shsurf)
222{
223 grab->grab.interface = interface;
224 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400225 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
226 wl_signal_add(&shsurf->resource.destroy_signal,
227 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228
229}
230
231static void
232shell_grab_finish(struct shell_grab *grab)
233{
234 wl_list_remove(&grab->shsurf_destroy_listener.link);
235}
236
237static void
Alex Wu4539b082012-03-01 12:57:46 +0800238center_on_output(struct weston_surface *surface,
239 struct weston_output *output);
240
Daniel Stone496ca172012-05-30 16:31:42 +0100241static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300242get_modifier(char *modifier)
243{
244 if (!modifier)
245 return MODIFIER_SUPER;
246
247 if (!strcmp("ctrl", modifier))
248 return MODIFIER_CTRL;
249 else if (!strcmp("alt", modifier))
250 return MODIFIER_ALT;
251 else if (!strcmp("super", modifier))
252 return MODIFIER_SUPER;
253 else
254 return MODIFIER_SUPER;
255}
256
Juan Zhaoe10d2792012-04-25 19:09:52 +0800257static enum animation_type
258get_animation_type(char *animation)
259{
260 if (!animation)
261 return ANIMATION_NONE;
262
263 if (!strcmp("zoom", animation))
264 return ANIMATION_ZOOM;
265 else if (!strcmp("fade", animation))
266 return ANIMATION_FADE;
267 else
268 return ANIMATION_NONE;
269}
270
Alex Wu4539b082012-03-01 12:57:46 +0800271static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300272shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200273{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200274 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200275 char *path = NULL;
276 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300277 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800278 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200279
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400280 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300281 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800282 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200283 };
284
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400285 struct config_key saver_keys[] = {
286 { "path", CONFIG_KEY_STRING, &path },
287 { "duration", CONFIG_KEY_INTEGER, &duration },
288 };
289
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200290 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400291 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200292 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
293 };
294
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200295 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500296 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200297 free(config_file);
298
Pekka Paalanen7296e792011-12-07 16:22:00 +0200299 shell->screensaver.path = path;
300 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300301 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800302 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200303}
304
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200305static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400306noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100307 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500308{
309 grab->focus = NULL;
310}
311
312static void
Scott Moreau447013d2012-02-18 05:05:29 -0700313move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100314 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100317 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300318 struct shell_surface *shsurf = move->base.shsurf;
319 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100320 int dx = wl_fixed_to_int(pointer->x + move->dx);
321 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322
323 if (!shsurf)
324 return;
325
326 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500327
Daniel Stone103db7f2012-05-08 17:17:55 +0100328 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200329 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500330}
331
332static void
Scott Moreau447013d2012-02-18 05:05:29 -0700333move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100334 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
337 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100338 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100339 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500340
Daniel Stone4dbadb12012-05-30 16:31:51 +0100341 if (pointer->button_count == 0 &&
342 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100344 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500345 free(grab);
346 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500347}
348
Scott Moreau447013d2012-02-18 05:05:29 -0700349static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500350 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500351 move_grab_motion,
352 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500353};
354
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600355static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400356busy_cursor_grab_focus(struct wl_pointer_grab *base,
357 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600358{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400359 struct shell_grab *grab = (struct shell_grab *) base;
360 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600361
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400362 if (grab->grab.focus != surface) {
363 shell_grab_finish(grab);
364 wl_pointer_end_grab(pointer);
365 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600366 }
367}
368
369static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400370busy_cursor_grab_motion(struct wl_pointer_grab *grab,
371 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600372{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400373}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600374
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400375static void
376busy_cursor_grab_button(struct wl_pointer_grab *grab,
377 uint32_t time, uint32_t button, uint32_t state)
378{
379}
380
381static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
382 busy_cursor_grab_focus,
383 busy_cursor_grab_motion,
384 busy_cursor_grab_button,
385};
386
387static void
388set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
389{
390 struct shell_grab *grab;
391 struct desktop_shell *shell = shsurf->shell;
392 struct weston_seat *seat = (struct weston_seat *) pointer->seat;
393 struct weston_surface *sprite;
394
395 grab = malloc(sizeof *grab);
396 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600397 return;
398
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400399 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
400 grab->grab.focus = &shsurf->surface->surface;
401 wl_pointer_start_grab(pointer, &grab->grab);
402 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600403
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400404 sprite = (struct weston_surface *) seat->sprite;
405 shell->busy_surface->output = sprite->output;
406}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600407
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400408static void
409end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
410{
411 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
412
413 if (grab->grab.interface == &busy_cursor_grab_interface) {
414 shell_grab_finish(grab);
415 wl_pointer_end_grab(pointer);
416 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600417 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600418}
419
Scott Moreau9521d5e2012-04-19 13:06:17 -0600420static void
421ping_timer_destroy(struct shell_surface *shsurf)
422{
423 if (!shsurf || !shsurf->ping_timer)
424 return;
425
426 if (shsurf->ping_timer->source)
427 wl_event_source_remove(shsurf->ping_timer->source);
428
429 free(shsurf->ping_timer);
430 shsurf->ping_timer = NULL;
431}
432
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400433static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600434ping_timeout_handler(void *data)
435{
436 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400437 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600438
Scott Moreau9521d5e2012-04-19 13:06:17 -0600439 /* Client is not responding */
440 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400441
442 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
443 if (seat->seat.pointer->focus == &shsurf->surface->surface)
444 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600445
446 return 1;
447}
448
449static void
450ping_handler(struct weston_surface *surface, uint32_t serial)
451{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400452 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600453 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400454 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600455
456 if (!shsurf)
457 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400458 if (!shsurf->resource.client)
459 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600460
461 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300462 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600463 if (!shsurf->ping_timer)
464 return;
465
466 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600467 loop = wl_display_get_event_loop(surface->compositor->wl_display);
468 shsurf->ping_timer->source =
469 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
470 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
471
472 wl_shell_surface_send_ping(&shsurf->resource, serial);
473 }
474}
475
476static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400477handle_pointer_focus(struct wl_listener *listener, void *data)
478{
479 struct wl_pointer *pointer = data;
480 struct weston_surface *surface =
481 (struct weston_surface *) pointer->focus;
482 struct weston_compositor *compositor;
483 struct shell_surface *shsurf;
484 uint32_t serial;
485
486 if (!surface)
487 return;
488
489 compositor = surface->compositor;
490 shsurf = get_shell_surface(surface);
491
492 if (shsurf->unresponsive) {
493 set_busy_cursor(shsurf, pointer);
494 } else {
495 serial = wl_display_next_serial(compositor->wl_display);
496 ping_handler(surface, serial);
497 }
498}
499
500static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600501shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
502 uint32_t serial)
503{
504 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400505 struct desktop_shell *shell = shsurf->shell;
506 struct weston_seat *seat;
507 struct weston_compositor *ec = shsurf->surface->compositor;
508 struct wl_pointer *pointer;
509 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600510
Scott Moreauff1db4a2012-04-17 19:06:18 -0600511 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400512 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600513 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400514 if (was_unresponsive) {
515 /* Received pong from previously unresponsive client */
516 wl_list_for_each(seat, &ec->seat_list, link) {
517 pointer = seat->seat.pointer;
518 if (pointer->focus ==
519 &shell->busy_surface->surface &&
520 pointer->current ==
521 &shsurf->surface->surface)
522 end_busy_cursor(shsurf, pointer);
523 }
524 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600525 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600526 }
527}
528
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400529static void
530shell_surface_set_title(struct wl_client *client,
531 struct wl_resource *resource, const char *title)
532{
533 struct shell_surface *shsurf = resource->data;
534
535 free(shsurf->title);
536 shsurf->title = strdup(title);
537}
538
539static void
540shell_surface_set_class(struct wl_client *client,
541 struct wl_resource *resource, const char *class)
542{
543 struct shell_surface *shsurf = resource->data;
544
545 free(shsurf->class);
546 shsurf->class = strdup(class);
547}
548
Scott Moreauff1db4a2012-04-17 19:06:18 -0600549static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400550surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500551{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500552 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300553
554 if (!shsurf)
555 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500556
557 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400558 if (!move)
559 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500560
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300561 shell_grab_init(&move->base, &move_grab_interface, shsurf);
562
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400563 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100564 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400565 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100566 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500567
Daniel Stone37816df2012-05-16 18:45:18 +0100568 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500569
Daniel Stone37816df2012-05-16 18:45:18 +0100570 wl_pointer_set_focus(ws->seat.pointer, NULL,
571 wl_fixed_from_int(0),
572 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400573
574 return 0;
575}
576
577static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200578shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100579 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400580{
Daniel Stone37816df2012-05-16 18:45:18 +0100581 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200582 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400583
Daniel Stone37816df2012-05-16 18:45:18 +0100584 if (ws->seat.pointer->button_count == 0 ||
585 ws->seat.pointer->grab_serial != serial ||
586 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500587 return;
588
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400589 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400590 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500591}
592
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300594 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500595 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200596 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500597};
598
599static void
Scott Moreau447013d2012-02-18 05:05:29 -0700600resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100601 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500602{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500603 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100604 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400605 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500606 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100607 wl_fixed_t from_x, from_y;
608 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200609
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400610 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300611 return;
612
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400613 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100614 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100615 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400616 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100617 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500618
Daniel Stone103db7f2012-05-08 17:17:55 +0100619 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200620 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100621 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200622 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100623 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500624 }
625
Daniel Stone103db7f2012-05-08 17:17:55 +0100626 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200627 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100628 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200629 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100630 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500631 }
632
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400633 shsurf->client->send_configure(shsurf->surface,
634 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500635}
636
637static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400638send_configure(struct weston_surface *surface,
639 uint32_t edges, int32_t width, int32_t height)
640{
641 struct shell_surface *shsurf = get_shell_surface(surface);
642
643 wl_shell_surface_send_configure(&shsurf->resource,
644 edges, width, height);
645}
646
647static const struct weston_shell_client shell_client = {
648 send_configure
649};
650
651static void
Scott Moreau447013d2012-02-18 05:05:29 -0700652resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100653 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500654{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300655 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100656 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100657 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500658
Daniel Stone4dbadb12012-05-30 16:31:51 +0100659 if (pointer->button_count == 0 &&
660 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300661 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100662 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500663 free(grab);
664 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500665}
666
Scott Moreau447013d2012-02-18 05:05:29 -0700667static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500668 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500669 resize_grab_motion,
670 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500671};
672
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400673static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400674surface_resize(struct shell_surface *shsurf,
675 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500676{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500677 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500678
Alex Wu4539b082012-03-01 12:57:46 +0800679 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
680 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500681
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200682 if (edges == 0 || edges > 15 ||
683 (edges & 3) == 3 || (edges & 12) == 12)
684 return 0;
685
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500686 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400687 if (!resize)
688 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500689
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300690 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
691
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500692 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200693 resize->width = shsurf->surface->geometry.width;
694 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400695
Daniel Stone37816df2012-05-16 18:45:18 +0100696 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500697
Daniel Stone37816df2012-05-16 18:45:18 +0100698 wl_pointer_set_focus(ws->seat.pointer, NULL,
699 wl_fixed_from_int(0),
700 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400701
702 return 0;
703}
704
705static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200706shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100707 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200708 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400709{
Daniel Stone37816df2012-05-16 18:45:18 +0100710 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200711 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400712
Alex Wu4539b082012-03-01 12:57:46 +0800713 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
714 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400715
Daniel Stone37816df2012-05-16 18:45:18 +0100716 if (ws->seat.pointer->button_count == 0 ||
717 ws->seat.pointer->grab_serial != serial ||
718 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500719 return;
720
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400721 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400722 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500723}
724
Juan Zhao96879df2012-02-07 08:45:41 +0800725static struct weston_output *
726get_default_output(struct weston_compositor *compositor)
727{
728 return container_of(compositor->output_list.next,
729 struct weston_output, link);
730}
731
Alex Wu4539b082012-03-01 12:57:46 +0800732static void
733shell_unset_fullscreen(struct shell_surface *shsurf)
734{
735 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800736 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
737 shell_surface_is_top_fullscreen(shsurf)) {
738 weston_output_switch_mode(shsurf->fullscreen_output,
739 shsurf->fullscreen_output->origin);
740 }
Alex Wu4539b082012-03-01 12:57:46 +0800741 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
742 shsurf->fullscreen.framerate = 0;
743 wl_list_remove(&shsurf->fullscreen.transform.link);
744 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800745 if (shsurf->fullscreen.black_surface)
746 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800747 shsurf->fullscreen.black_surface = NULL;
748 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800749 weston_surface_set_position(shsurf->surface,
750 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800751 if (shsurf->saved_rotation_valid) {
752 wl_list_insert(&shsurf->surface->geometry.transformation_list,
753 &shsurf->rotation.transform.link);
754 shsurf->saved_rotation_valid = false;
755 }
Alex Wu4539b082012-03-01 12:57:46 +0800756}
757
Pekka Paalanen98262232011-12-01 10:42:22 +0200758static int
759reset_shell_surface_type(struct shell_surface *surface)
760{
761 switch (surface->type) {
762 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800763 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200764 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800765 case SHELL_SURFACE_MAXIMIZED:
766 surface->output = get_default_output(surface->surface->compositor);
767 weston_surface_set_position(surface->surface,
768 surface->saved_x,
769 surface->saved_y);
770 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200771 case SHELL_SURFACE_PANEL:
772 case SHELL_SURFACE_BACKGROUND:
773 wl_list_remove(&surface->link);
774 wl_list_init(&surface->link);
775 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200776 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200777 case SHELL_SURFACE_LOCK:
778 wl_resource_post_error(&surface->resource,
779 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200780 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200781 return -1;
782 case SHELL_SURFACE_NONE:
783 case SHELL_SURFACE_TOPLEVEL:
784 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500785 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200786 break;
787 }
788
789 surface->type = SHELL_SURFACE_NONE;
790 return 0;
791}
792
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500793static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400794set_surface_type(struct shell_surface *shsurf)
795{
796 struct weston_surface *surface = shsurf->surface;
797 struct shell_surface *pshsurf = shsurf->parent;
798 struct weston_surface *pes;
799 struct shell_surface *priv;
800 struct desktop_shell *shell = shsurf->shell;
801
802 reset_shell_surface_type(shsurf);
803
804 shsurf->type = shsurf->next_type;
805 shsurf->next_type = SHELL_SURFACE_NONE;
806
807 switch (shsurf->type) {
808 case SHELL_SURFACE_TOPLEVEL:
809 break;
810 case SHELL_SURFACE_TRANSIENT:
811 pes = pshsurf->surface;
812 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300813 pes->geometry.x + shsurf->transient.x,
814 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400815 break;
816
817 case SHELL_SURFACE_MAXIMIZED:
818 shsurf->saved_x = surface->geometry.x;
819 shsurf->saved_y = surface->geometry.y;
820 shsurf->saved_position_valid = true;
821 break;
822
823 case SHELL_SURFACE_FULLSCREEN:
824 shsurf->saved_x = surface->geometry.x;
825 shsurf->saved_y = surface->geometry.y;
826 shsurf->saved_position_valid = true;
827
828 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
829 wl_list_remove(&shsurf->rotation.transform.link);
830 wl_list_init(&shsurf->rotation.transform.link);
831 shsurf->surface->geometry.dirty = 1;
832 shsurf->saved_rotation_valid = true;
833 }
834 break;
835
836 case SHELL_SURFACE_BACKGROUND:
837 wl_list_for_each(priv, &shell->backgrounds, link) {
838 if (priv->output == shsurf->output) {
839 priv->surface->output = NULL;
840 wl_list_remove(&priv->surface->layer_link);
841 wl_list_remove(&priv->link);
842 break;
843 }
844 }
845
846 wl_list_insert(&shell->backgrounds, &shsurf->link);
847
848 weston_surface_set_position(surface, shsurf->output->x,
849 shsurf->output->y);
850 break;
851
852 case SHELL_SURFACE_PANEL:
853 wl_list_for_each(priv, &shell->panels, link) {
854 if (priv->output == shsurf->output) {
855 priv->surface->output = NULL;
856 wl_list_remove(&priv->surface->layer_link);
857 wl_list_remove(&priv->link);
858 break;
859 }
860 }
861
862 wl_list_insert(&shell->panels, &shsurf->link);
863
864 weston_surface_set_position(surface, shsurf->output->x,
865 shsurf->output->y);
866 break;
867
868 default:
869 break;
870 }
871}
872
873static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300874set_toplevel(struct shell_surface *shsurf)
875{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400876 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300877}
878
879static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200880shell_surface_set_toplevel(struct wl_client *client,
881 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400882{
Pekka Paalanen98262232011-12-01 10:42:22 +0200883 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400884
Tiago Vignattibc052c92012-04-19 16:18:18 +0300885 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400886}
887
888static void
Tiago Vignatti491bac12012-05-18 16:37:43 -0400889set_transient(struct shell_surface *shsurf,
890 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
891{
892 /* assign to parents output */
893 shsurf->parent = pshsurf;
894 shsurf->transient.x = x;
895 shsurf->transient.y = y;
896 shsurf->transient.flags = flags;
897 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
898}
899
900static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200901shell_surface_set_transient(struct wl_client *client,
902 struct wl_resource *resource,
903 struct wl_resource *parent_resource,
904 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400905{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200906 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400907 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200908
Tiago Vignatti491bac12012-05-18 16:37:43 -0400909 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400910}
911
Tiago Vignattibe143262012-04-16 17:31:41 +0300912static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800913shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200914{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400915 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800916}
917
918static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300919get_output_panel_height(struct desktop_shell *shell,
920 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800921{
922 struct shell_surface *priv;
923 int panel_height = 0;
924
925 if (!output)
926 return 0;
927
Tiago Vignattibe143262012-04-16 17:31:41 +0300928 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800929 if (priv->output == output) {
930 panel_height = priv->surface->geometry.height;
931 break;
932 }
933 }
934 return panel_height;
935}
936
937static void
938shell_surface_set_maximized(struct wl_client *client,
939 struct wl_resource *resource,
940 struct wl_resource *output_resource )
941{
942 struct shell_surface *shsurf = resource->data;
943 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300944 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800945 uint32_t edges = 0, panel_height = 0;
946
947 /* get the default output, if the client set it as NULL
948 check whether the ouput is available */
949 if (output_resource)
950 shsurf->output = output_resource->data;
951 else
952 shsurf->output = get_default_output(es->compositor);
953
Tiago Vignattibe143262012-04-16 17:31:41 +0300954 shell = shell_surface_get_shell(shsurf);
955 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800956 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500957
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400958 shsurf->client->send_configure(shsurf->surface, edges,
959 es->output->current->width,
960 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800961
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400962 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200963}
964
Alex Wu21858432012-04-01 20:13:08 +0800965static void
966black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
967
Alex Wu4539b082012-03-01 12:57:46 +0800968static struct weston_surface *
969create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800970 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800971 GLfloat x, GLfloat y, int w, int h)
972{
973 struct weston_surface *surface = NULL;
974
975 surface = weston_surface_create(ec);
976 if (surface == NULL) {
977 fprintf(stderr, "no memory\n");
978 return NULL;
979 }
980
Alex Wu21858432012-04-01 20:13:08 +0800981 surface->configure = black_surface_configure;
982 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800983 weston_surface_configure(surface, x, y, w, h);
984 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
985 return surface;
986}
987
988/* Create black surface and append it to the associated fullscreen surface.
989 * Handle size dismatch and positioning according to the method. */
990static void
991shell_configure_fullscreen(struct shell_surface *shsurf)
992{
993 struct weston_output *output = shsurf->fullscreen_output;
994 struct weston_surface *surface = shsurf->surface;
995 struct weston_matrix *matrix;
996 float scale;
997
998 center_on_output(surface, output);
999
1000 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001001 shsurf->fullscreen.black_surface =
1002 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001003 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001004 output->x, output->y,
1005 output->current->width,
1006 output->current->height);
1007
1008 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1009 wl_list_insert(&surface->layer_link,
1010 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001011 shsurf->fullscreen.black_surface->output = output;
1012
1013 switch (shsurf->fullscreen.type) {
1014 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1015 break;
1016 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1017 matrix = &shsurf->fullscreen.transform.matrix;
1018 weston_matrix_init(matrix);
1019 scale = (float)output->current->width/(float)surface->geometry.width;
1020 weston_matrix_scale(matrix, scale, scale, 1);
1021 wl_list_remove(&shsurf->fullscreen.transform.link);
1022 wl_list_insert(surface->geometry.transformation_list.prev,
1023 &shsurf->fullscreen.transform.link);
1024 weston_surface_set_position(surface, output->x, output->y);
1025 break;
1026 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001027 if (shell_surface_is_top_fullscreen(shsurf)) {
1028 struct weston_mode mode = {0,
1029 surface->geometry.width,
1030 surface->geometry.height,
1031 shsurf->fullscreen.framerate};
1032
1033 if (weston_output_switch_mode(output, &mode) == 0) {
1034 weston_surface_configure(shsurf->fullscreen.black_surface,
1035 output->x, output->y,
1036 output->current->width,
1037 output->current->height);
1038 weston_surface_set_position(surface, output->x, output->y);
1039 break;
1040 }
1041 }
Alex Wu4539b082012-03-01 12:57:46 +08001042 break;
1043 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1044 break;
1045 default:
1046 break;
1047 }
1048}
1049
1050/* make the fullscreen and black surface at the top */
1051static void
1052shell_stack_fullscreen(struct shell_surface *shsurf)
1053{
Alex Wubd3354b2012-04-17 17:20:49 +08001054 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001055 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001056 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001057
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001058 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001059 wl_list_insert(&shell->fullscreen_layer.surface_list,
1060 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001061 weston_surface_damage(surface);
1062
1063 if (!shsurf->fullscreen.black_surface)
1064 shsurf->fullscreen.black_surface =
1065 create_black_surface(surface->compositor,
1066 surface,
1067 output->x, output->y,
1068 output->current->width,
1069 output->current->height);
1070
1071 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001072 wl_list_insert(&surface->layer_link,
1073 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001074 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001075}
1076
1077static void
1078shell_map_fullscreen(struct shell_surface *shsurf)
1079{
Alex Wu4539b082012-03-01 12:57:46 +08001080 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001081 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001082}
1083
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001084static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001085shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001086 struct wl_resource *resource,
1087 uint32_t method,
1088 uint32_t framerate,
1089 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001090{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001091 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001092 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001093
1094 if (output_resource)
1095 shsurf->output = output_resource->data;
1096 else
1097 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001098
Alex Wu4539b082012-03-01 12:57:46 +08001099 shsurf->fullscreen_output = shsurf->output;
1100 shsurf->fullscreen.type = method;
1101 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001102 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001103
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001104 shsurf->client->send_configure(shsurf->surface, 0,
1105 shsurf->output->current->width,
1106 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001107}
1108
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001109static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001110popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001111 struct wl_surface *surface,
1112 wl_fixed_t x,
1113 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001114{
Daniel Stone37816df2012-05-16 18:45:18 +01001115 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001116 struct shell_surface *priv =
1117 container_of(grab, struct shell_surface, popup.grab);
1118 struct wl_client *client = priv->surface->surface.resource.client;
1119
Pekka Paalanencb108432012-01-19 16:25:40 +02001120 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001121 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001122 grab->focus = surface;
1123 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001124 wl_pointer_set_focus(pointer, NULL,
1125 wl_fixed_from_int(0),
1126 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001127 grab->focus = NULL;
1128 }
1129}
1130
1131static void
Scott Moreau447013d2012-02-18 05:05:29 -07001132popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001133 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001134{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001135 struct wl_resource *resource;
1136
Daniel Stone37816df2012-05-16 18:45:18 +01001137 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001138 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001139 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001140}
1141
1142static void
Scott Moreau447013d2012-02-18 05:05:29 -07001143popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001144 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001145{
1146 struct wl_resource *resource;
1147 struct shell_surface *shsurf =
1148 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001149 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001150 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001151 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001152
Daniel Stone37816df2012-05-16 18:45:18 +01001153 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001154 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001155 display = wl_client_get_display(resource->client);
1156 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001157 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001158 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001159 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001160 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001161 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001162 wl_pointer_end_grab(grab->pointer);
1163 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001164 }
1165
Daniel Stone4dbadb12012-05-30 16:31:51 +01001166 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001167 shsurf->popup.initial_up = 1;
1168}
1169
Scott Moreau447013d2012-02-18 05:05:29 -07001170static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001171 popup_grab_focus,
1172 popup_grab_motion,
1173 popup_grab_button,
1174};
1175
1176static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001177shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001178{
Daniel Stone37816df2012-05-16 18:45:18 +01001179 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001180 struct weston_surface *es = shsurf->surface;
1181 struct weston_surface *parent = shsurf->parent->surface;
1182
1183 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001184 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001185
Pekka Paalanen938269a2012-02-07 14:19:01 +02001186 weston_surface_update_transform(parent);
1187 if (parent->transform.enabled) {
1188 shsurf->popup.parent_transform.matrix =
1189 parent->transform.matrix;
1190 } else {
1191 /* construct x, y translation matrix */
1192 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1193 shsurf->popup.parent_transform.matrix.d[12] =
1194 parent->geometry.x;
1195 shsurf->popup.parent_transform.matrix.d[13] =
1196 parent->geometry.y;
1197 }
1198 wl_list_insert(es->geometry.transformation_list.prev,
1199 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001200 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001201
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001202 shsurf->popup.initial_up = 0;
1203
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001204 /* We don't require the grab to still be active, but if another
1205 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001206 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1207 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001208 } else {
1209 wl_shell_surface_send_popup_done(&shsurf->resource);
1210 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001211}
1212
1213static void
1214shell_surface_set_popup(struct wl_client *client,
1215 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001216 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001217 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001218 struct wl_resource *parent_resource,
1219 int32_t x, int32_t y, uint32_t flags)
1220{
1221 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001222
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001223 shsurf->type = SHELL_SURFACE_POPUP;
1224 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001225 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001226 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001227 shsurf->popup.x = x;
1228 shsurf->popup.y = y;
1229}
1230
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001231static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001232 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001233 shell_surface_move,
1234 shell_surface_resize,
1235 shell_surface_set_toplevel,
1236 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001237 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001238 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001239 shell_surface_set_maximized,
1240 shell_surface_set_title,
1241 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001242};
1243
1244static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001245destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001246{
Daniel Stone37816df2012-05-16 18:45:18 +01001247 if (shsurf->popup.grab.pointer)
1248 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001249
Alex Wubd3354b2012-04-17 17:20:49 +08001250 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1251 shell_surface_is_top_fullscreen(shsurf)) {
1252 weston_output_switch_mode(shsurf->fullscreen_output,
1253 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001254 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001255
Alex Wuaa08e2d2012-03-05 11:01:40 +08001256 if (shsurf->fullscreen.black_surface)
1257 weston_surface_destroy(shsurf->fullscreen.black_surface);
1258
Alex Wubd3354b2012-04-17 17:20:49 +08001259 /* As destroy_resource() use wl_list_for_each_safe(),
1260 * we can always remove the listener.
1261 */
1262 wl_list_remove(&shsurf->surface_destroy_listener.link);
1263 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001264 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001265
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001266 wl_list_remove(&shsurf->link);
1267 free(shsurf);
1268}
1269
1270static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001271shell_destroy_shell_surface(struct wl_resource *resource)
1272{
1273 struct shell_surface *shsurf = resource->data;
1274
1275 destroy_shell_surface(shsurf);
1276}
1277
1278static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001279shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001280{
1281 struct shell_surface *shsurf = container_of(listener,
1282 struct shell_surface,
1283 surface_destroy_listener);
1284
Tiago Vignattibc052c92012-04-19 16:18:18 +03001285 /* tricky way to check if resource was in fact created */
1286 if (shsurf->resource.object.implementation != 0)
1287 wl_resource_destroy(&shsurf->resource);
1288 else
1289 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001290}
1291
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001292static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001294{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001295 struct wl_listener *listener;
1296
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001297 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1298 shell_handle_surface_destroy);
1299 if (listener)
1300 return container_of(listener, struct shell_surface,
1301 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001302
1303 return NULL;
1304}
1305
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001306static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001307shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1308
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001309static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001310create_shell_surface(void *shell, struct weston_surface *surface,
1311 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001312{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001313 struct shell_surface *shsurf;
1314
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001315 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001316 fprintf(stderr, "surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001317 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001318 }
1319
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001320 shsurf = calloc(1, sizeof *shsurf);
1321 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001322 fprintf(stderr, "no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001323 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001324 }
1325
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001326 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001327 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001328
Tiago Vignattibc052c92012-04-19 16:18:18 +03001329 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001330 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001331 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001332 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001333 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001334 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1335 shsurf->fullscreen.framerate = 0;
1336 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001337 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001338 wl_list_init(&shsurf->fullscreen.transform.link);
1339
Tiago Vignattibc052c92012-04-19 16:18:18 +03001340 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001341 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1342 wl_signal_add(&surface->surface.resource.destroy_signal,
1343 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001344
1345 /* init link so its safe to always remove it in destroy_shell_surface */
1346 wl_list_init(&shsurf->link);
1347
Pekka Paalanen460099f2012-01-20 16:48:25 +02001348 /* empty when not in use */
1349 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001350 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001351
Pekka Paalanen98262232011-12-01 10:42:22 +02001352 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001353 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001354
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001355 shsurf->client = client;
1356
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001357 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001358}
1359
1360static void
1361shell_get_shell_surface(struct wl_client *client,
1362 struct wl_resource *resource,
1363 uint32_t id,
1364 struct wl_resource *surface_resource)
1365{
1366 struct weston_surface *surface = surface_resource->data;
1367 struct desktop_shell *shell = resource->data;
1368 struct shell_surface *shsurf;
1369
1370 if (get_shell_surface(surface)) {
1371 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001372 WL_DISPLAY_ERROR_INVALID_OBJECT,
1373 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001374 return;
1375 }
1376
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001377 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001378 if (!shsurf) {
1379 wl_resource_post_error(surface_resource,
1380 WL_DISPLAY_ERROR_INVALID_OBJECT,
1381 "surface->configure already set");
1382 return;
1383 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001384
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001385 shsurf->resource.destroy = shell_destroy_shell_surface;
1386 shsurf->resource.object.id = id;
1387 shsurf->resource.object.interface = &wl_shell_surface_interface;
1388 shsurf->resource.object.implementation =
1389 (void (**)(void)) &shell_surface_implementation;
1390 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001391
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001392 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001393}
1394
1395static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001396 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001397};
1398
Kristian Høgsberg07937562011-04-12 17:25:42 -04001399static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001400handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001401{
1402 proc->pid = 0;
1403}
1404
1405static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001406launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001407{
1408 if (shell->screensaver.binding)
1409 return;
1410
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001411 if (!shell->screensaver.path)
1412 return;
1413
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001414 if (shell->screensaver.process.pid != 0) {
1415 fprintf(stderr, "old screensaver still running\n");
1416 return;
1417 }
1418
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001420 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001421 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001422 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001423}
1424
1425static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001426terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001427{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001428 if (shell->screensaver.process.pid == 0)
1429 return;
1430
1431 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001432}
1433
1434static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001435show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001436{
1437 struct wl_list *list;
1438
1439 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001440 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001441 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001442 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001443
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001444 wl_list_remove(&surface->surface->layer_link);
1445 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001446 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001447 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001448}
1449
1450static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001451hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001452{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001453 wl_list_remove(&surface->surface->layer_link);
1454 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001455 surface->surface->output = NULL;
1456}
1457
1458static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001459desktop_shell_set_background(struct wl_client *client,
1460 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001461 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001462 struct wl_resource *surface_resource)
1463{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001464 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001465
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001466 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001467 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001468
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001469 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001470 surface_resource,
1471 shsurf->output->current->width,
1472 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001473}
1474
1475static void
1476desktop_shell_set_panel(struct wl_client *client,
1477 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001478 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001479 struct wl_resource *surface_resource)
1480{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001481 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001482
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001483 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001484 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001485
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001486 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001487 surface_resource,
1488 shsurf->output->current->width,
1489 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001490}
1491
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001492static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001493handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001494{
Tiago Vignattibe143262012-04-16 17:31:41 +03001495 struct desktop_shell *shell =
1496 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001497
1498 fprintf(stderr, "lock surface gone\n");
1499 shell->lock_surface = NULL;
1500}
1501
1502static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001503desktop_shell_set_lock_surface(struct wl_client *client,
1504 struct wl_resource *resource,
1505 struct wl_resource *surface_resource)
1506{
Tiago Vignattibe143262012-04-16 17:31:41 +03001507 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001508 struct shell_surface *surface = surface_resource->data;
1509
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001510 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001511
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001512 if (!shell->locked)
1513 return;
1514
Pekka Paalanen98262232011-12-01 10:42:22 +02001515 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001516
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001517 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1518 wl_signal_add(&surface_resource->destroy_signal,
1519 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001520
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001521 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001522}
1523
1524static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001525resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001526{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001527 struct shell_surface *tmp;
1528
1529 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1530 hide_screensaver(shell, tmp);
1531
1532 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001533
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001534 wl_list_remove(&shell->lock_layer.link);
1535 wl_list_insert(&shell->compositor->cursor_layer.link,
1536 &shell->fullscreen_layer.link);
1537 wl_list_insert(&shell->fullscreen_layer.link,
1538 &shell->panel_layer.link);
1539 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001540
1541 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001542 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001543 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001544 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001545}
1546
1547static void
1548desktop_shell_unlock(struct wl_client *client,
1549 struct wl_resource *resource)
1550{
Tiago Vignattibe143262012-04-16 17:31:41 +03001551 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001552
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001553 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001554
1555 if (shell->locked)
1556 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001557}
1558
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001559static void
1560desktop_shell_set_busy_surface(struct wl_client *client,
1561 struct wl_resource *resource,
1562 struct wl_resource *surface_resource)
1563{
1564 struct desktop_shell *shell = resource->data;
1565
1566 shell->busy_surface = surface_resource->data;
1567}
1568
Kristian Høgsberg75840622011-09-06 13:48:16 -04001569static const struct desktop_shell_interface desktop_shell_implementation = {
1570 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001571 desktop_shell_set_panel,
1572 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001573 desktop_shell_unlock,
1574 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04001575};
1576
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001577static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001578get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001579{
1580 struct shell_surface *shsurf;
1581
1582 shsurf = get_shell_surface(surface);
1583 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001584 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001585 return shsurf->type;
1586}
1587
Kristian Høgsberg75840622011-09-06 13:48:16 -04001588static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001589move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001590{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001592 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001593 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001594
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001595 if (surface == NULL)
1596 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001597
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001598 shsurf = get_shell_surface(surface);
1599 if (shsurf == NULL)
1600 return;
1601
1602 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001603 case SHELL_SURFACE_PANEL:
1604 case SHELL_SURFACE_BACKGROUND:
1605 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001606 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001607 return;
1608 default:
1609 break;
1610 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001611
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001612 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001613}
1614
1615static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001616resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001617{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001618 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001619 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001620 uint32_t edges = 0;
1621 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001622 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001623
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001624 if (surface == NULL)
1625 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001626
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001627 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001628 if (!shsurf)
1629 return;
1630
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001631 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001632 case SHELL_SURFACE_PANEL:
1633 case SHELL_SURFACE_BACKGROUND:
1634 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001635 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001636 return;
1637 default:
1638 break;
1639 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001640
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001641 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001642 wl_fixed_to_int(seat->pointer->grab_x),
1643 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001644 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001645
Pekka Paalanen60921e52012-01-25 15:55:43 +02001646 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001647 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001648 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001649 edges |= 0;
1650 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001651 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001652
Pekka Paalanen60921e52012-01-25 15:55:43 +02001653 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001654 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001655 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001656 edges |= 0;
1657 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001658 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001659
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001660 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001661}
1662
1663static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001664surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001665 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001666{
Scott Moreau02709af2012-05-22 01:54:10 -06001667 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001668 struct shell_surface *shsurf;
1669 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001670 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001671
1672 if (surface == NULL)
1673 return;
1674
1675 shsurf = get_shell_surface(surface);
1676 if (!shsurf)
1677 return;
1678
1679 switch (shsurf->type) {
1680 case SHELL_SURFACE_BACKGROUND:
1681 case SHELL_SURFACE_SCREENSAVER:
1682 return;
1683 default:
1684 break;
1685 }
1686
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001687 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001688
Scott Moreau02709af2012-05-22 01:54:10 -06001689 if (surface->alpha > 1.0)
1690 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001691 if (surface->alpha < step)
1692 surface->alpha = step;
1693
1694 surface->geometry.dirty = 1;
1695 weston_surface_damage(surface);
1696}
1697
1698static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001699do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001700 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001701{
Daniel Stone37816df2012-05-16 18:45:18 +01001702 struct weston_seat *ws = (struct weston_seat *) seat;
1703 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001704 struct weston_output *output;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001705 float maximum_level, increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001706
1707 wl_list_for_each(output, &compositor->output_list, link) {
1708 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01001709 wl_fixed_to_double(seat->pointer->x),
1710 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001711 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01001712 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001713 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01001714 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001715 increment = -output->zoom.increment;
1716 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001717 increment = output->zoom.increment *
1718 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001719 else
1720 increment = 0;
1721
Scott Moreau1b45a792012-03-22 10:58:23 -06001722 output->zoom.active = 1;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001723 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001724
Scott Moreau850ca422012-05-21 15:21:25 -06001725 if (output->zoom.level <= 0.0) {
Scott Moreauc6d7f602012-02-23 22:28:37 -07001726 output->zoom.active = 0;
Scott Moreau850ca422012-05-21 15:21:25 -06001727 output->zoom.level = 0.0;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001728 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001729
Scott Moreau850ca422012-05-21 15:21:25 -06001730 maximum_level = 1 - output->zoom.increment;
1731
1732 if (output->zoom.level > maximum_level)
1733 output->zoom.level = maximum_level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001734
Daniel Stone37816df2012-05-16 18:45:18 +01001735 weston_output_update_zoom(output,
1736 seat->pointer->x,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001737 seat->pointer->y,
1738 ZOOM_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001739 }
1740 }
1741}
1742
Scott Moreauccbf29d2012-02-22 14:21:41 -07001743static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001744zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001745 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001746{
1747 do_zoom(seat, time, 0, axis, value);
1748}
1749
1750static void
1751zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1752 void *data)
1753{
1754 do_zoom(seat, time, key, 0, 0);
1755}
1756
1757static void
1758terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1759 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001760{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001761 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001762
Daniel Stone325fc2d2012-05-30 16:31:58 +01001763 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001764}
1765
1766static void
Scott Moreau447013d2012-02-18 05:05:29 -07001767rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001768 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001769{
1770 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001771 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001772 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001773 struct shell_surface *shsurf = rotate->base.shsurf;
1774 struct weston_surface *surface;
1775 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1776
1777 if (!shsurf)
1778 return;
1779
1780 surface = shsurf->surface;
1781
1782 cx = 0.5f * surface->geometry.width;
1783 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001784
Daniel Stone37816df2012-05-16 18:45:18 +01001785 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1786 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001787 r = sqrtf(dx * dx + dy * dy);
1788
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001789 wl_list_remove(&shsurf->rotation.transform.link);
1790 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001791
1792 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001793 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001794 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001795
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001796 weston_matrix_init(&rotate->rotation);
1797 rotate->rotation.d[0] = dx / r;
1798 rotate->rotation.d[4] = -dy / r;
1799 rotate->rotation.d[1] = -rotate->rotation.d[4];
1800 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001801
1802 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001803 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001804 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001805 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001806 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001807
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001808 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001809 &shsurf->surface->geometry.transformation_list,
1810 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001811 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001812 wl_list_init(&shsurf->rotation.transform.link);
1813 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001814 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001815 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001816
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001817 /* We need to adjust the position of the surface
1818 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001819 cposx = surface->geometry.x + cx;
1820 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001821 dposx = rotate->center.x - cposx;
1822 dposy = rotate->center.y - cposy;
1823 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001824 weston_surface_set_position(surface,
1825 surface->geometry.x + dposx,
1826 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001827 }
1828
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001829 /* Repaint implies weston_surface_update_transform(), which
1830 * lazily applies the damage due to rotation update.
1831 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001832 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001833}
1834
1835static void
Scott Moreau447013d2012-02-18 05:05:29 -07001836rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001837 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001838{
1839 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001840 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001841 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001842 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001843 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001844
Daniel Stone4dbadb12012-05-30 16:31:51 +01001845 if (pointer->button_count == 0 &&
1846 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001847 if (shsurf)
1848 weston_matrix_multiply(&shsurf->rotation.rotation,
1849 &rotate->rotation);
1850 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001851 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001852 free(rotate);
1853 }
1854}
1855
Scott Moreau447013d2012-02-18 05:05:29 -07001856static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001857 noop_grab_focus,
1858 rotate_grab_motion,
1859 rotate_grab_button,
1860};
1861
1862static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001863rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01001864 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001865{
1866 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001867 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001868 struct shell_surface *surface;
1869 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001870 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001871 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001872
1873 if (base_surface == NULL)
1874 return;
1875
1876 surface = get_shell_surface(base_surface);
1877 if (!surface)
1878 return;
1879
1880 switch (surface->type) {
1881 case SHELL_SURFACE_PANEL:
1882 case SHELL_SURFACE_BACKGROUND:
1883 case SHELL_SURFACE_FULLSCREEN:
1884 case SHELL_SURFACE_SCREENSAVER:
1885 return;
1886 default:
1887 break;
1888 }
1889
Pekka Paalanen460099f2012-01-20 16:48:25 +02001890 rotate = malloc(sizeof *rotate);
1891 if (!rotate)
1892 return;
1893
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001894 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001895
1896 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001897 surface->surface->geometry.width / 2,
1898 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001899 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001900
Daniel Stone37816df2012-05-16 18:45:18 +01001901 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001902
Daniel Stone37816df2012-05-16 18:45:18 +01001903 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1904 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001905 r = sqrtf(dx * dx + dy * dy);
1906 if (r > 20.0f) {
1907 struct weston_matrix inverse;
1908
1909 weston_matrix_init(&inverse);
1910 inverse.d[0] = dx / r;
1911 inverse.d[4] = dy / r;
1912 inverse.d[1] = -inverse.d[4];
1913 inverse.d[5] = inverse.d[0];
1914 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001915
1916 weston_matrix_init(&rotate->rotation);
1917 rotate->rotation.d[0] = dx / r;
1918 rotate->rotation.d[4] = -dy / r;
1919 rotate->rotation.d[1] = -rotate->rotation.d[4];
1920 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001921 } else {
1922 weston_matrix_init(&surface->rotation.rotation);
1923 weston_matrix_init(&rotate->rotation);
1924 }
1925
Daniel Stone37816df2012-05-16 18:45:18 +01001926 wl_pointer_set_focus(seat->pointer, NULL,
1927 wl_fixed_from_int(0),
1928 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02001929}
1930
1931static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001932activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01001933 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001934{
Alex Wu21858432012-04-01 20:13:08 +08001935 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001936
Daniel Stone37816df2012-05-16 18:45:18 +01001937 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001938
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001939 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001940 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001941 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001942 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001943 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001944
Pekka Paalanen77346a62011-11-30 16:26:35 +02001945 case SHELL_SURFACE_SCREENSAVER:
1946 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001947 if (shell->lock_surface)
1948 weston_surface_restack(es,
1949 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001950 break;
Alex Wu4539b082012-03-01 12:57:46 +08001951 case SHELL_SURFACE_FULLSCREEN:
1952 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001953 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001954 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001955 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001956 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001957 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001958 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1959 wl_list_for_each_reverse_safe(surf,
1960 prev,
1961 &shell->fullscreen_layer.surface_list,
1962 layer_link)
1963 weston_surface_restack(surf,
1964 &shell->toplevel_layer.surface_list);
1965 }
1966
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001967 weston_surface_restack(es,
1968 &shell->toplevel_layer.surface_list);
1969 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001970 }
1971}
1972
Alex Wu21858432012-04-01 20:13:08 +08001973/* no-op func for checking black surface */
1974static void
1975black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1976{
1977}
1978
1979static bool
1980is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1981{
1982 if (es->configure == black_surface_configure) {
1983 if (fs_surface)
1984 *fs_surface = (struct weston_surface *)es->private;
1985 return true;
1986 }
1987 return false;
1988}
1989
Kristian Høgsberg75840622011-09-06 13:48:16 -04001990static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001991click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001992 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001993{
Daniel Stone37816df2012-05-16 18:45:18 +01001994 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03001995 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001996 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001997 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001998
Daniel Stone37816df2012-05-16 18:45:18 +01001999 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002000 if (!focus)
2001 return;
2002
Alex Wu21858432012-04-01 20:13:08 +08002003 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002004 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002005
Daniel Stone325fc2d2012-05-30 16:31:58 +01002006 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002007 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002008}
2009
2010static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002011lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002012{
Tiago Vignattibe143262012-04-16 17:31:41 +03002013 struct desktop_shell *shell =
2014 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01002015 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002016 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002017 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002018
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002019 if (shell->locked) {
2020 wl_list_for_each(output, &shell->compositor->output_list, link)
2021 /* TODO: find a way to jump to other DPMS levels */
2022 if (output->set_dpms)
2023 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002024 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002025 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002026
2027 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002028
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002029 /* Hide all surfaces by removing the fullscreen, panel and
2030 * toplevel layers. This way nothing else can show or receive
2031 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002032
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002033 wl_list_remove(&shell->panel_layer.link);
2034 wl_list_remove(&shell->toplevel_layer.link);
2035 wl_list_remove(&shell->fullscreen_layer.link);
2036 wl_list_insert(&shell->compositor->cursor_layer.link,
2037 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002038
Pekka Paalanen77346a62011-11-30 16:26:35 +02002039 launch_screensaver(shell);
2040
2041 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2042 show_screensaver(shell, shsurf);
2043
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002044 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002045 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002046 weston_compositor_wake(shell->compositor);
2047 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002048 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002049
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002050 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002051 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002052
2053 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01002054 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2055 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002056 }
2057
2058 /* TODO: disable bindings that should not work while locked. */
2059
2060 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002061}
2062
2063static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002064unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002065{
Tiago Vignattibe143262012-04-16 17:31:41 +03002066 struct desktop_shell *shell =
2067 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002068
Pekka Paalanend81c2162011-11-16 13:47:34 +02002069 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002070 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002071 return;
2072 }
2073
2074 /* If desktop-shell client has gone away, unlock immediately. */
2075 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002076 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002077 return;
2078 }
2079
2080 if (shell->prepare_event_sent)
2081 return;
2082
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002083 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002084 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002085}
2086
2087static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002088center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002089{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002090 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002091 GLfloat x = (mode->width - surface->geometry.width) / 2;
2092 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002093
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002094 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002095}
2096
2097static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002098map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002099 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002100{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002101 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002102 struct shell_surface *shsurf;
2103 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002104 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002105 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08002106 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002107
Pekka Paalanen77346a62011-11-30 16:26:35 +02002108 shsurf = get_shell_surface(surface);
2109 if (shsurf)
2110 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002111
Pekka Paalanen60921e52012-01-25 15:55:43 +02002112 surface->geometry.width = width;
2113 surface->geometry.height = height;
2114 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002115
2116 /* initial positioning, see also configure() */
2117 switch (surface_type) {
2118 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002119 weston_surface_set_position(surface, 10 + random() % 400,
2120 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002121 break;
2122 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002123 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002124 break;
Alex Wu4539b082012-03-01 12:57:46 +08002125 case SHELL_SURFACE_FULLSCREEN:
2126 shell_map_fullscreen(shsurf);
2127 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002128 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002129 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002130 panel_height = get_output_panel_height(shell,surface->output);
2131 weston_surface_set_position(surface, surface->output->x,
2132 surface->output->y + panel_height);
2133 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002134 case SHELL_SURFACE_LOCK:
2135 center_on_output(surface, get_default_output(compositor));
2136 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002137 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002138 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002139 case SHELL_SURFACE_NONE:
2140 weston_surface_set_position(surface,
2141 surface->geometry.x + sx,
2142 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002143 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002144 default:
2145 ;
2146 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002147
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002148 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002149 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002150 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002151 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002152 wl_list_insert(&shell->background_layer.surface_list,
2153 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002154 break;
2155 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002156 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002157 wl_list_insert(&shell->panel_layer.surface_list,
2158 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002159 break;
2160 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002161 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002162 wl_list_insert(&shell->lock_layer.surface_list,
2163 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002164 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002165 break;
2166 case SHELL_SURFACE_SCREENSAVER:
2167 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002168 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002169 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002170 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002171 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002172 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002173 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002174 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002175 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002176 case SHELL_SURFACE_POPUP:
2177 case SHELL_SURFACE_TRANSIENT:
2178 parent = shsurf->parent->surface;
2179 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2180 break;
Alex Wu4539b082012-03-01 12:57:46 +08002181 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002182 case SHELL_SURFACE_NONE:
2183 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002184 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002185 wl_list_insert(&shell->toplevel_layer.surface_list,
2186 &surface->layer_link);
2187 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002188 }
2189
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002190 if (surface_type != SHELL_SURFACE_NONE) {
2191 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002192 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2193 surface->output = shsurf->output;
2194 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002195
Juan Zhao7bb92f02011-12-15 11:31:51 -05002196 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002197 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002198 if (shsurf->transient.flags ==
2199 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2200 break;
2201 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002202 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002203 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002204 if (!shell->locked) {
2205 wl_list_for_each(seat, &compositor->seat_list, link)
2206 activate(shell, surface, seat);
2207 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002208 break;
2209 default:
2210 break;
2211 }
2212
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002213 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002214 {
2215 switch (shell->win_animation_type) {
2216 case ANIMATION_FADE:
2217 weston_fade_run(surface, NULL, NULL);
2218 break;
2219 case ANIMATION_ZOOM:
2220 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2221 break;
2222 default:
2223 break;
2224 }
2225 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002226}
2227
2228static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002229configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002230 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002231{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002232 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2233 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002234
Pekka Paalanen77346a62011-11-30 16:26:35 +02002235 shsurf = get_shell_surface(surface);
2236 if (shsurf)
2237 surface_type = shsurf->type;
2238
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002239 surface->geometry.x = x;
2240 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002241 surface->geometry.width = width;
2242 surface->geometry.height = height;
2243 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002244
2245 switch (surface_type) {
2246 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002247 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002248 break;
Alex Wu4539b082012-03-01 12:57:46 +08002249 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002250 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002251 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002252 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002253 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002254 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002255 surface->geometry.x = surface->output->x;
2256 surface->geometry.y = surface->output->y +
2257 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002258 break;
Alex Wu4539b082012-03-01 12:57:46 +08002259 case SHELL_SURFACE_TOPLEVEL:
2260 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002261 default:
2262 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002263 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002264
Alex Wu4539b082012-03-01 12:57:46 +08002265 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002266 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002267 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002268
2269 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2270 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002271 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2272 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002273 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002274}
2275
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002276static void
2277shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2278{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002279 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002280 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002281 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002282
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002283 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002284 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002285 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002286 type_changed = 1;
2287 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002288
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002289 if (!weston_surface_is_mapped(es)) {
2290 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002291 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002292 es->geometry.width != es->buffer->width ||
2293 es->geometry.height != es->buffer->height) {
2294 GLfloat from_x, from_y;
2295 GLfloat to_x, to_y;
2296
2297 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2298 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2299 configure(shell, es,
2300 es->geometry.x + to_x - from_x,
2301 es->geometry.y + to_y - from_y,
2302 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002303 }
2304}
2305
Tiago Vignattibe143262012-04-16 17:31:41 +03002306static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002307
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002308static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002309desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002310{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002311 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002312 struct desktop_shell *shell =
2313 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002314
2315 shell->child.process.pid = 0;
2316 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002317
2318 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2319 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002320 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002321 shell->child.deathstamp = time;
2322 shell->child.deathcount = 0;
2323 }
2324
2325 shell->child.deathcount++;
2326 if (shell->child.deathcount > 5) {
2327 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2328 return;
2329 }
2330
2331 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2332 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002333}
2334
2335static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002336launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002337{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002338 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002339
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002340 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002341 &shell->child.process,
2342 shell_exe,
2343 desktop_shell_sigchld);
2344
2345 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002346 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002347 return 0;
2348}
2349
2350static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002351bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2352{
Tiago Vignattibe143262012-04-16 17:31:41 +03002353 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002354
2355 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002356 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002357}
2358
Kristian Høgsberg75840622011-09-06 13:48:16 -04002359static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002360unbind_desktop_shell(struct wl_resource *resource)
2361{
Tiago Vignattibe143262012-04-16 17:31:41 +03002362 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002363
2364 if (shell->locked)
2365 resume_desktop(shell);
2366
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002367 shell->child.desktop_shell = NULL;
2368 shell->prepare_event_sent = false;
2369 free(resource);
2370}
2371
2372static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002373bind_desktop_shell(struct wl_client *client,
2374 void *data, uint32_t version, uint32_t id)
2375{
Tiago Vignattibe143262012-04-16 17:31:41 +03002376 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002377 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002378
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002379 resource = wl_client_add_object(client, &desktop_shell_interface,
2380 &desktop_shell_implementation,
2381 id, shell);
2382
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002383 if (client == shell->child.client) {
2384 resource->destroy = unbind_desktop_shell;
2385 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002386 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002387 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002388
2389 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2390 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002391 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392}
2393
Pekka Paalanen6e168112011-11-24 11:34:05 +02002394static void
2395screensaver_set_surface(struct wl_client *client,
2396 struct wl_resource *resource,
2397 struct wl_resource *shell_surface_resource,
2398 struct wl_resource *output_resource)
2399{
Tiago Vignattibe143262012-04-16 17:31:41 +03002400 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002401 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002402 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002403
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002404 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002405
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002406 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002407 surface->output = output;
2408 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002409}
2410
2411static const struct screensaver_interface screensaver_implementation = {
2412 screensaver_set_surface
2413};
2414
2415static void
2416unbind_screensaver(struct wl_resource *resource)
2417{
Tiago Vignattibe143262012-04-16 17:31:41 +03002418 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002419
Pekka Paalanen77346a62011-11-30 16:26:35 +02002420 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002421 free(resource);
2422}
2423
2424static void
2425bind_screensaver(struct wl_client *client,
2426 void *data, uint32_t version, uint32_t id)
2427{
Tiago Vignattibe143262012-04-16 17:31:41 +03002428 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002429 struct wl_resource *resource;
2430
2431 resource = wl_client_add_object(client, &screensaver_interface,
2432 &screensaver_implementation,
2433 id, shell);
2434
Pekka Paalanen77346a62011-11-30 16:26:35 +02002435 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002436 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002437 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002438 return;
2439 }
2440
2441 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2442 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002443 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002444}
2445
Kristian Høgsberg07045392012-02-19 18:52:44 -05002446struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002447 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002448 struct weston_surface *current;
2449 struct wl_listener listener;
2450 struct wl_keyboard_grab grab;
2451};
2452
2453static void
2454switcher_next(struct switcher *switcher)
2455{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002456 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002457 struct weston_surface *surface;
2458 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002459 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002460
2461 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002462 switch (get_shell_surface_type(surface)) {
2463 case SHELL_SURFACE_TOPLEVEL:
2464 case SHELL_SURFACE_FULLSCREEN:
2465 case SHELL_SURFACE_MAXIMIZED:
2466 if (first == NULL)
2467 first = surface;
2468 if (prev == switcher->current)
2469 next = surface;
2470 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002471 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002472 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002473 weston_surface_damage(surface);
2474 break;
2475 default:
2476 break;
2477 }
Alex Wu1659daa2012-04-01 20:13:09 +08002478
2479 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002480 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002481 surface->geometry.dirty = 1;
2482 weston_surface_damage(surface);
2483 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002484 }
2485
2486 if (next == NULL)
2487 next = first;
2488
Alex Wu07b26062012-03-12 16:06:01 +08002489 if (next == NULL)
2490 return;
2491
Kristian Høgsberg07045392012-02-19 18:52:44 -05002492 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002493 wl_signal_add(&next->surface.resource.destroy_signal,
2494 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002495
2496 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002497 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002498
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002499 shsurf = get_shell_surface(switcher->current);
2500 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002501 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002502}
2503
2504static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002505switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002506{
2507 struct switcher *switcher =
2508 container_of(listener, struct switcher, listener);
2509
2510 switcher_next(switcher);
2511}
2512
2513static void
Daniel Stone351eb612012-05-31 15:27:47 -04002514switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002515{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002516 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002517 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002518 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002519
2520 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002521 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002522 weston_surface_damage(surface);
2523 }
2524
Alex Wu07b26062012-03-12 16:06:01 +08002525 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002526 activate(switcher->shell, switcher->current,
2527 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002528 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002529 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002530 free(switcher);
2531}
2532
2533static void
2534switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002535 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002536{
2537 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002538 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002539
Daniel Stonec9785ea2012-05-30 16:31:52 +01002540 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002541 switcher_next(switcher);
2542}
2543
2544static void
2545switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2546 uint32_t mods_depressed, uint32_t mods_latched,
2547 uint32_t mods_locked, uint32_t group)
2548{
2549 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002550 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002551
Daniel Stone351eb612012-05-31 15:27:47 -04002552 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
2553 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002554}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002555
2556static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04002557 switcher_key,
2558 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05002559};
2560
2561static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002562switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2563 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002564{
Tiago Vignattibe143262012-04-16 17:31:41 +03002565 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002566 struct switcher *switcher;
2567
2568 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002569 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002570 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002571 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002572 wl_list_init(&switcher->listener.link);
2573
2574 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002575 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2576 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002577 switcher_next(switcher);
2578}
2579
Pekka Paalanen3c647232011-12-22 13:43:43 +02002580static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002581backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2582 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002583{
2584 struct weston_compositor *compositor = data;
2585 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002586 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002587
2588 /* TODO: we're limiting to simple use cases, where we assume just
2589 * control on the primary display. We'd have to extend later if we
2590 * ever get support for setting backlights on random desktop LCD
2591 * panels though */
2592 output = get_default_output(compositor);
2593 if (!output)
2594 return;
2595
2596 if (!output->set_backlight)
2597 return;
2598
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002599 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2600 backlight_new = output->backlight_current - 25;
2601 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2602 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002603
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002604 if (backlight_new < 5)
2605 backlight_new = 5;
2606 if (backlight_new > 255)
2607 backlight_new = 255;
2608
2609 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002610 output->set_backlight(output, output->backlight_current);
2611}
2612
2613static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002614debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2615 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002616{
Tiago Vignattibe143262012-04-16 17:31:41 +03002617 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002618 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002619 struct weston_surface *surface;
2620
2621 if (shell->debug_repaint_surface) {
2622 weston_surface_destroy(shell->debug_repaint_surface);
2623 shell->debug_repaint_surface = NULL;
2624 } else {
2625 surface = weston_surface_create(compositor);
2626 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2627 weston_surface_configure(surface, 0, 0, 8192, 8192);
2628 wl_list_insert(&compositor->fade_layer.surface_list,
2629 &surface->layer_link);
2630 weston_surface_assign_output(surface);
2631 pixman_region32_init(&surface->input);
2632
2633 /* Here's the dirty little trick that makes the
2634 * repaint debugging work: we force an
2635 * update_transform first to update dependent state
2636 * and clear the geometry.dirty bit. Then we clear
2637 * the surface damage so it only gets repainted
2638 * piecewise as we repaint other things. */
2639
2640 weston_surface_update_transform(surface);
2641 pixman_region32_fini(&surface->damage);
2642 pixman_region32_init(&surface->damage);
2643 shell->debug_repaint_surface = surface;
2644 }
2645}
2646
2647static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002648force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2649 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002650{
2651 struct wl_client *client;
2652 pid_t pid;
2653 uid_t uid;
2654 gid_t gid;
2655
Daniel Stone325fc2d2012-05-30 16:31:58 +01002656 client = seat->keyboard->focus->resource.client;
2657 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002658
Daniel Stone325fc2d2012-05-30 16:31:58 +01002659 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002660}
2661
2662static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002663shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002664{
Tiago Vignattibe143262012-04-16 17:31:41 +03002665 struct desktop_shell *shell =
2666 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002667
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002668 if (shell->child.client)
2669 wl_client_destroy(shell->child.client);
2670
Kristian Høgsberg88c16072012-05-16 08:04:19 -04002671 wl_list_remove(&shell->lock_listener.link);
2672 wl_list_remove(&shell->unlock_listener.link);
2673
Pekka Paalanen3c647232011-12-22 13:43:43 +02002674 free(shell->screensaver.path);
2675 free(shell);
2676}
2677
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002678static void
2679shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2680{
2681 uint32_t mod;
2682
2683 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01002684 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
2685 MODIFIER_CTRL | MODIFIER_ALT,
2686 terminate_binding, ec);
2687 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
2688 click_to_activate_binding,
2689 shell);
2690 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2691 MODIFIER_SUPER | MODIFIER_ALT,
2692 surface_opacity_binding, NULL);
2693 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2694 MODIFIER_SUPER, zoom_axis_binding,
2695 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002696
2697 /* configurable bindings */
2698 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002699 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
2700 zoom_key_binding, NULL);
2701 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
2702 zoom_key_binding, NULL);
2703 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
2704 shell);
2705 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
2706 resize_binding, shell);
2707 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
2708 rotate_binding, NULL);
2709 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
2710 shell);
2711 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
2712 ec);
2713 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
2714 backlight_binding, ec);
2715 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
2716 ec);
2717 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
2718 backlight_binding, ec);
2719 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
2720 debug_repaint_binding, shell);
2721 weston_compositor_add_key_binding(ec, KEY_K, mod,
2722 force_kill_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002723}
2724
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002725int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002726shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002727
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002728WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002729shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002730{
Tiago Vignattibe143262012-04-16 17:31:41 +03002731 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002732
2733 shell = malloc(sizeof *shell);
2734 if (shell == NULL)
2735 return -1;
2736
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002737 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002738 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002739
2740 shell->destroy_listener.notify = shell_destroy;
2741 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2742 shell->lock_listener.notify = lock;
2743 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2744 shell->unlock_listener.notify = unlock;
2745 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002746 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002747 ec->shell_interface.create_shell_surface = create_shell_surface;
2748 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002749 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002750 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002751 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002752
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002753 wl_list_init(&shell->backgrounds);
2754 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002755 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002756
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002757 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2758 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2759 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2760 weston_layer_init(&shell->background_layer,
2761 &shell->toplevel_layer.link);
2762 wl_list_init(&shell->lock_layer.surface_list);
2763
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002764 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002765
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002766 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2767 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002768 return -1;
2769
Kristian Høgsberg75840622011-09-06 13:48:16 -04002770 if (wl_display_add_global(ec->wl_display,
2771 &desktop_shell_interface,
2772 shell, bind_desktop_shell) == NULL)
2773 return -1;
2774
Pekka Paalanen6e168112011-11-24 11:34:05 +02002775 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2776 shell, bind_screensaver) == NULL)
2777 return -1;
2778
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002779 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002780 if (launch_desktop_shell_process(shell) != 0)
2781 return -1;
2782
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002783 shell->pointer_focus_listener.notify = handle_pointer_focus;
2784 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
2785 &shell->pointer_focus_listener);
2786
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002787 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002788
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002789 return 0;
2790}