blob: cae13c5511520d470c973a2cbbd27595f35fb06d [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
43 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
45 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020048 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020049
50 unsigned deathcount;
51 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020053
54 bool locked;
55 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020056
Pekka Paalanen068ae942011-11-28 14:11:15 +020057 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050058 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020059 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060
61 struct wl_list backgrounds;
62 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020063
Pekka Paalanen77346a62011-11-30 16:26:35 +020064 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020065 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020066 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020067 struct wl_resource *binding;
68 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020070 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040071};
72
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050073enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020074 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050075
Pekka Paalanen57da4a82011-11-23 16:42:16 +020076 SHELL_SURFACE_PANEL,
77 SHELL_SURFACE_BACKGROUND,
78 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020079 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080
81 SHELL_SURFACE_TOPLEVEL,
82 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050083 SHELL_SURFACE_FULLSCREEN,
84 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020085};
86
Pekka Paalanen56cdea92011-11-23 16:14:12 +020087struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020088 struct wl_resource resource;
89
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020091 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050092 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050094 enum shell_surface_type type;
95 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010096
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050097 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +020098 struct weston_transform transform;
99 } rotation;
100
101 struct {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 struct wl_grab grab;
103 uint32_t time;
104 int32_t x, y;
105 int32_t initial_up;
106 } popup;
107
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100109 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110};
111
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500113 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500115 int32_t dx, dy;
116};
117
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118struct rotate_grab {
119 struct wl_grab grab;
120 struct shell_surface *surface;
121 struct {
122 int32_t x;
123 int32_t y;
124 } center;
125};
126
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500127static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200128shell_configuration(struct wl_shell *shell)
129{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200130 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200131 char *path = NULL;
132 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200133
134 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200135 { "path", CONFIG_KEY_STRING, &path },
136 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200137 };
138
139 struct config_section cs[] = {
140 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
141 };
142
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500143 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500144 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200145 free(config_file);
146
Pekka Paalanen7296e792011-12-07 16:22:00 +0200147 shell->screensaver.path = path;
148 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200149}
150
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500152noop_grab_focus(struct wl_grab *grab, uint32_t time,
153 struct wl_surface *surface, int32_t x, int32_t y)
154{
155 grab->focus = NULL;
156}
157
158static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500160 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500161{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500163 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500166 weston_surface_configure(es,
167 device->x + move->dx,
168 device->y + move->dy,
169 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500170}
171
172static void
173move_grab_button(struct wl_grab *grab,
174 uint32_t time, int32_t button, int32_t state)
175{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500176 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500178 if (device->button_count == 0 && state == 0) {
179 wl_input_device_end_grab(device, time);
180 free(grab);
181 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182}
183
184static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500185 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186 move_grab_motion,
187 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188};
189
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400190static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500192 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500195
196 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400197 if (!move)
198 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199
200 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200201 move->dx = es->geometry.x - wd->input_device.grab_x;
202 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500203 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500204
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500205 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400207 wl_input_device_set_pointer_focus(&wd->input_device,
208 NULL, time, 0, 0, 0, 0);
209
210 return 0;
211}
212
213static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200214shell_surface_move(struct wl_client *client, struct wl_resource *resource,
215 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400216{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500217 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200218 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400219
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500220 if (wd->input_device.button_count == 0 ||
221 wd->input_device.grab_time != time ||
222 wd->input_device.pointer_focus != &shsurf->surface->surface)
223 return;
224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400226 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227}
228
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230 struct wl_grab grab;
231 uint32_t edges;
232 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200233 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234};
235
236static void
237resize_grab_motion(struct wl_grab *grab,
238 uint32_t time, int32_t x, int32_t y)
239{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500240 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500241 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242 int32_t width, height;
243
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200244 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500245 width = device->grab_x - device->x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200246 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500247 width = device->x - device->grab_x + resize->width;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248 } else {
249 width = resize->width;
250 }
251
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200252 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500253 height = device->grab_y - device->y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200254 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500255 height = device->y - device->grab_y + resize->height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256 } else {
257 height = resize->height;
258 }
259
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200260 wl_resource_post_event(&resize->shsurf->resource,
261 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
262 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263}
264
265static void
266resize_grab_button(struct wl_grab *grab,
267 uint32_t time, int32_t button, int32_t state)
268{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500269 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500271 if (device->button_count == 0 && state == 0) {
272 wl_input_device_end_grab(device, time);
273 free(grab);
274 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275}
276
277static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500278 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279 resize_grab_motion,
280 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500281};
282
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400283static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500284weston_surface_resize(struct shell_surface *shsurf,
285 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200286 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500288 struct weston_resize_grab *resize;
289 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500290
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500291 /* FIXME: Reject if fullscreen */
292
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500293 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400294 if (!resize)
295 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500296
297 resize->grab.interface = &resize_grab_interface;
298 resize->edges = edges;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200299 resize->dx = es->geometry.x - wd->input_device.grab_x;
300 resize->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500301 resize->width = es->width;
302 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200303 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400304
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500305 if (edges == 0 || edges > 15 ||
306 (edges & 3) == 3 || (edges & 12) == 12)
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200307 goto err_out;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500309 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400311 wl_input_device_set_pointer_focus(&wd->input_device,
312 NULL, time, 0, 0, 0, 0);
313
314 return 0;
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200315
316err_out:
317 free(resize);
318 return 0;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400319}
320
321static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200322shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
323 struct wl_resource *input_resource, uint32_t time,
324 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400325{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500326 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200327 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400328
329 /* FIXME: Reject if fullscreen */
330
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500331 if (wd->input_device.button_count == 0 ||
332 wd->input_device.grab_time != time ||
333 wd->input_device.pointer_focus != &shsurf->surface->surface)
334 return;
335
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500336 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400337 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500338}
339
Pekka Paalanen98262232011-12-01 10:42:22 +0200340static int
341reset_shell_surface_type(struct shell_surface *surface)
342{
343 switch (surface->type) {
344 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200345 surface->surface->geometry.x = surface->saved_x;
346 surface->surface->geometry.y = surface->saved_y;
347 surface->surface->geometry.dirty = 1;
Pekka Paalanen98262232011-12-01 10:42:22 +0200348 surface->surface->fullscreen_output = NULL;
349 break;
350 case SHELL_SURFACE_PANEL:
351 case SHELL_SURFACE_BACKGROUND:
352 wl_list_remove(&surface->link);
353 wl_list_init(&surface->link);
354 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200355 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200356 case SHELL_SURFACE_LOCK:
357 wl_resource_post_error(&surface->resource,
358 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200359 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200360 return -1;
361 case SHELL_SURFACE_NONE:
362 case SHELL_SURFACE_TOPLEVEL:
363 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500364 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200365 break;
366 }
367
368 surface->type = SHELL_SURFACE_NONE;
369 return 0;
370}
371
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500372static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200373shell_surface_set_toplevel(struct wl_client *client,
374 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400375
376{
Pekka Paalanen98262232011-12-01 10:42:22 +0200377 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400378
Pekka Paalanen98262232011-12-01 10:42:22 +0200379 if (reset_shell_surface_type(surface))
380 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400381
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500382 weston_surface_damage(surface->surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200383 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400384}
385
386static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200387shell_surface_set_transient(struct wl_client *client,
388 struct wl_resource *resource,
389 struct wl_resource *parent_resource,
390 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400391{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200392 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500393 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200394 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500395 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400396
Pekka Paalanen98262232011-12-01 10:42:22 +0200397 if (reset_shell_surface_type(shsurf))
398 return;
399
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400400 /* assign to parents output */
401 es->output = pes->output;
402
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200403 es->geometry.x = pes->geometry.x + x;
404 es->geometry.y = pes->geometry.y + y;
405 es->geometry.dirty = 1;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400406
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500407 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200408 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400409}
410
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411static struct weston_output *
412get_default_output(struct weston_compositor *compositor)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200413{
414 return container_of(compositor->output_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500415 struct weston_output, link);
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200416}
417
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400418static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200419shell_surface_set_fullscreen(struct wl_client *client,
420 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400421
422{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200423 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500424 struct weston_surface *es = shsurf->surface;
425 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400426
Pekka Paalanen98262232011-12-01 10:42:22 +0200427 if (reset_shell_surface_type(shsurf))
428 return;
429
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400430 /* FIXME: Fullscreen on first output */
431 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200432 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400433 es->output = output;
434
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200435 shsurf->saved_x = es->geometry.x;
436 shsurf->saved_y = es->geometry.y;
437 es->geometry.x = (output->current->width - es->width) / 2;
438 es->geometry.y = (output->current->height - es->height) / 2;
439 es->geometry.dirty = 1;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400440 es->fullscreen_output = output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500441 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200442 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400443}
444
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500445static void
446popup_grab_focus(struct wl_grab *grab, uint32_t time,
447 struct wl_surface *surface, int32_t x, int32_t y)
448{
449 struct wl_input_device *device = grab->input_device;
450 struct shell_surface *priv =
451 container_of(grab, struct shell_surface, popup.grab);
452 struct wl_client *client = priv->surface->surface.resource.client;
453
Pekka Paalanencb108432012-01-19 16:25:40 +0200454 if (surface && surface->resource.client == client) {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500455 wl_input_device_set_pointer_focus(device, surface, time,
456 device->x, device->y, x, y);
457 grab->focus = surface;
458 } else {
459 wl_input_device_set_pointer_focus(device, NULL,
460 time, 0, 0, 0, 0);
461 grab->focus = NULL;
462 }
463}
464
465static void
466popup_grab_motion(struct wl_grab *grab,
467 uint32_t time, int32_t x, int32_t y)
468{
469 struct wl_input_device *device = grab->input_device;
470 struct wl_resource *resource;
471
472 resource = grab->input_device->pointer_focus_resource;
473 if (resource)
474 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
475 time, device->x, device->y, x, y);
476}
477
478static void
479popup_grab_button(struct wl_grab *grab,
480 uint32_t time, int32_t button, int32_t state)
481{
482 struct wl_resource *resource;
483 struct shell_surface *shsurf =
484 container_of(grab, struct shell_surface, popup.grab);
485
486 resource = grab->input_device->pointer_focus_resource;
487 if (resource) {
488 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
489 time, button, state);
490 } else if (state == 0 &&
491 (shsurf->popup.initial_up ||
492 time - shsurf->popup.time > 500)) {
493 wl_resource_post_event(&shsurf->resource,
494 WL_SHELL_SURFACE_POPUP_DONE);
495 wl_input_device_end_grab(grab->input_device, time);
496 shsurf->popup.grab.input_device = NULL;
497 }
498
499 if (state == 0)
500 shsurf->popup.initial_up = 1;
501}
502
503static const struct wl_grab_interface popup_grab_interface = {
504 popup_grab_focus,
505 popup_grab_motion,
506 popup_grab_button,
507};
508
509static void
510shell_map_popup(struct shell_surface *shsurf, uint32_t time)
511{
512 struct wl_input_device *device;
513 struct weston_surface *es = shsurf->surface;
514 struct weston_surface *parent = shsurf->parent->surface;
515
516 es->output = parent->output;
517
518 shsurf->popup.grab.interface = &popup_grab_interface;
519 device = es->compositor->input_device;
520
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200521 es->geometry.x = shsurf->parent->surface->geometry.x + shsurf->popup.x;
522 es->geometry.y = shsurf->parent->surface->geometry.y + shsurf->popup.y;
523 es->geometry.dirty = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500524
525 shsurf->popup.grab.input_device = device;
526 shsurf->popup.time = device->grab_time;
527 shsurf->popup.initial_up = 0;
528
529 wl_input_device_start_grab(shsurf->popup.grab.input_device,
530 &shsurf->popup.grab, shsurf->popup.time);
531}
532
533static void
534shell_surface_set_popup(struct wl_client *client,
535 struct wl_resource *resource,
536 struct wl_resource *input_device_resource,
537 uint32_t time,
538 struct wl_resource *parent_resource,
539 int32_t x, int32_t y, uint32_t flags)
540{
541 struct shell_surface *shsurf = resource->data;
542 struct weston_surface *es = shsurf->surface;
543
544 weston_surface_damage(es);
545 shsurf->type = SHELL_SURFACE_POPUP;
546 shsurf->parent = parent_resource->data;
547 shsurf->popup.x = x;
548 shsurf->popup.y = y;
549}
550
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200551static const struct wl_shell_surface_interface shell_surface_implementation = {
552 shell_surface_move,
553 shell_surface_resize,
554 shell_surface_set_toplevel,
555 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500556 shell_surface_set_fullscreen,
557 shell_surface_set_popup
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200558};
559
560static void
561destroy_shell_surface(struct wl_resource *resource)
562{
563 struct shell_surface *shsurf = resource->data;
564
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500565 if (shsurf->popup.grab.input_device)
566 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
567
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200568 /* in case cleaning up a dead client destroys shell_surface first */
569 if (shsurf->surface)
570 wl_list_remove(&shsurf->surface_destroy_listener.link);
571
572 wl_list_remove(&shsurf->link);
573 free(shsurf);
574}
575
576static void
577shell_handle_surface_destroy(struct wl_listener *listener,
578 struct wl_resource *resource, uint32_t time)
579{
580 struct shell_surface *shsurf = container_of(listener,
581 struct shell_surface,
582 surface_destroy_listener);
583
584 shsurf->surface = NULL;
585 wl_resource_destroy(&shsurf->resource, time);
586}
587
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200588static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500589get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200590{
591 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
592 struct wl_listener *listener;
593
594 /* search the destroy listener list for our callback */
595 wl_list_for_each(listener, lst, link) {
596 if (listener->func == shell_handle_surface_destroy) {
597 return container_of(listener, struct shell_surface,
598 surface_destroy_listener);
599 }
600 }
601
602 return NULL;
603}
604
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200605static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200606shell_get_shell_surface(struct wl_client *client,
607 struct wl_resource *resource,
608 uint32_t id,
609 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200610{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500611 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200612 struct shell_surface *shsurf;
613
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200614 if (get_shell_surface(surface)) {
615 wl_resource_post_error(surface_resource,
616 WL_DISPLAY_ERROR_INVALID_OBJECT,
617 "wl_shell::get_shell_surface already requested");
618 return;
619 }
620
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200621 shsurf = calloc(1, sizeof *shsurf);
622 if (!shsurf) {
623 wl_resource_post_no_memory(resource);
624 return;
625 }
626
627 shsurf->resource.destroy = destroy_shell_surface;
628 shsurf->resource.object.id = id;
629 shsurf->resource.object.interface = &wl_shell_surface_interface;
630 shsurf->resource.object.implementation =
631 (void (**)(void)) &shell_surface_implementation;
632 shsurf->resource.data = shsurf;
633
634 shsurf->surface = surface;
635 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
636 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
637 &shsurf->surface_destroy_listener.link);
638
639 /* init link so its safe to always remove it in destroy_shell_surface */
640 wl_list_init(&shsurf->link);
641
Pekka Paalanen460099f2012-01-20 16:48:25 +0200642 /* empty when not in use */
643 wl_list_init(&shsurf->rotation.transform.link);
644
Pekka Paalanen98262232011-12-01 10:42:22 +0200645 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200646
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200647 wl_client_add_resource(client, &shsurf->resource);
648}
649
650static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200651 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500652};
653
Kristian Høgsberg07937562011-04-12 17:25:42 -0400654static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500655handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200656{
657 proc->pid = 0;
658}
659
660static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200661launch_screensaver(struct wl_shell *shell)
662{
663 if (shell->screensaver.binding)
664 return;
665
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200666 if (!shell->screensaver.path)
667 return;
668
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500669 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200670 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200671 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200672 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200673}
674
675static void
676terminate_screensaver(struct wl_shell *shell)
677{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200678 if (shell->screensaver.process.pid == 0)
679 return;
680
681 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200682}
683
684static void
685show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
686{
687 struct wl_list *list;
688
689 if (shell->lock_surface)
690 list = &shell->lock_surface->surface->link;
691 else
692 list = &shell->compositor->surface_list;
693
694 wl_list_remove(&surface->surface->link);
695 wl_list_insert(list, &surface->surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500696 weston_surface_configure(surface->surface,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200697 surface->surface->geometry.x,
698 surface->surface->geometry.y,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200699 surface->surface->width,
700 surface->surface->height);
701 surface->surface->output = surface->output;
702}
703
704static void
705hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
706{
707 wl_list_remove(&surface->surface->link);
708 wl_list_init(&surface->surface->link);
709 surface->surface->output = NULL;
710}
711
712static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400713desktop_shell_set_background(struct wl_client *client,
714 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100715 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400716 struct wl_resource *surface_resource)
717{
718 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200719 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500720 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200721 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400722
Pekka Paalanen98262232011-12-01 10:42:22 +0200723 if (reset_shell_surface_type(shsurf))
724 return;
725
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100726 wl_list_for_each(priv, &shell->backgrounds, link) {
727 if (priv->output == output_resource->data) {
728 priv->surface->output = NULL;
729 wl_list_remove(&priv->surface->link);
730 wl_list_remove(&priv->link);
731 break;
732 }
733 }
734
Pekka Paalanen068ae942011-11-28 14:11:15 +0200735 shsurf->type = SHELL_SURFACE_BACKGROUND;
736 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100737
Pekka Paalanen068ae942011-11-28 14:11:15 +0200738 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100739
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200740 surface->geometry.x = shsurf->output->x;
741 surface->geometry.y = shsurf->output->y;
742 surface->geometry.dirty = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200743
Kristian Høgsberg75840622011-09-06 13:48:16 -0400744 wl_resource_post_event(resource,
745 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500746 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200747 shsurf->output->current->width,
748 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400749}
750
751static void
752desktop_shell_set_panel(struct wl_client *client,
753 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100754 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400755 struct wl_resource *surface_resource)
756{
757 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200758 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500759 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200760 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400761
Pekka Paalanen98262232011-12-01 10:42:22 +0200762 if (reset_shell_surface_type(shsurf))
763 return;
764
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100765 wl_list_for_each(priv, &shell->panels, link) {
766 if (priv->output == output_resource->data) {
767 priv->surface->output = NULL;
768 wl_list_remove(&priv->surface->link);
769 wl_list_remove(&priv->link);
770 break;
771 }
772 }
773
Pekka Paalanen068ae942011-11-28 14:11:15 +0200774 shsurf->type = SHELL_SURFACE_PANEL;
775 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100776
Pekka Paalanen068ae942011-11-28 14:11:15 +0200777 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100778
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200779 surface->geometry.x = shsurf->output->x;
780 surface->geometry.y = shsurf->output->y;
781 surface->geometry.dirty = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200782
Kristian Høgsberg75840622011-09-06 13:48:16 -0400783 wl_resource_post_event(resource,
784 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500785 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200786 shsurf->output->current->width,
787 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400788}
789
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200790static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500791handle_lock_surface_destroy(struct wl_listener *listener,
792 struct wl_resource *resource, uint32_t time)
793{
794 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200795 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500796
797 fprintf(stderr, "lock surface gone\n");
798 shell->lock_surface = NULL;
799}
800
801static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200802desktop_shell_set_lock_surface(struct wl_client *client,
803 struct wl_resource *resource,
804 struct wl_resource *surface_resource)
805{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200806 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200807 struct shell_surface *surface = surface_resource->data;
808
809 if (reset_shell_surface_type(surface))
810 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200811
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200812 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200813
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200814 if (!shell->locked)
815 return;
816
Pekka Paalanen98262232011-12-01 10:42:22 +0200817 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200818
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500819 shell->lock_surface_listener.func = handle_lock_surface_destroy;
820 wl_list_insert(&surface_resource->destroy_listener_list,
821 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200822
Pekka Paalanen068ae942011-11-28 14:11:15 +0200823 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200824}
825
826static void
827resume_desktop(struct wl_shell *shell)
828{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500829 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200830 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200831 struct shell_surface *tmp;
832
833 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
834 hide_screensaver(shell, tmp);
835
836 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200837
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500838 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200839 weston_surface_configure(surface, surface->geometry.x,
840 surface->geometry.y,
841 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200842
Pekka Paalanenfe340832011-11-25 16:07:52 +0200843 if (wl_list_empty(&shell->backgrounds)) {
844 list = &shell->compositor->surface_list;
845 } else {
846 struct shell_surface *background;
847 background = container_of(shell->backgrounds.prev,
848 struct shell_surface, link);
849 list = background->surface->link.prev;
850 }
851
852 if (!wl_list_empty(&shell->hidden_surface_list))
853 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500854 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200855
856 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500857 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200858 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500859 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200860}
861
862static void
863desktop_shell_unlock(struct wl_client *client,
864 struct wl_resource *resource)
865{
866 struct wl_shell *shell = resource->data;
867
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200868 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200869
870 if (shell->locked)
871 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200872}
873
Kristian Høgsberg75840622011-09-06 13:48:16 -0400874static const struct desktop_shell_interface desktop_shell_implementation = {
875 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200876 desktop_shell_set_panel,
877 desktop_shell_set_lock_surface,
878 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400879};
880
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200881static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500882get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200883{
884 struct shell_surface *shsurf;
885
886 shsurf = get_shell_surface(surface);
887 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200888 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200889 return shsurf->type;
890}
891
Kristian Høgsberg75840622011-09-06 13:48:16 -0400892static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400893move_binding(struct wl_input_device *device, uint32_t time,
894 uint32_t key, uint32_t button, uint32_t state, void *data)
895{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500896 struct weston_surface *surface =
897 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500898
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100899 if (surface == NULL)
900 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400901
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200902 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100903 case SHELL_SURFACE_PANEL:
904 case SHELL_SURFACE_BACKGROUND:
905 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200906 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100907 return;
908 default:
909 break;
910 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400911
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500912 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400913}
914
915static void
916resize_binding(struct wl_input_device *device, uint32_t time,
917 uint32_t key, uint32_t button, uint32_t state, void *data)
918{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500919 struct weston_surface *surface =
920 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400921 uint32_t edges = 0;
922 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200923 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500924
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100925 if (surface == NULL)
926 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200927
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200928 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200929 if (!shsurf)
930 return;
931
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200932 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100933 case SHELL_SURFACE_PANEL:
934 case SHELL_SURFACE_BACKGROUND:
935 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200936 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100937 return;
938 default:
939 break;
940 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400941
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200942 x = device->grab_x - surface->geometry.x;
943 y = device->grab_y - surface->geometry.y;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400944
945 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200946 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400947 else if (x < 2 * surface->width / 3)
948 edges |= 0;
949 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200950 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400951
952 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200953 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400954 else if (y < 2 * surface->height / 3)
955 edges |= 0;
956 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200957 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400958
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200960 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400961}
962
963static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500964terminate_binding(struct wl_input_device *device, uint32_t time,
965 uint32_t key, uint32_t button, uint32_t state, void *data)
966{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500967 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500968
969 if (state)
970 wl_display_terminate(compositor->wl_display);
971}
972
973static void
Pekka Paalanen460099f2012-01-20 16:48:25 +0200974rotate_grab_motion(struct wl_grab *grab,
975 uint32_t time, int32_t x, int32_t y)
976{
977 struct rotate_grab *rotate =
978 container_of(grab, struct rotate_grab, grab);
979 struct wl_input_device *device = grab->input_device;
980 struct shell_surface *surface = rotate->surface;
981 GLfloat dx, dy;
982 GLfloat r;
983
984 dx = device->x - rotate->center.x;
985 dy = device->y - rotate->center.y;
986 r = sqrtf(dx * dx + dy * dy);
987
988 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200989 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200990
991 if (r > 20.0f) {
992 struct weston_matrix roto;
993 struct weston_matrix *matrix =
994 &surface->rotation.transform.matrix;
995
996 weston_matrix_init(&roto);
997 roto.d[0] = dx / r;
998 roto.d[4] = -dy / r;
999 roto.d[1] = -roto.d[4];
1000 roto.d[5] = roto.d[0];
1001
1002 weston_matrix_init(matrix);
1003 weston_matrix_translate(matrix, -rotate->center.x,
1004 -rotate->center.y, 0.0f);
1005 weston_matrix_multiply(matrix, &roto);
1006 weston_matrix_translate(matrix, rotate->center.x,
1007 rotate->center.y, 0.0f);
1008
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001009 wl_list_insert(
1010 surface->surface->geometry.transformation_list.prev,
1011 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001012 } else {
1013 wl_list_init(&surface->rotation.transform.link);
1014 }
1015
1016 weston_compositor_damage_all(surface->surface->compositor);
1017}
1018
1019static void
1020rotate_grab_button(struct wl_grab *grab,
1021 uint32_t time, int32_t button, int32_t state)
1022{
1023 struct rotate_grab *rotate =
1024 container_of(grab, struct rotate_grab, grab);
1025 struct wl_input_device *device = grab->input_device;
1026
1027 if (device->button_count == 0 && state == 0) {
1028 wl_input_device_end_grab(device, time);
1029 free(rotate);
1030 }
1031}
1032
1033static const struct wl_grab_interface rotate_grab_interface = {
1034 noop_grab_focus,
1035 rotate_grab_motion,
1036 rotate_grab_button,
1037};
1038
1039static void
1040rotate_binding(struct wl_input_device *device, uint32_t time,
1041 uint32_t key, uint32_t button, uint32_t state, void *data)
1042{
1043 struct weston_surface *base_surface =
1044 (struct weston_surface *) device->pointer_focus;
1045 struct shell_surface *surface;
1046 struct rotate_grab *rotate;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001047
1048 if (base_surface == NULL)
1049 return;
1050
1051 surface = get_shell_surface(base_surface);
1052 if (!surface)
1053 return;
1054
1055 switch (surface->type) {
1056 case SHELL_SURFACE_PANEL:
1057 case SHELL_SURFACE_BACKGROUND:
1058 case SHELL_SURFACE_FULLSCREEN:
1059 case SHELL_SURFACE_SCREENSAVER:
1060 return;
1061 default:
1062 break;
1063 }
1064
Pekka Paalanen460099f2012-01-20 16:48:25 +02001065 rotate = malloc(sizeof *rotate);
1066 if (!rotate)
1067 return;
1068
1069 rotate->grab.interface = &rotate_grab_interface;
1070 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001071
1072 weston_surface_to_global(surface->surface,
1073 surface->surface->width / 2,
1074 surface->surface->height / 2,
1075 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001076
1077 wl_input_device_start_grab(device, &rotate->grab, time);
1078 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
1079}
1080
1081static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001082activate(struct weston_shell *base, struct weston_surface *es,
1083 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001084{
1085 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001086 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001087 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001088
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001089 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001090
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001091 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001092 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001093
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001094 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001095 case SHELL_SURFACE_BACKGROUND:
1096 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001097 wl_list_remove(&es->link);
1098 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001099 break;
1100 case SHELL_SURFACE_PANEL:
1101 /* already put on top */
1102 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001103 case SHELL_SURFACE_SCREENSAVER:
1104 /* always below lock surface */
1105 if (shell->lock_surface) {
1106 wl_list_remove(&es->link);
1107 wl_list_insert(&shell->lock_surface->surface->link,
1108 &es->link);
1109 }
1110 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001111 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001112 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001113 list = weston_compositor_top(compositor);
1114
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001115 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001116 struct shell_surface *panel;
1117 wl_list_for_each(panel, &shell->panels, link) {
1118 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001119 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001120 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001121 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001122 }
1123}
1124
1125static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001126click_to_activate_binding(struct wl_input_device *device,
1127 uint32_t time, uint32_t key,
1128 uint32_t button, uint32_t state, void *data)
1129{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001130 struct weston_input_device *wd = (struct weston_input_device *) device;
1131 struct weston_compositor *compositor = data;
1132 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001133
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001134 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001135 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001136 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001137}
1138
1139static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001140lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001141{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001142 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001143 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001144 struct weston_surface *cur;
1145 struct weston_surface *tmp;
1146 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001147 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001148 uint32_t time;
1149
1150 if (shell->locked)
1151 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001152
1153 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001154
1155 /* Move all surfaces from compositor's list to our hidden list,
1156 * except the background. This way nothing else can show or
1157 * receive input events while we are locked. */
1158
1159 if (!wl_list_empty(&shell->hidden_surface_list)) {
1160 fprintf(stderr,
1161 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1162 __func__);
1163 }
1164
1165 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1166 /* skip input device sprites, cur->surface is uninitialised */
1167 if (cur->surface.resource.client == NULL)
1168 continue;
1169
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001170 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001171 continue;
1172
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001173 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001174 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001175 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001176 }
1177
Pekka Paalanen77346a62011-11-30 16:26:35 +02001178 launch_screensaver(shell);
1179
1180 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1181 show_screensaver(shell, shsurf);
1182
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001183 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001184 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001185 weston_compositor_wake(shell->compositor);
1186 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001187 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001188
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001189 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001191
1192 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001193 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001194 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1195 wl_input_device_set_keyboard_focus(&device->input_device,
1196 NULL, time);
1197 }
1198
1199 /* TODO: disable bindings that should not work while locked. */
1200
1201 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001202}
1203
1204static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001205unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001206{
1207 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1208
Pekka Paalanend81c2162011-11-16 13:47:34 +02001209 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001210 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001211 return;
1212 }
1213
1214 /* If desktop-shell client has gone away, unlock immediately. */
1215 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001216 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001217 return;
1218 }
1219
1220 if (shell->prepare_event_sent)
1221 return;
1222
1223 wl_resource_post_event(shell->child.desktop_shell,
1224 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1225 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001226}
1227
1228static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001229center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001230{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001231 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001232
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001233 surface->geometry.x = output->x + (mode->width - surface->width) / 2;
1234 surface->geometry.y = output->y + (mode->height - surface->height) / 2;
1235 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001236}
1237
1238static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001239map(struct weston_shell *base,
1240 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001241{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001242 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001243 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001244 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001245 struct shell_surface *shsurf;
1246 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1247 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001248
Pekka Paalanen77346a62011-11-30 16:26:35 +02001249 shsurf = get_shell_surface(surface);
1250 if (shsurf)
1251 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001252
Pekka Paalanen77346a62011-11-30 16:26:35 +02001253 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001254 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001255 do_configure = 0;
1256 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001257 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001258 do_configure = 1;
1259 }
1260
1261 surface->width = width;
1262 surface->height = height;
1263
1264 /* initial positioning, see also configure() */
1265 switch (surface_type) {
1266 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001267 surface->geometry.x = 10 + random() % 400;
1268 surface->geometry.y = 10 + random() % 400;
1269 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001270 break;
1271 case SHELL_SURFACE_SCREENSAVER:
1272 case SHELL_SURFACE_FULLSCREEN:
1273 center_on_output(surface, surface->fullscreen_output);
1274 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001275 case SHELL_SURFACE_LOCK:
1276 center_on_output(surface, get_default_output(compositor));
1277 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001278 default:
1279 ;
1280 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001281
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001282 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001283 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001284 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001285 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001286 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001287 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001288 break;
1289 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001290 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001291 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001292 break;
1293 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001294 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001295 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001296
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001297 weston_compositor_repick(compositor);
1298 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001299 do_configure = 1;
1300 break;
1301 case SHELL_SURFACE_SCREENSAVER:
1302 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001303 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001304 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001305 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001306 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001307 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001308 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001309 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001310 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001311 break;
1312 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001313 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001314 if (!wl_list_empty(&shell->panels)) {
1315 struct shell_surface *panel =
1316 container_of(shell->panels.prev,
1317 struct shell_surface, link);
1318 wl_list_insert(&panel->surface->link, &surface->link);
1319 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001320 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001321 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001322 }
1323
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001324 switch (surface_type) {
1325 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001326 surface->geometry.x = 10 + random() % 400;
1327 surface->geometry.y = 10 + random() % 400;
1328 surface->geometry.dirty = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001329 break;
1330 case SHELL_SURFACE_POPUP:
1331 shell_map_popup(shsurf, shsurf->popup.time);
1332 break;
1333 default:
1334 break;
1335 }
1336
1337 surface->width = width;
1338 surface->height = height;
1339 if (do_configure) {
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001340 weston_surface_configure(surface, surface->geometry.x,
1341 surface->geometry.y,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001342 width, height);
1343 weston_compositor_repick(compositor);
1344 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001345
Juan Zhao7bb92f02011-12-15 11:31:51 -05001346 switch (surface_type) {
1347 case SHELL_SURFACE_TOPLEVEL:
1348 case SHELL_SURFACE_TRANSIENT:
1349 case SHELL_SURFACE_FULLSCREEN:
1350 if (!shell->locked)
1351 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001352 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001353 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001354 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001355 break;
1356 default:
1357 break;
1358 }
1359
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001360 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001361 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001362}
1363
1364static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001365configure(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001366 int32_t x, int32_t y, int32_t width, int32_t height)
1367{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001368 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1369 int do_configure = !shell->locked;
1370 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1371 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001372
Pekka Paalanen77346a62011-11-30 16:26:35 +02001373 shsurf = get_shell_surface(surface);
1374 if (shsurf)
1375 surface_type = shsurf->type;
1376
1377 surface->width = width;
1378 surface->height = height;
1379
1380 switch (surface_type) {
1381 case SHELL_SURFACE_SCREENSAVER:
1382 do_configure = !do_configure;
1383 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001384 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001385 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001386 break;
1387 default:
1388 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001389 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001390
Pekka Paalanen77346a62011-11-30 16:26:35 +02001391 /*
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001392 * weston_surface_configure() will assign an output, which means
Pekka Paalanen77346a62011-11-30 16:26:35 +02001393 * the surface is supposed to be in compositor->surface_list.
1394 * Be careful with that, and make sure we stay on the right output.
1395 * XXX: would a fullscreen surface need the same handling?
1396 */
1397 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001398 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001399
1400 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1401 surface->output = shsurf->output;
1402 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001403}
1404
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001405static int launch_desktop_shell_process(struct wl_shell *shell);
1406
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001407static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001409{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001410 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001411 struct wl_shell *shell =
1412 container_of(process, struct wl_shell, child.process);
1413
1414 shell->child.process.pid = 0;
1415 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001416
1417 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1418 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001419 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001420 shell->child.deathstamp = time;
1421 shell->child.deathcount = 0;
1422 }
1423
1424 shell->child.deathcount++;
1425 if (shell->child.deathcount > 5) {
1426 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1427 return;
1428 }
1429
1430 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1431 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001432}
1433
1434static int
1435launch_desktop_shell_process(struct wl_shell *shell)
1436{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001437 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001438
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001439 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001440 &shell->child.process,
1441 shell_exe,
1442 desktop_shell_sigchld);
1443
1444 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001445 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001446 return 0;
1447}
1448
1449static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001450bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1451{
1452 struct wl_shell *shell = data;
1453
1454 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001455 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001456}
1457
Kristian Høgsberg75840622011-09-06 13:48:16 -04001458static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001459unbind_desktop_shell(struct wl_resource *resource)
1460{
1461 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001462
1463 if (shell->locked)
1464 resume_desktop(shell);
1465
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001466 shell->child.desktop_shell = NULL;
1467 shell->prepare_event_sent = false;
1468 free(resource);
1469}
1470
1471static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001472bind_desktop_shell(struct wl_client *client,
1473 void *data, uint32_t version, uint32_t id)
1474{
1475 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001476 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001477
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001478 resource = wl_client_add_object(client, &desktop_shell_interface,
1479 &desktop_shell_implementation,
1480 id, shell);
1481
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001482 if (client == shell->child.client) {
1483 resource->destroy = unbind_desktop_shell;
1484 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001485 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001486 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001487
1488 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1489 "permission to bind desktop_shell denied");
1490 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001491}
1492
Pekka Paalanen6e168112011-11-24 11:34:05 +02001493static void
1494screensaver_set_surface(struct wl_client *client,
1495 struct wl_resource *resource,
1496 struct wl_resource *shell_surface_resource,
1497 struct wl_resource *output_resource)
1498{
1499 struct wl_shell *shell = resource->data;
1500 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001502
Pekka Paalanen98262232011-12-01 10:42:22 +02001503 if (reset_shell_surface_type(surface))
1504 return;
1505
Pekka Paalanen77346a62011-11-30 16:26:35 +02001506 surface->type = SHELL_SURFACE_SCREENSAVER;
1507
1508 surface->surface->fullscreen_output = output;
1509 surface->output = output;
1510 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001511}
1512
1513static const struct screensaver_interface screensaver_implementation = {
1514 screensaver_set_surface
1515};
1516
1517static void
1518unbind_screensaver(struct wl_resource *resource)
1519{
1520 struct wl_shell *shell = resource->data;
1521
Pekka Paalanen77346a62011-11-30 16:26:35 +02001522 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001523 free(resource);
1524}
1525
1526static void
1527bind_screensaver(struct wl_client *client,
1528 void *data, uint32_t version, uint32_t id)
1529{
1530 struct wl_shell *shell = data;
1531 struct wl_resource *resource;
1532
1533 resource = wl_client_add_object(client, &screensaver_interface,
1534 &screensaver_implementation,
1535 id, shell);
1536
Pekka Paalanen77346a62011-11-30 16:26:35 +02001537 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001538 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001539 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001540 return;
1541 }
1542
1543 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1544 "interface object already bound");
1545 wl_resource_destroy(resource, 0);
1546}
1547
Pekka Paalanen3c647232011-12-22 13:43:43 +02001548static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001549shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001550{
1551 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1552
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001553 if (shell->child.client)
1554 wl_client_destroy(shell->child.client);
1555
Pekka Paalanen3c647232011-12-22 13:43:43 +02001556 free(shell->screensaver.path);
1557 free(shell);
1558}
1559
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001560int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001561shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001562
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001563WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001565{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001566 struct wl_shell *shell;
1567
1568 shell = malloc(sizeof *shell);
1569 if (shell == NULL)
1570 return -1;
1571
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001572 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001573 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001574 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001575 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001576 shell->shell.map = map;
1577 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001578 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001579
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001580 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001581 wl_list_init(&shell->backgrounds);
1582 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001583 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001584
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001585 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001586
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001587 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1588 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001589 return -1;
1590
Kristian Høgsberg75840622011-09-06 13:48:16 -04001591 if (wl_display_add_global(ec->wl_display,
1592 &desktop_shell_interface,
1593 shell, bind_desktop_shell) == NULL)
1594 return -1;
1595
Pekka Paalanen6e168112011-11-24 11:34:05 +02001596 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1597 shell, bind_screensaver) == NULL)
1598 return -1;
1599
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001600 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001601 if (launch_desktop_shell_process(shell) != 0)
1602 return -1;
1603
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001604 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001605 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001606 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001607 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001608 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001609 MODIFIER_CTRL | MODIFIER_ALT,
1610 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001611 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001612 click_to_activate_binding, ec);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001613 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1614 MODIFIER_SUPER | MODIFIER_ALT,
1615 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001616
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001617 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001618
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001619 return 0;
1620}