blob: 73af7519210849c2052a92bf78c68aec6af25dd3 [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"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Juan Zhaoe10d2792012-04-25 19:09:52 +080041enum animation_type {
42 ANIMATION_NONE,
43
44 ANIMATION_ZOOM,
45 ANIMATION_FADE
46};
47
Tiago Vignattibe143262012-04-16 17:31:41 +030048struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050049 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040050
51 struct wl_listener lock_listener;
52 struct wl_listener unlock_listener;
53 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050055 struct weston_layer fullscreen_layer;
56 struct weston_layer panel_layer;
57 struct weston_layer toplevel_layer;
58 struct weston_layer background_layer;
59 struct weston_layer lock_layer;
60
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040061 struct wl_listener pointer_focus_listener;
62 struct weston_surface *busy_surface;
63
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020064 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050065 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020066 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020067 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020068
69 unsigned deathcount;
70 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020071 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020072
73 bool locked;
74 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020075
Pekka Paalanen068ae942011-11-28 14:11:15 +020076 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050077 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010078
79 struct wl_list backgrounds;
80 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020081
Pekka Paalanen77346a62011-11-30 16:26:35 +020082 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020083 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020084 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020085 struct wl_resource *binding;
86 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050087 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020088 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050089
Tiago Vignatti0b52d482012-04-20 18:54:25 +030090 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080091 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050092 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040093};
94
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050095enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020096 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050097
Pekka Paalanen57da4a82011-11-23 16:42:16 +020098 SHELL_SURFACE_PANEL,
99 SHELL_SURFACE_BACKGROUND,
100 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200101 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500102
103 SHELL_SURFACE_TOPLEVEL,
104 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500105 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800106 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500107 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200108};
109
Scott Moreauff1db4a2012-04-17 19:06:18 -0600110struct ping_timer {
111 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600112 uint32_t serial;
113};
114
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200115struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200116 struct wl_resource resource;
117
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500118 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500120 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300121 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200122
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400123 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400124 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500125 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800127 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600128 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100129
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500130 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200131 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500132 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 } rotation;
134
135 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700136 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200138 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100140 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
153 struct weston_surface *black_surface;
154 } fullscreen;
155
Scott Moreauff1db4a2012-04-17 19:06:18 -0600156 struct ping_timer *ping_timer;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400161
162 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200163};
164
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300165struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700166 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300167 struct shell_surface *shsurf;
168 struct wl_listener shsurf_destroy_listener;
169};
170
171struct weston_move_grab {
172 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100173 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174};
175
Pekka Paalanen460099f2012-01-20 16:48:25 +0200176struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300177 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500178 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200179 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400180 GLfloat x;
181 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200182 } center;
183};
184
Alex Wubd3354b2012-04-17 17:20:49 +0800185static struct shell_surface *
186get_shell_surface(struct weston_surface *surface);
187
188static struct desktop_shell *
189shell_surface_get_shell(struct shell_surface *shsurf);
190
191static bool
192shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
193{
194 struct desktop_shell *shell;
195 struct weston_surface *top_fs_es;
196
197 shell = shell_surface_get_shell(shsurf);
198
199 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
200 return false;
201
202 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
203 struct weston_surface,
204 layer_link);
205 return (shsurf == get_shell_surface(top_fs_es));
206}
207
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500208static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400209destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210{
211 struct shell_grab *grab;
212
213 grab = container_of(listener, struct shell_grab,
214 shsurf_destroy_listener);
215
216 grab->shsurf = NULL;
217}
218
219static void
220shell_grab_init(struct shell_grab *grab,
221 const struct wl_pointer_grab_interface *interface,
222 struct shell_surface *shsurf)
223{
224 grab->grab.interface = interface;
225 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400226 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
227 wl_signal_add(&shsurf->resource.destroy_signal,
228 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300229
230}
231
232static void
233shell_grab_finish(struct shell_grab *grab)
234{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400235 if (grab->shsurf)
236 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237}
238
239static void
Alex Wu4539b082012-03-01 12:57:46 +0800240center_on_output(struct weston_surface *surface,
241 struct weston_output *output);
242
Daniel Stone496ca172012-05-30 16:31:42 +0100243static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300244get_modifier(char *modifier)
245{
246 if (!modifier)
247 return MODIFIER_SUPER;
248
249 if (!strcmp("ctrl", modifier))
250 return MODIFIER_CTRL;
251 else if (!strcmp("alt", modifier))
252 return MODIFIER_ALT;
253 else if (!strcmp("super", modifier))
254 return MODIFIER_SUPER;
255 else
256 return MODIFIER_SUPER;
257}
258
Juan Zhaoe10d2792012-04-25 19:09:52 +0800259static enum animation_type
260get_animation_type(char *animation)
261{
262 if (!animation)
263 return ANIMATION_NONE;
264
265 if (!strcmp("zoom", animation))
266 return ANIMATION_ZOOM;
267 else if (!strcmp("fade", animation))
268 return ANIMATION_FADE;
269 else
270 return ANIMATION_NONE;
271}
272
Alex Wu4539b082012-03-01 12:57:46 +0800273static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300274shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200275{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200276 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200277 char *path = NULL;
278 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300279 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800280 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200281
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400282 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300283 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800284 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200285 };
286
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400287 struct config_key saver_keys[] = {
288 { "path", CONFIG_KEY_STRING, &path },
289 { "duration", CONFIG_KEY_INTEGER, &duration },
290 };
291
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200292 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400293 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200294 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
295 };
296
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200297 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500298 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200299 free(config_file);
300
Pekka Paalanen7296e792011-12-07 16:22:00 +0200301 shell->screensaver.path = path;
302 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300303 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800304 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200305}
306
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200307static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400308noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100309 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500310{
311 grab->focus = NULL;
312}
313
314static void
Scott Moreau447013d2012-02-18 05:05:29 -0700315move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100316 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100319 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300320 struct shell_surface *shsurf = move->base.shsurf;
321 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100322 int dx = wl_fixed_to_int(pointer->x + move->dx);
323 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324
325 if (!shsurf)
326 return;
327
328 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500329
Daniel Stone103db7f2012-05-08 17:17:55 +0100330 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200331 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500332}
333
334static void
Scott Moreau447013d2012-02-18 05:05:29 -0700335move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100336 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500337{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
339 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100340 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100341 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342
Daniel Stone4dbadb12012-05-30 16:31:51 +0100343 if (pointer->button_count == 0 &&
344 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100346 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500347 free(grab);
348 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500349}
350
Scott Moreau447013d2012-02-18 05:05:29 -0700351static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500352 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500353 move_grab_motion,
354 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500355};
356
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600357static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400358busy_cursor_grab_focus(struct wl_pointer_grab *base,
359 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600360{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400361 struct shell_grab *grab = (struct shell_grab *) base;
362 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600363
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400364 if (grab->grab.focus != surface) {
365 shell_grab_finish(grab);
366 wl_pointer_end_grab(pointer);
367 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600368 }
369}
370
371static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400372busy_cursor_grab_motion(struct wl_pointer_grab *grab,
373 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600374{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400375}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600376
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400377static void
378busy_cursor_grab_button(struct wl_pointer_grab *grab,
379 uint32_t time, uint32_t button, uint32_t state)
380{
381}
382
383static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
384 busy_cursor_grab_focus,
385 busy_cursor_grab_motion,
386 busy_cursor_grab_button,
387};
388
389static void
390set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
391{
392 struct shell_grab *grab;
393 struct desktop_shell *shell = shsurf->shell;
394 struct weston_seat *seat = (struct weston_seat *) pointer->seat;
395 struct weston_surface *sprite;
396
397 grab = malloc(sizeof *grab);
398 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600399 return;
400
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400401 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
402 grab->grab.focus = &shsurf->surface->surface;
403 wl_pointer_start_grab(pointer, &grab->grab);
404 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600405
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400406 sprite = (struct weston_surface *) seat->sprite;
407 shell->busy_surface->output = sprite->output;
408}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600409
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400410static void
411end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
412{
413 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
414
415 if (grab->grab.interface == &busy_cursor_grab_interface) {
416 shell_grab_finish(grab);
417 wl_pointer_end_grab(pointer);
418 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600419 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600420}
421
Scott Moreau9521d5e2012-04-19 13:06:17 -0600422static void
423ping_timer_destroy(struct shell_surface *shsurf)
424{
425 if (!shsurf || !shsurf->ping_timer)
426 return;
427
428 if (shsurf->ping_timer->source)
429 wl_event_source_remove(shsurf->ping_timer->source);
430
431 free(shsurf->ping_timer);
432 shsurf->ping_timer = NULL;
433}
434
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400435static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600436ping_timeout_handler(void *data)
437{
438 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400439 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600440
Scott Moreau9521d5e2012-04-19 13:06:17 -0600441 /* Client is not responding */
442 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400443
444 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
445 if (seat->seat.pointer->focus == &shsurf->surface->surface)
446 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600447
448 return 1;
449}
450
451static void
452ping_handler(struct weston_surface *surface, uint32_t serial)
453{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400454 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600455 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400456 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600457
458 if (!shsurf)
459 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400460 if (!shsurf->resource.client)
461 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600462
463 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300464 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600465 if (!shsurf->ping_timer)
466 return;
467
468 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600469 loop = wl_display_get_event_loop(surface->compositor->wl_display);
470 shsurf->ping_timer->source =
471 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
472 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
473
474 wl_shell_surface_send_ping(&shsurf->resource, serial);
475 }
476}
477
478static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400479handle_pointer_focus(struct wl_listener *listener, void *data)
480{
481 struct wl_pointer *pointer = data;
482 struct weston_surface *surface =
483 (struct weston_surface *) pointer->focus;
484 struct weston_compositor *compositor;
485 struct shell_surface *shsurf;
486 uint32_t serial;
487
488 if (!surface)
489 return;
490
491 compositor = surface->compositor;
492 shsurf = get_shell_surface(surface);
493
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300494 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400495 set_busy_cursor(shsurf, pointer);
496 } else {
497 serial = wl_display_next_serial(compositor->wl_display);
498 ping_handler(surface, serial);
499 }
500}
501
502static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600503shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
504 uint32_t serial)
505{
506 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400507 struct desktop_shell *shell = shsurf->shell;
508 struct weston_seat *seat;
509 struct weston_compositor *ec = shsurf->surface->compositor;
510 struct wl_pointer *pointer;
511 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600512
Scott Moreauff1db4a2012-04-17 19:06:18 -0600513 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400514 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600515 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400516 if (was_unresponsive) {
517 /* Received pong from previously unresponsive client */
518 wl_list_for_each(seat, &ec->seat_list, link) {
519 pointer = seat->seat.pointer;
520 if (pointer->focus ==
521 &shell->busy_surface->surface &&
522 pointer->current ==
523 &shsurf->surface->surface)
524 end_busy_cursor(shsurf, pointer);
525 }
526 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600527 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600528 }
529}
530
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400531static void
532shell_surface_set_title(struct wl_client *client,
533 struct wl_resource *resource, const char *title)
534{
535 struct shell_surface *shsurf = resource->data;
536
537 free(shsurf->title);
538 shsurf->title = strdup(title);
539}
540
541static void
542shell_surface_set_class(struct wl_client *client,
543 struct wl_resource *resource, const char *class)
544{
545 struct shell_surface *shsurf = resource->data;
546
547 free(shsurf->class);
548 shsurf->class = strdup(class);
549}
550
Scott Moreauff1db4a2012-04-17 19:06:18 -0600551static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400552surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500553{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500554 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300555
556 if (!shsurf)
557 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500558
559 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400560 if (!move)
561 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500562
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300563 shell_grab_init(&move->base, &move_grab_interface, shsurf);
564
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400565 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100566 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400567 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100568 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500569
Daniel Stone37816df2012-05-16 18:45:18 +0100570 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500571
Daniel Stone37816df2012-05-16 18:45:18 +0100572 wl_pointer_set_focus(ws->seat.pointer, NULL,
573 wl_fixed_from_int(0),
574 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400575
576 return 0;
577}
578
579static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200580shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100581 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400582{
Daniel Stone37816df2012-05-16 18:45:18 +0100583 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200584 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400585
Daniel Stone37816df2012-05-16 18:45:18 +0100586 if (ws->seat.pointer->button_count == 0 ||
587 ws->seat.pointer->grab_serial != serial ||
588 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500589 return;
590
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400591 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400592 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500593}
594
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500595struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300596 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500597 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200598 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500599};
600
601static void
Scott Moreau447013d2012-02-18 05:05:29 -0700602resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100603 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500604{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500605 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100606 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400607 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500608 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100609 wl_fixed_t from_x, from_y;
610 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200611
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400612 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300613 return;
614
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400615 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100616 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100617 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400618 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100619 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500620
Daniel Stone103db7f2012-05-08 17:17:55 +0100621 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200622 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100623 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200624 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100625 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500626 }
627
Daniel Stone103db7f2012-05-08 17:17:55 +0100628 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200629 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100630 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200631 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100632 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500633 }
634
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400635 shsurf->client->send_configure(shsurf->surface,
636 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500637}
638
639static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400640send_configure(struct weston_surface *surface,
641 uint32_t edges, int32_t width, int32_t height)
642{
643 struct shell_surface *shsurf = get_shell_surface(surface);
644
645 wl_shell_surface_send_configure(&shsurf->resource,
646 edges, width, height);
647}
648
649static const struct weston_shell_client shell_client = {
650 send_configure
651};
652
653static void
Scott Moreau447013d2012-02-18 05:05:29 -0700654resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100655 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500656{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300657 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100658 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100659 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500660
Daniel Stone4dbadb12012-05-30 16:31:51 +0100661 if (pointer->button_count == 0 &&
662 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300663 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100664 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500665 free(grab);
666 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500667}
668
Scott Moreau447013d2012-02-18 05:05:29 -0700669static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500670 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500671 resize_grab_motion,
672 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500673};
674
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400675static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400676surface_resize(struct shell_surface *shsurf,
677 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500678{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500679 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500680
Alex Wu4539b082012-03-01 12:57:46 +0800681 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
682 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500683
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200684 if (edges == 0 || edges > 15 ||
685 (edges & 3) == 3 || (edges & 12) == 12)
686 return 0;
687
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500688 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400689 if (!resize)
690 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500691
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300692 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
693
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500694 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200695 resize->width = shsurf->surface->geometry.width;
696 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400697
Daniel Stone37816df2012-05-16 18:45:18 +0100698 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500699
Daniel Stone37816df2012-05-16 18:45:18 +0100700 wl_pointer_set_focus(ws->seat.pointer, NULL,
701 wl_fixed_from_int(0),
702 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400703
704 return 0;
705}
706
707static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200708shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100709 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200710 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400711{
Daniel Stone37816df2012-05-16 18:45:18 +0100712 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200713 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400714
Alex Wu4539b082012-03-01 12:57:46 +0800715 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
716 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400717
Daniel Stone37816df2012-05-16 18:45:18 +0100718 if (ws->seat.pointer->button_count == 0 ||
719 ws->seat.pointer->grab_serial != serial ||
720 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500721 return;
722
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400723 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400724 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500725}
726
Juan Zhao96879df2012-02-07 08:45:41 +0800727static struct weston_output *
728get_default_output(struct weston_compositor *compositor)
729{
730 return container_of(compositor->output_list.next,
731 struct weston_output, link);
732}
733
Alex Wu4539b082012-03-01 12:57:46 +0800734static void
735shell_unset_fullscreen(struct shell_surface *shsurf)
736{
737 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800738 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
739 shell_surface_is_top_fullscreen(shsurf)) {
740 weston_output_switch_mode(shsurf->fullscreen_output,
741 shsurf->fullscreen_output->origin);
742 }
Alex Wu4539b082012-03-01 12:57:46 +0800743 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
744 shsurf->fullscreen.framerate = 0;
745 wl_list_remove(&shsurf->fullscreen.transform.link);
746 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800747 if (shsurf->fullscreen.black_surface)
748 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800749 shsurf->fullscreen.black_surface = NULL;
750 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800751 weston_surface_set_position(shsurf->surface,
752 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800753 if (shsurf->saved_rotation_valid) {
754 wl_list_insert(&shsurf->surface->geometry.transformation_list,
755 &shsurf->rotation.transform.link);
756 shsurf->saved_rotation_valid = false;
757 }
Alex Wu4539b082012-03-01 12:57:46 +0800758}
759
Pekka Paalanen98262232011-12-01 10:42:22 +0200760static int
761reset_shell_surface_type(struct shell_surface *surface)
762{
763 switch (surface->type) {
764 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800765 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200766 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800767 case SHELL_SURFACE_MAXIMIZED:
768 surface->output = get_default_output(surface->surface->compositor);
769 weston_surface_set_position(surface->surface,
770 surface->saved_x,
771 surface->saved_y);
772 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200773 case SHELL_SURFACE_PANEL:
774 case SHELL_SURFACE_BACKGROUND:
775 wl_list_remove(&surface->link);
776 wl_list_init(&surface->link);
777 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200778 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200779 case SHELL_SURFACE_LOCK:
780 wl_resource_post_error(&surface->resource,
781 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200782 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200783 return -1;
784 case SHELL_SURFACE_NONE:
785 case SHELL_SURFACE_TOPLEVEL:
786 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500787 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200788 break;
789 }
790
791 surface->type = SHELL_SURFACE_NONE;
792 return 0;
793}
794
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500795static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400796set_surface_type(struct shell_surface *shsurf)
797{
798 struct weston_surface *surface = shsurf->surface;
799 struct shell_surface *pshsurf = shsurf->parent;
800 struct weston_surface *pes;
801 struct shell_surface *priv;
802 struct desktop_shell *shell = shsurf->shell;
803
804 reset_shell_surface_type(shsurf);
805
806 shsurf->type = shsurf->next_type;
807 shsurf->next_type = SHELL_SURFACE_NONE;
808
809 switch (shsurf->type) {
810 case SHELL_SURFACE_TOPLEVEL:
811 break;
812 case SHELL_SURFACE_TRANSIENT:
813 pes = pshsurf->surface;
814 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300815 pes->geometry.x + shsurf->transient.x,
816 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400817 break;
818
819 case SHELL_SURFACE_MAXIMIZED:
820 shsurf->saved_x = surface->geometry.x;
821 shsurf->saved_y = surface->geometry.y;
822 shsurf->saved_position_valid = true;
823 break;
824
825 case SHELL_SURFACE_FULLSCREEN:
826 shsurf->saved_x = surface->geometry.x;
827 shsurf->saved_y = surface->geometry.y;
828 shsurf->saved_position_valid = true;
829
830 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
831 wl_list_remove(&shsurf->rotation.transform.link);
832 wl_list_init(&shsurf->rotation.transform.link);
833 shsurf->surface->geometry.dirty = 1;
834 shsurf->saved_rotation_valid = true;
835 }
836 break;
837
838 case SHELL_SURFACE_BACKGROUND:
839 wl_list_for_each(priv, &shell->backgrounds, link) {
840 if (priv->output == shsurf->output) {
841 priv->surface->output = NULL;
842 wl_list_remove(&priv->surface->layer_link);
843 wl_list_remove(&priv->link);
844 break;
845 }
846 }
847
848 wl_list_insert(&shell->backgrounds, &shsurf->link);
849
850 weston_surface_set_position(surface, shsurf->output->x,
851 shsurf->output->y);
852 break;
853
854 case SHELL_SURFACE_PANEL:
855 wl_list_for_each(priv, &shell->panels, link) {
856 if (priv->output == shsurf->output) {
857 priv->surface->output = NULL;
858 wl_list_remove(&priv->surface->layer_link);
859 wl_list_remove(&priv->link);
860 break;
861 }
862 }
863
864 wl_list_insert(&shell->panels, &shsurf->link);
865
866 weston_surface_set_position(surface, shsurf->output->x,
867 shsurf->output->y);
868 break;
869
870 default:
871 break;
872 }
873}
874
875static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300876set_toplevel(struct shell_surface *shsurf)
877{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400878 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300879}
880
881static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200882shell_surface_set_toplevel(struct wl_client *client,
883 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400884{
Pekka Paalanen98262232011-12-01 10:42:22 +0200885 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400886
Tiago Vignattibc052c92012-04-19 16:18:18 +0300887 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400888}
889
890static void
Tiago Vignatti491bac12012-05-18 16:37:43 -0400891set_transient(struct shell_surface *shsurf,
892 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
893{
894 /* assign to parents output */
895 shsurf->parent = pshsurf;
896 shsurf->transient.x = x;
897 shsurf->transient.y = y;
898 shsurf->transient.flags = flags;
899 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
900}
901
902static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200903shell_surface_set_transient(struct wl_client *client,
904 struct wl_resource *resource,
905 struct wl_resource *parent_resource,
906 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400907{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200908 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400909 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200910
Tiago Vignatti491bac12012-05-18 16:37:43 -0400911 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400912}
913
Tiago Vignattibe143262012-04-16 17:31:41 +0300914static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800915shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200916{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400917 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800918}
919
920static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300921get_output_panel_height(struct desktop_shell *shell,
922 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800923{
924 struct shell_surface *priv;
925 int panel_height = 0;
926
927 if (!output)
928 return 0;
929
Tiago Vignattibe143262012-04-16 17:31:41 +0300930 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800931 if (priv->output == output) {
932 panel_height = priv->surface->geometry.height;
933 break;
934 }
935 }
936 return panel_height;
937}
938
939static void
940shell_surface_set_maximized(struct wl_client *client,
941 struct wl_resource *resource,
942 struct wl_resource *output_resource )
943{
944 struct shell_surface *shsurf = resource->data;
945 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300946 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800947 uint32_t edges = 0, panel_height = 0;
948
949 /* get the default output, if the client set it as NULL
950 check whether the ouput is available */
951 if (output_resource)
952 shsurf->output = output_resource->data;
953 else
954 shsurf->output = get_default_output(es->compositor);
955
Tiago Vignattibe143262012-04-16 17:31:41 +0300956 shell = shell_surface_get_shell(shsurf);
957 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800958 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500959
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400960 shsurf->client->send_configure(shsurf->surface, edges,
961 es->output->current->width,
962 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800963
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400964 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200965}
966
Alex Wu21858432012-04-01 20:13:08 +0800967static void
968black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
969
Alex Wu4539b082012-03-01 12:57:46 +0800970static struct weston_surface *
971create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800972 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800973 GLfloat x, GLfloat y, int w, int h)
974{
975 struct weston_surface *surface = NULL;
976
977 surface = weston_surface_create(ec);
978 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200979 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +0800980 return NULL;
981 }
982
Alex Wu21858432012-04-01 20:13:08 +0800983 surface->configure = black_surface_configure;
984 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800985 weston_surface_configure(surface, x, y, w, h);
986 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
987 return surface;
988}
989
990/* Create black surface and append it to the associated fullscreen surface.
991 * Handle size dismatch and positioning according to the method. */
992static void
993shell_configure_fullscreen(struct shell_surface *shsurf)
994{
995 struct weston_output *output = shsurf->fullscreen_output;
996 struct weston_surface *surface = shsurf->surface;
997 struct weston_matrix *matrix;
998 float scale;
999
1000 center_on_output(surface, output);
1001
1002 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001003 shsurf->fullscreen.black_surface =
1004 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001005 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001006 output->x, output->y,
1007 output->current->width,
1008 output->current->height);
1009
1010 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1011 wl_list_insert(&surface->layer_link,
1012 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001013 shsurf->fullscreen.black_surface->output = output;
1014
1015 switch (shsurf->fullscreen.type) {
1016 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1017 break;
1018 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1019 matrix = &shsurf->fullscreen.transform.matrix;
1020 weston_matrix_init(matrix);
1021 scale = (float)output->current->width/(float)surface->geometry.width;
1022 weston_matrix_scale(matrix, scale, scale, 1);
1023 wl_list_remove(&shsurf->fullscreen.transform.link);
1024 wl_list_insert(surface->geometry.transformation_list.prev,
1025 &shsurf->fullscreen.transform.link);
1026 weston_surface_set_position(surface, output->x, output->y);
1027 break;
1028 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001029 if (shell_surface_is_top_fullscreen(shsurf)) {
1030 struct weston_mode mode = {0,
1031 surface->geometry.width,
1032 surface->geometry.height,
1033 shsurf->fullscreen.framerate};
1034
1035 if (weston_output_switch_mode(output, &mode) == 0) {
1036 weston_surface_configure(shsurf->fullscreen.black_surface,
1037 output->x, output->y,
1038 output->current->width,
1039 output->current->height);
1040 weston_surface_set_position(surface, output->x, output->y);
1041 break;
1042 }
1043 }
Alex Wu4539b082012-03-01 12:57:46 +08001044 break;
1045 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1046 break;
1047 default:
1048 break;
1049 }
1050}
1051
1052/* make the fullscreen and black surface at the top */
1053static void
1054shell_stack_fullscreen(struct shell_surface *shsurf)
1055{
Alex Wubd3354b2012-04-17 17:20:49 +08001056 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001057 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001058 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001059
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001060 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001061 wl_list_insert(&shell->fullscreen_layer.surface_list,
1062 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001063 weston_surface_damage(surface);
1064
1065 if (!shsurf->fullscreen.black_surface)
1066 shsurf->fullscreen.black_surface =
1067 create_black_surface(surface->compositor,
1068 surface,
1069 output->x, output->y,
1070 output->current->width,
1071 output->current->height);
1072
1073 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001074 wl_list_insert(&surface->layer_link,
1075 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001076 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001077}
1078
1079static void
1080shell_map_fullscreen(struct shell_surface *shsurf)
1081{
Alex Wu4539b082012-03-01 12:57:46 +08001082 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001083 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001084}
1085
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001086static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001088 struct wl_resource *resource,
1089 uint32_t method,
1090 uint32_t framerate,
1091 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001092{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001093 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001094 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001095
1096 if (output_resource)
1097 shsurf->output = output_resource->data;
1098 else
1099 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001100
Alex Wu4539b082012-03-01 12:57:46 +08001101 shsurf->fullscreen_output = shsurf->output;
1102 shsurf->fullscreen.type = method;
1103 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001104 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001105
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001106 shsurf->client->send_configure(shsurf->surface, 0,
1107 shsurf->output->current->width,
1108 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001109}
1110
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001111static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001112popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001113 struct wl_surface *surface,
1114 wl_fixed_t x,
1115 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001116{
Daniel Stone37816df2012-05-16 18:45:18 +01001117 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001118 struct shell_surface *priv =
1119 container_of(grab, struct shell_surface, popup.grab);
1120 struct wl_client *client = priv->surface->surface.resource.client;
1121
Pekka Paalanencb108432012-01-19 16:25:40 +02001122 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001123 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001124 grab->focus = surface;
1125 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001126 wl_pointer_set_focus(pointer, NULL,
1127 wl_fixed_from_int(0),
1128 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001129 grab->focus = NULL;
1130 }
1131}
1132
1133static void
Scott Moreau447013d2012-02-18 05:05:29 -07001134popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001135 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001136{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001137 struct wl_resource *resource;
1138
Daniel Stone37816df2012-05-16 18:45:18 +01001139 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001140 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001141 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001142}
1143
1144static void
Scott Moreau447013d2012-02-18 05:05:29 -07001145popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001146 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001147{
1148 struct wl_resource *resource;
1149 struct shell_surface *shsurf =
1150 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001151 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001152 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001153 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001154
Daniel Stone37816df2012-05-16 18:45:18 +01001155 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001156 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001157 display = wl_client_get_display(resource->client);
1158 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001159 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001160 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001161 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001162 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001163 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001164 wl_pointer_end_grab(grab->pointer);
1165 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001166 }
1167
Daniel Stone4dbadb12012-05-30 16:31:51 +01001168 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001169 shsurf->popup.initial_up = 1;
1170}
1171
Scott Moreau447013d2012-02-18 05:05:29 -07001172static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001173 popup_grab_focus,
1174 popup_grab_motion,
1175 popup_grab_button,
1176};
1177
1178static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001179shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001180{
Daniel Stone37816df2012-05-16 18:45:18 +01001181 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001182 struct weston_surface *es = shsurf->surface;
1183 struct weston_surface *parent = shsurf->parent->surface;
1184
1185 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001186 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001187
Pekka Paalanen938269a2012-02-07 14:19:01 +02001188 weston_surface_update_transform(parent);
1189 if (parent->transform.enabled) {
1190 shsurf->popup.parent_transform.matrix =
1191 parent->transform.matrix;
1192 } else {
1193 /* construct x, y translation matrix */
1194 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1195 shsurf->popup.parent_transform.matrix.d[12] =
1196 parent->geometry.x;
1197 shsurf->popup.parent_transform.matrix.d[13] =
1198 parent->geometry.y;
1199 }
1200 wl_list_insert(es->geometry.transformation_list.prev,
1201 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001202 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001203
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001204 shsurf->popup.initial_up = 0;
1205
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001206 /* We don't require the grab to still be active, but if another
1207 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001208 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1209 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001210 } else {
1211 wl_shell_surface_send_popup_done(&shsurf->resource);
1212 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001213}
1214
1215static void
1216shell_surface_set_popup(struct wl_client *client,
1217 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001218 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001219 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001220 struct wl_resource *parent_resource,
1221 int32_t x, int32_t y, uint32_t flags)
1222{
1223 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001224
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001225 shsurf->type = SHELL_SURFACE_POPUP;
1226 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001227 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001228 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001229 shsurf->popup.x = x;
1230 shsurf->popup.y = y;
1231}
1232
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001233static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001234 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001235 shell_surface_move,
1236 shell_surface_resize,
1237 shell_surface_set_toplevel,
1238 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001239 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001240 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001241 shell_surface_set_maximized,
1242 shell_surface_set_title,
1243 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001244};
1245
1246static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001247destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001248{
Daniel Stone37816df2012-05-16 18:45:18 +01001249 if (shsurf->popup.grab.pointer)
1250 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001251
Alex Wubd3354b2012-04-17 17:20:49 +08001252 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1253 shell_surface_is_top_fullscreen(shsurf)) {
1254 weston_output_switch_mode(shsurf->fullscreen_output,
1255 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001256 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001257
Alex Wuaa08e2d2012-03-05 11:01:40 +08001258 if (shsurf->fullscreen.black_surface)
1259 weston_surface_destroy(shsurf->fullscreen.black_surface);
1260
Alex Wubd3354b2012-04-17 17:20:49 +08001261 /* As destroy_resource() use wl_list_for_each_safe(),
1262 * we can always remove the listener.
1263 */
1264 wl_list_remove(&shsurf->surface_destroy_listener.link);
1265 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001266 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001267
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001268 wl_list_remove(&shsurf->link);
1269 free(shsurf);
1270}
1271
1272static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001273shell_destroy_shell_surface(struct wl_resource *resource)
1274{
1275 struct shell_surface *shsurf = resource->data;
1276
1277 destroy_shell_surface(shsurf);
1278}
1279
1280static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001281shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001282{
1283 struct shell_surface *shsurf = container_of(listener,
1284 struct shell_surface,
1285 surface_destroy_listener);
1286
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001287 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001288 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001289 } else {
1290 wl_signal_emit(&shsurf->resource.destroy_signal,
1291 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001292 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001293 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001294}
1295
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001296static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001297get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001298{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001299 struct wl_listener *listener;
1300
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001301 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1302 shell_handle_surface_destroy);
1303 if (listener)
1304 return container_of(listener, struct shell_surface,
1305 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001306
1307 return NULL;
1308}
1309
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001310static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001311shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1312
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001313static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001314create_shell_surface(void *shell, struct weston_surface *surface,
1315 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001316{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001317 struct shell_surface *shsurf;
1318
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001319 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001320 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001321 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001322 }
1323
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001324 shsurf = calloc(1, sizeof *shsurf);
1325 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001326 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001327 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001328 }
1329
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001330 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001331 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001332
Tiago Vignattibc052c92012-04-19 16:18:18 +03001333 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001334 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001335 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001336 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001337 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001338 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1339 shsurf->fullscreen.framerate = 0;
1340 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001341 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001342 wl_list_init(&shsurf->fullscreen.transform.link);
1343
Tiago Vignattibc052c92012-04-19 16:18:18 +03001344 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001345 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1346 wl_signal_add(&surface->surface.resource.destroy_signal,
1347 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001348
1349 /* init link so its safe to always remove it in destroy_shell_surface */
1350 wl_list_init(&shsurf->link);
1351
Pekka Paalanen460099f2012-01-20 16:48:25 +02001352 /* empty when not in use */
1353 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001354 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001355
Pekka Paalanen98262232011-12-01 10:42:22 +02001356 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001357 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001358
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001359 shsurf->client = client;
1360
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001361 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001362}
1363
1364static void
1365shell_get_shell_surface(struct wl_client *client,
1366 struct wl_resource *resource,
1367 uint32_t id,
1368 struct wl_resource *surface_resource)
1369{
1370 struct weston_surface *surface = surface_resource->data;
1371 struct desktop_shell *shell = resource->data;
1372 struct shell_surface *shsurf;
1373
1374 if (get_shell_surface(surface)) {
1375 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001376 WL_DISPLAY_ERROR_INVALID_OBJECT,
1377 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001378 return;
1379 }
1380
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001381 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001382 if (!shsurf) {
1383 wl_resource_post_error(surface_resource,
1384 WL_DISPLAY_ERROR_INVALID_OBJECT,
1385 "surface->configure already set");
1386 return;
1387 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001388
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001389 shsurf->resource.destroy = shell_destroy_shell_surface;
1390 shsurf->resource.object.id = id;
1391 shsurf->resource.object.interface = &wl_shell_surface_interface;
1392 shsurf->resource.object.implementation =
1393 (void (**)(void)) &shell_surface_implementation;
1394 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001395
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001396 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001397}
1398
1399static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001400 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001401};
1402
Kristian Høgsberg07937562011-04-12 17:25:42 -04001403static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001405{
1406 proc->pid = 0;
1407}
1408
1409static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001410launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001411{
1412 if (shell->screensaver.binding)
1413 return;
1414
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001415 if (!shell->screensaver.path)
1416 return;
1417
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001418 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001419 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001420 return;
1421 }
1422
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001423 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001424 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001425 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001426 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001427}
1428
1429static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001430terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001431{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001432 if (shell->screensaver.process.pid == 0)
1433 return;
1434
1435 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001436}
1437
1438static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001439show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001440{
1441 struct wl_list *list;
1442
1443 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001444 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001445 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001446 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001447
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001448 wl_list_remove(&surface->surface->layer_link);
1449 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001450 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001451 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001452}
1453
1454static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001455hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001456{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001457 wl_list_remove(&surface->surface->layer_link);
1458 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001459 surface->surface->output = NULL;
1460}
1461
1462static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001463desktop_shell_set_background(struct wl_client *client,
1464 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001465 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001466 struct wl_resource *surface_resource)
1467{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001468 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001469
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001470 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001471 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001472
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001473 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001474 surface_resource,
1475 shsurf->output->current->width,
1476 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001477}
1478
1479static void
1480desktop_shell_set_panel(struct wl_client *client,
1481 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001482 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001483 struct wl_resource *surface_resource)
1484{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001485 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001486
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001487 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001488 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001489
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001490 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001491 surface_resource,
1492 shsurf->output->current->width,
1493 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001494}
1495
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001496static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001497handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001498{
Tiago Vignattibe143262012-04-16 17:31:41 +03001499 struct desktop_shell *shell =
1500 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001501
Martin Minarik6d118362012-06-07 18:01:59 +02001502 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001503 shell->lock_surface = NULL;
1504}
1505
1506static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001507desktop_shell_set_lock_surface(struct wl_client *client,
1508 struct wl_resource *resource,
1509 struct wl_resource *surface_resource)
1510{
Tiago Vignattibe143262012-04-16 17:31:41 +03001511 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001512 struct shell_surface *surface = surface_resource->data;
1513
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001514 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001515
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001516 if (!shell->locked)
1517 return;
1518
Pekka Paalanen98262232011-12-01 10:42:22 +02001519 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001520
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001521 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1522 wl_signal_add(&surface_resource->destroy_signal,
1523 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001524
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001525 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001526}
1527
1528static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001529resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001530{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001531 struct shell_surface *tmp;
1532
1533 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1534 hide_screensaver(shell, tmp);
1535
1536 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001537
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001538 wl_list_remove(&shell->lock_layer.link);
1539 wl_list_insert(&shell->compositor->cursor_layer.link,
1540 &shell->fullscreen_layer.link);
1541 wl_list_insert(&shell->fullscreen_layer.link,
1542 &shell->panel_layer.link);
1543 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001544
1545 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001546 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001547 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001548 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001549}
1550
1551static void
1552desktop_shell_unlock(struct wl_client *client,
1553 struct wl_resource *resource)
1554{
Tiago Vignattibe143262012-04-16 17:31:41 +03001555 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001556
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001557 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001558
1559 if (shell->locked)
1560 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001561}
1562
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001563static void
1564desktop_shell_set_busy_surface(struct wl_client *client,
1565 struct wl_resource *resource,
1566 struct wl_resource *surface_resource)
1567{
1568 struct desktop_shell *shell = resource->data;
1569
1570 shell->busy_surface = surface_resource->data;
1571}
1572
Kristian Høgsberg75840622011-09-06 13:48:16 -04001573static const struct desktop_shell_interface desktop_shell_implementation = {
1574 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001575 desktop_shell_set_panel,
1576 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001577 desktop_shell_unlock,
1578 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04001579};
1580
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001581static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001582get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001583{
1584 struct shell_surface *shsurf;
1585
1586 shsurf = get_shell_surface(surface);
1587 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001588 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001589 return shsurf->type;
1590}
1591
Kristian Høgsberg75840622011-09-06 13:48:16 -04001592static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001593move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001594{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001595 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001596 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001597 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001598
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001599 if (surface == NULL)
1600 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001601
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001602 shsurf = get_shell_surface(surface);
1603 if (shsurf == NULL)
1604 return;
1605
1606 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001607 case SHELL_SURFACE_PANEL:
1608 case SHELL_SURFACE_BACKGROUND:
1609 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001610 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001611 return;
1612 default:
1613 break;
1614 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001615
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001616 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001617}
1618
1619static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001620resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001621{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001622 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001623 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001624 uint32_t edges = 0;
1625 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001626 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001627
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001628 if (surface == NULL)
1629 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001630
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001631 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001632 if (!shsurf)
1633 return;
1634
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001635 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001636 case SHELL_SURFACE_PANEL:
1637 case SHELL_SURFACE_BACKGROUND:
1638 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001639 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001640 return;
1641 default:
1642 break;
1643 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001644
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001645 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001646 wl_fixed_to_int(seat->pointer->grab_x),
1647 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001648 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001649
Pekka Paalanen60921e52012-01-25 15:55:43 +02001650 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001651 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001652 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001653 edges |= 0;
1654 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001656
Pekka Paalanen60921e52012-01-25 15:55:43 +02001657 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001658 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001659 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001660 edges |= 0;
1661 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001662 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001663
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001664 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001665}
1666
1667static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001668surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001669 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001670{
Scott Moreau02709af2012-05-22 01:54:10 -06001671 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001672 struct shell_surface *shsurf;
1673 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001674 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001675
1676 if (surface == NULL)
1677 return;
1678
1679 shsurf = get_shell_surface(surface);
1680 if (!shsurf)
1681 return;
1682
1683 switch (shsurf->type) {
1684 case SHELL_SURFACE_BACKGROUND:
1685 case SHELL_SURFACE_SCREENSAVER:
1686 return;
1687 default:
1688 break;
1689 }
1690
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001691 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001692
Scott Moreau02709af2012-05-22 01:54:10 -06001693 if (surface->alpha > 1.0)
1694 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001695 if (surface->alpha < step)
1696 surface->alpha = step;
1697
1698 surface->geometry.dirty = 1;
1699 weston_surface_damage(surface);
1700}
1701
1702static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001703do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001704 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001705{
Daniel Stone37816df2012-05-16 18:45:18 +01001706 struct weston_seat *ws = (struct weston_seat *) seat;
1707 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001708 struct weston_output *output;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001709 float maximum_level, increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001710
1711 wl_list_for_each(output, &compositor->output_list, link) {
1712 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01001713 wl_fixed_to_double(seat->pointer->x),
1714 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001715 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01001716 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001717 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01001718 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001719 increment = -output->zoom.increment;
1720 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001721 increment = output->zoom.increment *
1722 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001723 else
1724 increment = 0;
1725
Scott Moreau1b45a792012-03-22 10:58:23 -06001726 output->zoom.active = 1;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001727 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001728
Scott Moreau850ca422012-05-21 15:21:25 -06001729 if (output->zoom.level <= 0.0) {
Scott Moreauc6d7f602012-02-23 22:28:37 -07001730 output->zoom.active = 0;
Scott Moreau850ca422012-05-21 15:21:25 -06001731 output->zoom.level = 0.0;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001732 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001733
Scott Moreau850ca422012-05-21 15:21:25 -06001734 maximum_level = 1 - output->zoom.increment;
1735
1736 if (output->zoom.level > maximum_level)
1737 output->zoom.level = maximum_level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001738
Daniel Stone37816df2012-05-16 18:45:18 +01001739 weston_output_update_zoom(output,
1740 seat->pointer->x,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001741 seat->pointer->y,
1742 ZOOM_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001743 }
1744 }
1745}
1746
Scott Moreauccbf29d2012-02-22 14:21:41 -07001747static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001748zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001749 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001750{
1751 do_zoom(seat, time, 0, axis, value);
1752}
1753
1754static void
1755zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1756 void *data)
1757{
1758 do_zoom(seat, time, key, 0, 0);
1759}
1760
1761static void
1762terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1763 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001764{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001765 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001766
Daniel Stone325fc2d2012-05-30 16:31:58 +01001767 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001768}
1769
1770static void
Scott Moreau447013d2012-02-18 05:05:29 -07001771rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001772 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001773{
1774 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001775 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001776 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001777 struct shell_surface *shsurf = rotate->base.shsurf;
1778 struct weston_surface *surface;
1779 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1780
1781 if (!shsurf)
1782 return;
1783
1784 surface = shsurf->surface;
1785
1786 cx = 0.5f * surface->geometry.width;
1787 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001788
Daniel Stone37816df2012-05-16 18:45:18 +01001789 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1790 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001791 r = sqrtf(dx * dx + dy * dy);
1792
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001793 wl_list_remove(&shsurf->rotation.transform.link);
1794 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001795
1796 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001797 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001798 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001799
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001800 weston_matrix_init(&rotate->rotation);
1801 rotate->rotation.d[0] = dx / r;
1802 rotate->rotation.d[4] = -dy / r;
1803 rotate->rotation.d[1] = -rotate->rotation.d[4];
1804 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001805
1806 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001807 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001808 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001809 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001810 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001811
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001812 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001813 &shsurf->surface->geometry.transformation_list,
1814 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001815 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001816 wl_list_init(&shsurf->rotation.transform.link);
1817 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001818 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001819 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001820
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001821 /* We need to adjust the position of the surface
1822 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001823 cposx = surface->geometry.x + cx;
1824 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001825 dposx = rotate->center.x - cposx;
1826 dposy = rotate->center.y - cposy;
1827 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001828 weston_surface_set_position(surface,
1829 surface->geometry.x + dposx,
1830 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001831 }
1832
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001833 /* Repaint implies weston_surface_update_transform(), which
1834 * lazily applies the damage due to rotation update.
1835 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001836 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001837}
1838
1839static void
Scott Moreau447013d2012-02-18 05:05:29 -07001840rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001841 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001842{
1843 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001844 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001845 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001846 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001847 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001848
Daniel Stone4dbadb12012-05-30 16:31:51 +01001849 if (pointer->button_count == 0 &&
1850 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001851 if (shsurf)
1852 weston_matrix_multiply(&shsurf->rotation.rotation,
1853 &rotate->rotation);
1854 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001855 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001856 free(rotate);
1857 }
1858}
1859
Scott Moreau447013d2012-02-18 05:05:29 -07001860static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001861 noop_grab_focus,
1862 rotate_grab_motion,
1863 rotate_grab_button,
1864};
1865
1866static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001867rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01001868 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001869{
1870 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001871 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001872 struct shell_surface *surface;
1873 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001874 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001875 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001876
1877 if (base_surface == NULL)
1878 return;
1879
1880 surface = get_shell_surface(base_surface);
1881 if (!surface)
1882 return;
1883
1884 switch (surface->type) {
1885 case SHELL_SURFACE_PANEL:
1886 case SHELL_SURFACE_BACKGROUND:
1887 case SHELL_SURFACE_FULLSCREEN:
1888 case SHELL_SURFACE_SCREENSAVER:
1889 return;
1890 default:
1891 break;
1892 }
1893
Pekka Paalanen460099f2012-01-20 16:48:25 +02001894 rotate = malloc(sizeof *rotate);
1895 if (!rotate)
1896 return;
1897
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001898 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001899
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04001900 weston_surface_to_global_float(surface->surface,
1901 surface->surface->geometry.width / 2,
1902 surface->surface->geometry.height / 2,
1903 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001904
Daniel Stone37816df2012-05-16 18:45:18 +01001905 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001906
Daniel Stone37816df2012-05-16 18:45:18 +01001907 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1908 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001909 r = sqrtf(dx * dx + dy * dy);
1910 if (r > 20.0f) {
1911 struct weston_matrix inverse;
1912
1913 weston_matrix_init(&inverse);
1914 inverse.d[0] = dx / r;
1915 inverse.d[4] = dy / r;
1916 inverse.d[1] = -inverse.d[4];
1917 inverse.d[5] = inverse.d[0];
1918 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001919
1920 weston_matrix_init(&rotate->rotation);
1921 rotate->rotation.d[0] = dx / r;
1922 rotate->rotation.d[4] = -dy / r;
1923 rotate->rotation.d[1] = -rotate->rotation.d[4];
1924 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001925 } else {
1926 weston_matrix_init(&surface->rotation.rotation);
1927 weston_matrix_init(&rotate->rotation);
1928 }
1929
Daniel Stone37816df2012-05-16 18:45:18 +01001930 wl_pointer_set_focus(seat->pointer, NULL,
1931 wl_fixed_from_int(0),
1932 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02001933}
1934
1935static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001936activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01001937 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001938{
Alex Wu21858432012-04-01 20:13:08 +08001939 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001940
Daniel Stone37816df2012-05-16 18:45:18 +01001941 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001942
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001943 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001944 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001945 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001946 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001947 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001948
Pekka Paalanen77346a62011-11-30 16:26:35 +02001949 case SHELL_SURFACE_SCREENSAVER:
1950 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001951 if (shell->lock_surface)
1952 weston_surface_restack(es,
1953 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001954 break;
Alex Wu4539b082012-03-01 12:57:46 +08001955 case SHELL_SURFACE_FULLSCREEN:
1956 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001957 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001958 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001959 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001960 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001961 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001962 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1963 wl_list_for_each_reverse_safe(surf,
1964 prev,
1965 &shell->fullscreen_layer.surface_list,
1966 layer_link)
1967 weston_surface_restack(surf,
1968 &shell->toplevel_layer.surface_list);
1969 }
1970
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001971 weston_surface_restack(es,
1972 &shell->toplevel_layer.surface_list);
1973 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001974 }
1975}
1976
Alex Wu21858432012-04-01 20:13:08 +08001977/* no-op func for checking black surface */
1978static void
1979black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1980{
1981}
1982
1983static bool
1984is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1985{
1986 if (es->configure == black_surface_configure) {
1987 if (fs_surface)
1988 *fs_surface = (struct weston_surface *)es->private;
1989 return true;
1990 }
1991 return false;
1992}
1993
Kristian Høgsberg75840622011-09-06 13:48:16 -04001994static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001995click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001996 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001997{
Daniel Stone37816df2012-05-16 18:45:18 +01001998 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03001999 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002000 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002001 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002002
Daniel Stone37816df2012-05-16 18:45:18 +01002003 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002004 if (!focus)
2005 return;
2006
Alex Wu21858432012-04-01 20:13:08 +08002007 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002008 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002009
Daniel Stone325fc2d2012-05-30 16:31:58 +01002010 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002011 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002012}
2013
2014static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002015lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002016{
Tiago Vignattibe143262012-04-16 17:31:41 +03002017 struct desktop_shell *shell =
2018 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01002019 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002020 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002021 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002022
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002023 if (shell->locked) {
2024 wl_list_for_each(output, &shell->compositor->output_list, link)
2025 /* TODO: find a way to jump to other DPMS levels */
2026 if (output->set_dpms)
2027 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002028 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002029 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002030
2031 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002032
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002033 /* Hide all surfaces by removing the fullscreen, panel and
2034 * toplevel layers. This way nothing else can show or receive
2035 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002036
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002037 wl_list_remove(&shell->panel_layer.link);
2038 wl_list_remove(&shell->toplevel_layer.link);
2039 wl_list_remove(&shell->fullscreen_layer.link);
2040 wl_list_insert(&shell->compositor->cursor_layer.link,
2041 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002042
Pekka Paalanen77346a62011-11-30 16:26:35 +02002043 launch_screensaver(shell);
2044
2045 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2046 show_screensaver(shell, shsurf);
2047
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002048 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002049 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 weston_compositor_wake(shell->compositor);
2051 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002052 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002053
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002054 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002055 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002056
2057 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01002058 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2059 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002060 }
2061
2062 /* TODO: disable bindings that should not work while locked. */
2063
2064 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002065}
2066
2067static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002068unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002069{
Tiago Vignattibe143262012-04-16 17:31:41 +03002070 struct desktop_shell *shell =
2071 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002072
Pekka Paalanend81c2162011-11-16 13:47:34 +02002073 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002074 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002075 return;
2076 }
2077
2078 /* If desktop-shell client has gone away, unlock immediately. */
2079 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002080 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002081 return;
2082 }
2083
2084 if (shell->prepare_event_sent)
2085 return;
2086
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002087 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002088 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002089}
2090
2091static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002092center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002093{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002094 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002095 GLfloat x = (mode->width - surface->geometry.width) / 2;
2096 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002097
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002098 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002099}
2100
2101static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002102map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002103 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002104{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002105 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002106 struct shell_surface *shsurf;
2107 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002108 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002109 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08002110 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002111
Pekka Paalanen77346a62011-11-30 16:26:35 +02002112 shsurf = get_shell_surface(surface);
2113 if (shsurf)
2114 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002115
Pekka Paalanen60921e52012-01-25 15:55:43 +02002116 surface->geometry.width = width;
2117 surface->geometry.height = height;
2118 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002119
2120 /* initial positioning, see also configure() */
2121 switch (surface_type) {
2122 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002123 weston_surface_set_position(surface, 10 + random() % 400,
2124 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002125 break;
2126 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002127 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002128 break;
Alex Wu4539b082012-03-01 12:57:46 +08002129 case SHELL_SURFACE_FULLSCREEN:
2130 shell_map_fullscreen(shsurf);
2131 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002132 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002133 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002134 panel_height = get_output_panel_height(shell,surface->output);
2135 weston_surface_set_position(surface, surface->output->x,
2136 surface->output->y + panel_height);
2137 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002138 case SHELL_SURFACE_LOCK:
2139 center_on_output(surface, get_default_output(compositor));
2140 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002141 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002142 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002143 case SHELL_SURFACE_NONE:
2144 weston_surface_set_position(surface,
2145 surface->geometry.x + sx,
2146 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002147 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002148 default:
2149 ;
2150 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002151
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002152 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002153 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002154 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002155 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002156 wl_list_insert(&shell->background_layer.surface_list,
2157 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002158 break;
2159 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002160 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002161 wl_list_insert(&shell->panel_layer.surface_list,
2162 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002163 break;
2164 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002165 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002166 wl_list_insert(&shell->lock_layer.surface_list,
2167 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002168 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002169 break;
2170 case SHELL_SURFACE_SCREENSAVER:
2171 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002172 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002173 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002174 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002175 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002176 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002177 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002178 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002179 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002180 case SHELL_SURFACE_POPUP:
2181 case SHELL_SURFACE_TRANSIENT:
2182 parent = shsurf->parent->surface;
2183 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2184 break;
Alex Wu4539b082012-03-01 12:57:46 +08002185 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002186 case SHELL_SURFACE_NONE:
2187 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002188 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002189 wl_list_insert(&shell->toplevel_layer.surface_list,
2190 &surface->layer_link);
2191 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002192 }
2193
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002194 if (surface_type != SHELL_SURFACE_NONE) {
2195 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002196 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2197 surface->output = shsurf->output;
2198 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002199
Juan Zhao7bb92f02011-12-15 11:31:51 -05002200 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002201 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002202 if (shsurf->transient.flags ==
2203 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2204 break;
2205 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002206 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002207 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002208 if (!shell->locked) {
2209 wl_list_for_each(seat, &compositor->seat_list, link)
2210 activate(shell, surface, seat);
2211 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002212 break;
2213 default:
2214 break;
2215 }
2216
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002217 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002218 {
2219 switch (shell->win_animation_type) {
2220 case ANIMATION_FADE:
2221 weston_fade_run(surface, NULL, NULL);
2222 break;
2223 case ANIMATION_ZOOM:
2224 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2225 break;
2226 default:
2227 break;
2228 }
2229 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002230}
2231
2232static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002233configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002234 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002235{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002236 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2237 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002238
Pekka Paalanen77346a62011-11-30 16:26:35 +02002239 shsurf = get_shell_surface(surface);
2240 if (shsurf)
2241 surface_type = shsurf->type;
2242
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002243 surface->geometry.x = x;
2244 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002245 surface->geometry.width = width;
2246 surface->geometry.height = height;
2247 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002248
2249 switch (surface_type) {
2250 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002251 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002252 break;
Alex Wu4539b082012-03-01 12:57:46 +08002253 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002254 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002255 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002256 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002257 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002258 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002259 surface->geometry.x = surface->output->x;
2260 surface->geometry.y = surface->output->y +
2261 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002262 break;
Alex Wu4539b082012-03-01 12:57:46 +08002263 case SHELL_SURFACE_TOPLEVEL:
2264 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002265 default:
2266 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002267 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002268
Alex Wu4539b082012-03-01 12:57:46 +08002269 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002270 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002271 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002272
2273 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2274 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002275 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2276 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002277 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002278}
2279
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002280static void
2281shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2282{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002283 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002284 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002285 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002286
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002287 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002288 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002289 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002290 type_changed = 1;
2291 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002292
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002293 if (!weston_surface_is_mapped(es)) {
2294 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002295 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002296 es->geometry.width != es->buffer->width ||
2297 es->geometry.height != es->buffer->height) {
2298 GLfloat from_x, from_y;
2299 GLfloat to_x, to_y;
2300
2301 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2302 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2303 configure(shell, es,
2304 es->geometry.x + to_x - from_x,
2305 es->geometry.y + to_y - from_y,
2306 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002307 }
2308}
2309
Tiago Vignattibe143262012-04-16 17:31:41 +03002310static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002311
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002312static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002313desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002314{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002315 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002316 struct desktop_shell *shell =
2317 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002318
2319 shell->child.process.pid = 0;
2320 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002321
2322 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2323 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002324 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002325 shell->child.deathstamp = time;
2326 shell->child.deathcount = 0;
2327 }
2328
2329 shell->child.deathcount++;
2330 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002331 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002332 return;
2333 }
2334
Martin Minarik6d118362012-06-07 18:01:59 +02002335 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002336 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002337}
2338
2339static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002340launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002341{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002342 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002343
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002344 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002345 &shell->child.process,
2346 shell_exe,
2347 desktop_shell_sigchld);
2348
2349 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002350 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002351 return 0;
2352}
2353
2354static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002355bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2356{
Tiago Vignattibe143262012-04-16 17:31:41 +03002357 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002358
2359 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002360 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002361}
2362
Kristian Høgsberg75840622011-09-06 13:48:16 -04002363static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002364unbind_desktop_shell(struct wl_resource *resource)
2365{
Tiago Vignattibe143262012-04-16 17:31:41 +03002366 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002367
2368 if (shell->locked)
2369 resume_desktop(shell);
2370
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002371 shell->child.desktop_shell = NULL;
2372 shell->prepare_event_sent = false;
2373 free(resource);
2374}
2375
2376static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002377bind_desktop_shell(struct wl_client *client,
2378 void *data, uint32_t version, uint32_t id)
2379{
Tiago Vignattibe143262012-04-16 17:31:41 +03002380 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002381 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002382
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002383 resource = wl_client_add_object(client, &desktop_shell_interface,
2384 &desktop_shell_implementation,
2385 id, shell);
2386
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002387 if (client == shell->child.client) {
2388 resource->destroy = unbind_desktop_shell;
2389 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002390 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002391 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002392
2393 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2394 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002395 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002396}
2397
Pekka Paalanen6e168112011-11-24 11:34:05 +02002398static void
2399screensaver_set_surface(struct wl_client *client,
2400 struct wl_resource *resource,
2401 struct wl_resource *shell_surface_resource,
2402 struct wl_resource *output_resource)
2403{
Tiago Vignattibe143262012-04-16 17:31:41 +03002404 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002405 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002406 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002407
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002408 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002409
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002410 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002411 surface->output = output;
2412 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002413}
2414
2415static const struct screensaver_interface screensaver_implementation = {
2416 screensaver_set_surface
2417};
2418
2419static void
2420unbind_screensaver(struct wl_resource *resource)
2421{
Tiago Vignattibe143262012-04-16 17:31:41 +03002422 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002423
Pekka Paalanen77346a62011-11-30 16:26:35 +02002424 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002425 free(resource);
2426}
2427
2428static void
2429bind_screensaver(struct wl_client *client,
2430 void *data, uint32_t version, uint32_t id)
2431{
Tiago Vignattibe143262012-04-16 17:31:41 +03002432 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002433 struct wl_resource *resource;
2434
2435 resource = wl_client_add_object(client, &screensaver_interface,
2436 &screensaver_implementation,
2437 id, shell);
2438
Pekka Paalanen77346a62011-11-30 16:26:35 +02002439 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002440 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002441 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002442 return;
2443 }
2444
2445 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2446 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002447 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002448}
2449
Kristian Høgsberg07045392012-02-19 18:52:44 -05002450struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002451 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002452 struct weston_surface *current;
2453 struct wl_listener listener;
2454 struct wl_keyboard_grab grab;
2455};
2456
2457static void
2458switcher_next(struct switcher *switcher)
2459{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002460 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002461 struct weston_surface *surface;
2462 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002463 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002464
2465 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002466 switch (get_shell_surface_type(surface)) {
2467 case SHELL_SURFACE_TOPLEVEL:
2468 case SHELL_SURFACE_FULLSCREEN:
2469 case SHELL_SURFACE_MAXIMIZED:
2470 if (first == NULL)
2471 first = surface;
2472 if (prev == switcher->current)
2473 next = surface;
2474 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002475 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002476 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002477 weston_surface_damage(surface);
2478 break;
2479 default:
2480 break;
2481 }
Alex Wu1659daa2012-04-01 20:13:09 +08002482
2483 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002484 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002485 surface->geometry.dirty = 1;
2486 weston_surface_damage(surface);
2487 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002488 }
2489
2490 if (next == NULL)
2491 next = first;
2492
Alex Wu07b26062012-03-12 16:06:01 +08002493 if (next == NULL)
2494 return;
2495
Kristian Høgsberg07045392012-02-19 18:52:44 -05002496 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002497 wl_signal_add(&next->surface.resource.destroy_signal,
2498 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002499
2500 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002501 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002502
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002503 shsurf = get_shell_surface(switcher->current);
2504 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002505 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002506}
2507
2508static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002509switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002510{
2511 struct switcher *switcher =
2512 container_of(listener, struct switcher, listener);
2513
2514 switcher_next(switcher);
2515}
2516
2517static void
Daniel Stone351eb612012-05-31 15:27:47 -04002518switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002519{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002520 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002521 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002522 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002523
2524 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002525 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002526 weston_surface_damage(surface);
2527 }
2528
Alex Wu07b26062012-03-12 16:06:01 +08002529 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002530 activate(switcher->shell, switcher->current,
2531 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002532 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002533 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002534 free(switcher);
2535}
2536
2537static void
2538switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002539 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002540{
2541 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002542 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002543
Daniel Stonec9785ea2012-05-30 16:31:52 +01002544 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002545 switcher_next(switcher);
2546}
2547
2548static void
2549switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2550 uint32_t mods_depressed, uint32_t mods_latched,
2551 uint32_t mods_locked, uint32_t group)
2552{
2553 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002554 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002555
Daniel Stone351eb612012-05-31 15:27:47 -04002556 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
2557 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002558}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002559
2560static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04002561 switcher_key,
2562 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05002563};
2564
2565static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002566switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2567 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002568{
Tiago Vignattibe143262012-04-16 17:31:41 +03002569 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002570 struct switcher *switcher;
2571
2572 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002573 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002574 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002575 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002576 wl_list_init(&switcher->listener.link);
2577
2578 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002579 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2580 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002581 switcher_next(switcher);
2582}
2583
Pekka Paalanen3c647232011-12-22 13:43:43 +02002584static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002585backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2586 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002587{
2588 struct weston_compositor *compositor = data;
2589 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002590 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002591
2592 /* TODO: we're limiting to simple use cases, where we assume just
2593 * control on the primary display. We'd have to extend later if we
2594 * ever get support for setting backlights on random desktop LCD
2595 * panels though */
2596 output = get_default_output(compositor);
2597 if (!output)
2598 return;
2599
2600 if (!output->set_backlight)
2601 return;
2602
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002603 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2604 backlight_new = output->backlight_current - 25;
2605 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2606 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002607
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002608 if (backlight_new < 5)
2609 backlight_new = 5;
2610 if (backlight_new > 255)
2611 backlight_new = 255;
2612
2613 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002614 output->set_backlight(output, output->backlight_current);
2615}
2616
2617static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002618debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2619 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002620{
Tiago Vignattibe143262012-04-16 17:31:41 +03002621 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002622 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002623 struct weston_surface *surface;
2624
2625 if (shell->debug_repaint_surface) {
2626 weston_surface_destroy(shell->debug_repaint_surface);
2627 shell->debug_repaint_surface = NULL;
2628 } else {
2629 surface = weston_surface_create(compositor);
2630 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2631 weston_surface_configure(surface, 0, 0, 8192, 8192);
2632 wl_list_insert(&compositor->fade_layer.surface_list,
2633 &surface->layer_link);
2634 weston_surface_assign_output(surface);
2635 pixman_region32_init(&surface->input);
2636
2637 /* Here's the dirty little trick that makes the
2638 * repaint debugging work: we force an
2639 * update_transform first to update dependent state
2640 * and clear the geometry.dirty bit. Then we clear
2641 * the surface damage so it only gets repainted
2642 * piecewise as we repaint other things. */
2643
2644 weston_surface_update_transform(surface);
2645 pixman_region32_fini(&surface->damage);
2646 pixman_region32_init(&surface->damage);
2647 shell->debug_repaint_surface = surface;
2648 }
2649}
2650
2651static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002652force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2653 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002654{
2655 struct wl_client *client;
2656 pid_t pid;
2657 uid_t uid;
2658 gid_t gid;
2659
Daniel Stone325fc2d2012-05-30 16:31:58 +01002660 client = seat->keyboard->focus->resource.client;
2661 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002662
Daniel Stone325fc2d2012-05-30 16:31:58 +01002663 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002664}
2665
2666static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002667shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002668{
Tiago Vignattibe143262012-04-16 17:31:41 +03002669 struct desktop_shell *shell =
2670 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002671
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002672 if (shell->child.client)
2673 wl_client_destroy(shell->child.client);
2674
Kristian Høgsberg88c16072012-05-16 08:04:19 -04002675 wl_list_remove(&shell->lock_listener.link);
2676 wl_list_remove(&shell->unlock_listener.link);
2677
Pekka Paalanen3c647232011-12-22 13:43:43 +02002678 free(shell->screensaver.path);
2679 free(shell);
2680}
2681
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002682static void
2683shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2684{
2685 uint32_t mod;
2686
2687 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01002688 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
2689 MODIFIER_CTRL | MODIFIER_ALT,
2690 terminate_binding, ec);
2691 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
2692 click_to_activate_binding,
2693 shell);
2694 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2695 MODIFIER_SUPER | MODIFIER_ALT,
2696 surface_opacity_binding, NULL);
2697 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2698 MODIFIER_SUPER, zoom_axis_binding,
2699 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002700
2701 /* configurable bindings */
2702 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002703 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
2704 zoom_key_binding, NULL);
2705 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
2706 zoom_key_binding, NULL);
2707 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
2708 shell);
2709 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
2710 resize_binding, shell);
2711 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
2712 rotate_binding, NULL);
2713 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
2714 shell);
2715 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
2716 ec);
2717 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
2718 backlight_binding, ec);
2719 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
2720 ec);
2721 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
2722 backlight_binding, ec);
2723 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
2724 debug_repaint_binding, shell);
2725 weston_compositor_add_key_binding(ec, KEY_K, mod,
2726 force_kill_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002727}
2728
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002729int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002730shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002731
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002732WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002733shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002734{
Tiago Vignattibe143262012-04-16 17:31:41 +03002735 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002736
2737 shell = malloc(sizeof *shell);
2738 if (shell == NULL)
2739 return -1;
2740
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002741 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002742 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002743
2744 shell->destroy_listener.notify = shell_destroy;
2745 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2746 shell->lock_listener.notify = lock;
2747 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2748 shell->unlock_listener.notify = unlock;
2749 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002750 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002751 ec->shell_interface.create_shell_surface = create_shell_surface;
2752 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002753 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002754 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002755 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002756
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002757 wl_list_init(&shell->backgrounds);
2758 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002759 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002760
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002761 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2762 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2763 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2764 weston_layer_init(&shell->background_layer,
2765 &shell->toplevel_layer.link);
2766 wl_list_init(&shell->lock_layer.surface_list);
2767
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002768 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002769
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002770 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2771 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002772 return -1;
2773
Kristian Høgsberg75840622011-09-06 13:48:16 -04002774 if (wl_display_add_global(ec->wl_display,
2775 &desktop_shell_interface,
2776 shell, bind_desktop_shell) == NULL)
2777 return -1;
2778
Pekka Paalanen6e168112011-11-24 11:34:05 +02002779 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2780 shell, bind_screensaver) == NULL)
2781 return -1;
2782
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002783 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002784 if (launch_desktop_shell_process(shell) != 0)
2785 return -1;
2786
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002787 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03002788 if (ec->seat->seat.pointer)
2789 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
2790 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002791
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002792 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002793
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002794 return 0;
2795}