blob: 38a2600f69397a404d69ca6a187c65050e3b93a5 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
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;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -050099 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200100 } rotation;
101
102 struct {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500103 struct wl_grab grab;
104 uint32_t time;
105 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200106 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500107 int32_t initial_up;
108 } popup;
109
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500110 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100111 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200112};
113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500115 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500117 int32_t dx, dy;
118};
119
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120struct rotate_grab {
121 struct wl_grab grab;
122 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500123 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200124 struct {
125 int32_t x;
126 int32_t y;
127 } center;
128};
129
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500130static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200131shell_configuration(struct wl_shell *shell)
132{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200133 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200134 char *path = NULL;
135 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200136
137 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200138 { "path", CONFIG_KEY_STRING, &path },
139 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200140 };
141
142 struct config_section cs[] = {
143 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
144 };
145
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500146 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500147 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200148 free(config_file);
149
Pekka Paalanen7296e792011-12-07 16:22:00 +0200150 shell->screensaver.path = path;
151 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200152}
153
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200154static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500155noop_grab_focus(struct wl_grab *grab, uint32_t time,
156 struct wl_surface *surface, int32_t x, int32_t y)
157{
158 grab->focus = NULL;
159}
160
161static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500162move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500163 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500164{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500165 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500166 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500168
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500169 weston_surface_configure(es,
170 device->x + move->dx,
171 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200172 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500173}
174
175static void
176move_grab_button(struct wl_grab *grab,
177 uint32_t time, int32_t button, int32_t state)
178{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500179 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500180
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500181 if (device->button_count == 0 && state == 0) {
182 wl_input_device_end_grab(device, time);
183 free(grab);
184 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500185}
186
187static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500188 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189 move_grab_motion,
190 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500191};
192
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400193static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500195 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500196{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198
199 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400200 if (!move)
201 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202
203 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200204 move->dx = es->geometry.x - wd->input_device.grab_x;
205 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500206 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500208 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400210 wl_input_device_set_pointer_focus(&wd->input_device,
211 NULL, time, 0, 0, 0, 0);
212
213 return 0;
214}
215
216static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200217shell_surface_move(struct wl_client *client, struct wl_resource *resource,
218 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400219{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500220 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200221 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400222
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500223 if (wd->input_device.button_count == 0 ||
224 wd->input_device.grab_time != time ||
225 wd->input_device.pointer_focus != &shsurf->surface->surface)
226 return;
227
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400229 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230}
231
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500232struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500233 struct wl_grab grab;
234 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200235 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200236 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500237};
238
239static void
240resize_grab_motion(struct wl_grab *grab,
241 uint32_t time, int32_t x, int32_t y)
242{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500243 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200246 int32_t from_x, from_y;
247 int32_t to_x, to_y;
248
249 weston_surface_from_global(resize->shsurf->surface,
250 device->grab_x, device->grab_y,
251 &from_x, &from_y);
252 weston_surface_from_global(resize->shsurf->surface,
253 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200255 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200256 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200257 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200258 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259 } else {
260 width = resize->width;
261 }
262
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200263 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200264 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200265 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200266 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500267 } else {
268 height = resize->height;
269 }
270
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200271 wl_resource_post_event(&resize->shsurf->resource,
272 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
273 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500274}
275
276static void
277resize_grab_button(struct wl_grab *grab,
278 uint32_t time, int32_t button, int32_t state)
279{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500280 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500281
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500282 if (device->button_count == 0 && state == 0) {
283 wl_input_device_end_grab(device, time);
284 free(grab);
285 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500286}
287
288static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500289 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500290 resize_grab_motion,
291 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500292};
293
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400294static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500295weston_surface_resize(struct shell_surface *shsurf,
296 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200297 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500298{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500299 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500300
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500301 /* FIXME: Reject if fullscreen */
302
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200303 if (edges == 0 || edges > 15 ||
304 (edges & 3) == 3 || (edges & 12) == 12)
305 return 0;
306
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500307 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400308 if (!resize)
309 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310
311 resize->grab.interface = &resize_grab_interface;
312 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200313 resize->width = shsurf->surface->geometry.width;
314 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200315 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400316
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500317 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400319 wl_input_device_set_pointer_focus(&wd->input_device,
320 NULL, time, 0, 0, 0, 0);
321
322 return 0;
323}
324
325static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200326shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
327 struct wl_resource *input_resource, uint32_t time,
328 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400329{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500330 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200331 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400332
333 /* FIXME: Reject if fullscreen */
334
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500335 if (wd->input_device.button_count == 0 ||
336 wd->input_device.grab_time != time ||
337 wd->input_device.pointer_focus != &shsurf->surface->surface)
338 return;
339
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500340 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400341 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342}
343
Pekka Paalanen98262232011-12-01 10:42:22 +0200344static int
345reset_shell_surface_type(struct shell_surface *surface)
346{
347 switch (surface->type) {
348 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200349 surface->surface->geometry.x = surface->saved_x;
350 surface->surface->geometry.y = surface->saved_y;
351 surface->surface->geometry.dirty = 1;
Pekka Paalanen98262232011-12-01 10:42:22 +0200352 surface->surface->fullscreen_output = NULL;
353 break;
354 case SHELL_SURFACE_PANEL:
355 case SHELL_SURFACE_BACKGROUND:
356 wl_list_remove(&surface->link);
357 wl_list_init(&surface->link);
358 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200359 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200360 case SHELL_SURFACE_LOCK:
361 wl_resource_post_error(&surface->resource,
362 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200363 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200364 return -1;
365 case SHELL_SURFACE_NONE:
366 case SHELL_SURFACE_TOPLEVEL:
367 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500368 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200369 break;
370 }
371
372 surface->type = SHELL_SURFACE_NONE;
373 return 0;
374}
375
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500376static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200377shell_surface_set_toplevel(struct wl_client *client,
378 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400379
380{
Pekka Paalanen98262232011-12-01 10:42:22 +0200381 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400382
Pekka Paalanen98262232011-12-01 10:42:22 +0200383 if (reset_shell_surface_type(surface))
384 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400385
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500386 weston_surface_damage(surface->surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200387 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400388}
389
390static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200391shell_surface_set_transient(struct wl_client *client,
392 struct wl_resource *resource,
393 struct wl_resource *parent_resource,
394 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400395{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200396 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500397 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200398 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500399 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400400
Pekka Paalanen98262232011-12-01 10:42:22 +0200401 if (reset_shell_surface_type(shsurf))
402 return;
403
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400404 /* assign to parents output */
405 es->output = pes->output;
406
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200407 es->geometry.x = pes->geometry.x + x;
408 es->geometry.y = pes->geometry.y + y;
409 es->geometry.dirty = 1;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400410
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200412 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400413}
414
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500415static struct weston_output *
416get_default_output(struct weston_compositor *compositor)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200417{
418 return container_of(compositor->output_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500419 struct weston_output, link);
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200420}
421
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400422static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200423shell_surface_set_fullscreen(struct wl_client *client,
424 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400425
426{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200427 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500428 struct weston_surface *es = shsurf->surface;
429 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400430
Pekka Paalanen98262232011-12-01 10:42:22 +0200431 if (reset_shell_surface_type(shsurf))
432 return;
433
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400434 /* FIXME: Fullscreen on first output */
435 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200436 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400437 es->output = output;
438
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200439 shsurf->saved_x = es->geometry.x;
440 shsurf->saved_y = es->geometry.y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200441 es->geometry.x = (output->current->width - es->geometry.width) / 2;
442 es->geometry.y = (output->current->height - es->geometry.height) / 2;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200443 es->geometry.dirty = 1;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400444 es->fullscreen_output = output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500445 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200446 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400447}
448
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500449static void
450popup_grab_focus(struct wl_grab *grab, uint32_t time,
451 struct wl_surface *surface, int32_t x, int32_t y)
452{
453 struct wl_input_device *device = grab->input_device;
454 struct shell_surface *priv =
455 container_of(grab, struct shell_surface, popup.grab);
456 struct wl_client *client = priv->surface->surface.resource.client;
457
Pekka Paalanencb108432012-01-19 16:25:40 +0200458 if (surface && surface->resource.client == client) {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500459 wl_input_device_set_pointer_focus(device, surface, time,
460 device->x, device->y, x, y);
461 grab->focus = surface;
462 } else {
463 wl_input_device_set_pointer_focus(device, NULL,
464 time, 0, 0, 0, 0);
465 grab->focus = NULL;
466 }
467}
468
469static void
470popup_grab_motion(struct wl_grab *grab,
471 uint32_t time, int32_t x, int32_t y)
472{
473 struct wl_input_device *device = grab->input_device;
474 struct wl_resource *resource;
475
476 resource = grab->input_device->pointer_focus_resource;
477 if (resource)
478 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
479 time, device->x, device->y, x, y);
480}
481
482static void
483popup_grab_button(struct wl_grab *grab,
484 uint32_t time, int32_t button, int32_t state)
485{
486 struct wl_resource *resource;
487 struct shell_surface *shsurf =
488 container_of(grab, struct shell_surface, popup.grab);
489
490 resource = grab->input_device->pointer_focus_resource;
491 if (resource) {
492 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
493 time, button, state);
494 } else if (state == 0 &&
495 (shsurf->popup.initial_up ||
496 time - shsurf->popup.time > 500)) {
497 wl_resource_post_event(&shsurf->resource,
498 WL_SHELL_SURFACE_POPUP_DONE);
499 wl_input_device_end_grab(grab->input_device, time);
500 shsurf->popup.grab.input_device = NULL;
501 }
502
503 if (state == 0)
504 shsurf->popup.initial_up = 1;
505}
506
507static const struct wl_grab_interface popup_grab_interface = {
508 popup_grab_focus,
509 popup_grab_motion,
510 popup_grab_button,
511};
512
513static void
514shell_map_popup(struct shell_surface *shsurf, uint32_t time)
515{
516 struct wl_input_device *device;
517 struct weston_surface *es = shsurf->surface;
518 struct weston_surface *parent = shsurf->parent->surface;
519
520 es->output = parent->output;
521
522 shsurf->popup.grab.interface = &popup_grab_interface;
523 device = es->compositor->input_device;
524
Pekka Paalanen938269a2012-02-07 14:19:01 +0200525 weston_surface_update_transform(parent);
526 if (parent->transform.enabled) {
527 shsurf->popup.parent_transform.matrix =
528 parent->transform.matrix;
529 } else {
530 /* construct x, y translation matrix */
531 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
532 shsurf->popup.parent_transform.matrix.d[12] =
533 parent->geometry.x;
534 shsurf->popup.parent_transform.matrix.d[13] =
535 parent->geometry.y;
536 }
537 wl_list_insert(es->geometry.transformation_list.prev,
538 &shsurf->popup.parent_transform.link);
539 es->geometry.x = shsurf->popup.x;
540 es->geometry.y = shsurf->popup.y;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200541 es->geometry.dirty = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500542
543 shsurf->popup.grab.input_device = device;
544 shsurf->popup.time = device->grab_time;
545 shsurf->popup.initial_up = 0;
546
547 wl_input_device_start_grab(shsurf->popup.grab.input_device,
548 &shsurf->popup.grab, shsurf->popup.time);
549}
550
551static void
552shell_surface_set_popup(struct wl_client *client,
553 struct wl_resource *resource,
554 struct wl_resource *input_device_resource,
555 uint32_t time,
556 struct wl_resource *parent_resource,
557 int32_t x, int32_t y, uint32_t flags)
558{
559 struct shell_surface *shsurf = resource->data;
560 struct weston_surface *es = shsurf->surface;
561
562 weston_surface_damage(es);
563 shsurf->type = SHELL_SURFACE_POPUP;
564 shsurf->parent = parent_resource->data;
565 shsurf->popup.x = x;
566 shsurf->popup.y = y;
567}
568
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200569static const struct wl_shell_surface_interface shell_surface_implementation = {
570 shell_surface_move,
571 shell_surface_resize,
572 shell_surface_set_toplevel,
573 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500574 shell_surface_set_fullscreen,
575 shell_surface_set_popup
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200576};
577
578static void
579destroy_shell_surface(struct wl_resource *resource)
580{
581 struct shell_surface *shsurf = resource->data;
582
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500583 if (shsurf->popup.grab.input_device)
584 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
585
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200586 /* in case cleaning up a dead client destroys shell_surface first */
587 if (shsurf->surface)
588 wl_list_remove(&shsurf->surface_destroy_listener.link);
589
590 wl_list_remove(&shsurf->link);
591 free(shsurf);
592}
593
594static void
595shell_handle_surface_destroy(struct wl_listener *listener,
596 struct wl_resource *resource, uint32_t time)
597{
598 struct shell_surface *shsurf = container_of(listener,
599 struct shell_surface,
600 surface_destroy_listener);
601
602 shsurf->surface = NULL;
603 wl_resource_destroy(&shsurf->resource, time);
604}
605
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200606static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500607get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200608{
609 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
610 struct wl_listener *listener;
611
612 /* search the destroy listener list for our callback */
613 wl_list_for_each(listener, lst, link) {
614 if (listener->func == shell_handle_surface_destroy) {
615 return container_of(listener, struct shell_surface,
616 surface_destroy_listener);
617 }
618 }
619
620 return NULL;
621}
622
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200623static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200624shell_get_shell_surface(struct wl_client *client,
625 struct wl_resource *resource,
626 uint32_t id,
627 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200628{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500629 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200630 struct shell_surface *shsurf;
631
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200632 if (get_shell_surface(surface)) {
633 wl_resource_post_error(surface_resource,
634 WL_DISPLAY_ERROR_INVALID_OBJECT,
635 "wl_shell::get_shell_surface already requested");
636 return;
637 }
638
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200639 shsurf = calloc(1, sizeof *shsurf);
640 if (!shsurf) {
641 wl_resource_post_no_memory(resource);
642 return;
643 }
644
645 shsurf->resource.destroy = destroy_shell_surface;
646 shsurf->resource.object.id = id;
647 shsurf->resource.object.interface = &wl_shell_surface_interface;
648 shsurf->resource.object.implementation =
649 (void (**)(void)) &shell_surface_implementation;
650 shsurf->resource.data = shsurf;
651
652 shsurf->surface = surface;
653 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
654 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
655 &shsurf->surface_destroy_listener.link);
656
657 /* init link so its safe to always remove it in destroy_shell_surface */
658 wl_list_init(&shsurf->link);
659
Pekka Paalanen460099f2012-01-20 16:48:25 +0200660 /* empty when not in use */
661 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500662 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200663
Pekka Paalanen98262232011-12-01 10:42:22 +0200664 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200665
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200666 wl_client_add_resource(client, &shsurf->resource);
667}
668
669static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200670 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500671};
672
Kristian Høgsberg07937562011-04-12 17:25:42 -0400673static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500674handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200675{
676 proc->pid = 0;
677}
678
679static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200680launch_screensaver(struct wl_shell *shell)
681{
682 if (shell->screensaver.binding)
683 return;
684
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200685 if (!shell->screensaver.path)
686 return;
687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500688 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200689 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200690 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200691 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200692}
693
694static void
695terminate_screensaver(struct wl_shell *shell)
696{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200697 if (shell->screensaver.process.pid == 0)
698 return;
699
700 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200701}
702
703static void
704show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
705{
706 struct wl_list *list;
707
708 if (shell->lock_surface)
709 list = &shell->lock_surface->surface->link;
710 else
711 list = &shell->compositor->surface_list;
712
713 wl_list_remove(&surface->surface->link);
714 wl_list_insert(list, &surface->surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500715 weston_surface_configure(surface->surface,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200716 surface->surface->geometry.x,
717 surface->surface->geometry.y,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200718 surface->surface->geometry.width,
719 surface->surface->geometry.height);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200720 surface->surface->output = surface->output;
721}
722
723static void
724hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
725{
726 wl_list_remove(&surface->surface->link);
727 wl_list_init(&surface->surface->link);
728 surface->surface->output = NULL;
729}
730
731static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400732desktop_shell_set_background(struct wl_client *client,
733 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100734 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400735 struct wl_resource *surface_resource)
736{
737 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200738 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500739 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200740 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400741
Pekka Paalanen98262232011-12-01 10:42:22 +0200742 if (reset_shell_surface_type(shsurf))
743 return;
744
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100745 wl_list_for_each(priv, &shell->backgrounds, link) {
746 if (priv->output == output_resource->data) {
747 priv->surface->output = NULL;
748 wl_list_remove(&priv->surface->link);
749 wl_list_remove(&priv->link);
750 break;
751 }
752 }
753
Pekka Paalanen068ae942011-11-28 14:11:15 +0200754 shsurf->type = SHELL_SURFACE_BACKGROUND;
755 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100756
Pekka Paalanen068ae942011-11-28 14:11:15 +0200757 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100758
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200759 surface->geometry.x = shsurf->output->x;
760 surface->geometry.y = shsurf->output->y;
761 surface->geometry.dirty = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200762
Kristian Høgsberg75840622011-09-06 13:48:16 -0400763 wl_resource_post_event(resource,
764 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500765 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200766 shsurf->output->current->width,
767 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400768}
769
770static void
771desktop_shell_set_panel(struct wl_client *client,
772 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100773 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400774 struct wl_resource *surface_resource)
775{
776 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200777 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500778 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200779 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400780
Pekka Paalanen98262232011-12-01 10:42:22 +0200781 if (reset_shell_surface_type(shsurf))
782 return;
783
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100784 wl_list_for_each(priv, &shell->panels, link) {
785 if (priv->output == output_resource->data) {
786 priv->surface->output = NULL;
787 wl_list_remove(&priv->surface->link);
788 wl_list_remove(&priv->link);
789 break;
790 }
791 }
792
Pekka Paalanen068ae942011-11-28 14:11:15 +0200793 shsurf->type = SHELL_SURFACE_PANEL;
794 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100795
Pekka Paalanen068ae942011-11-28 14:11:15 +0200796 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100797
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200798 surface->geometry.x = shsurf->output->x;
799 surface->geometry.y = shsurf->output->y;
800 surface->geometry.dirty = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200801
Kristian Høgsberg75840622011-09-06 13:48:16 -0400802 wl_resource_post_event(resource,
803 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500804 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200805 shsurf->output->current->width,
806 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400807}
808
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200809static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500810handle_lock_surface_destroy(struct wl_listener *listener,
811 struct wl_resource *resource, uint32_t time)
812{
813 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200814 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500815
816 fprintf(stderr, "lock surface gone\n");
817 shell->lock_surface = NULL;
818}
819
820static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200821desktop_shell_set_lock_surface(struct wl_client *client,
822 struct wl_resource *resource,
823 struct wl_resource *surface_resource)
824{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200825 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200826 struct shell_surface *surface = surface_resource->data;
827
828 if (reset_shell_surface_type(surface))
829 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200830
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200831 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200832
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200833 if (!shell->locked)
834 return;
835
Pekka Paalanen98262232011-12-01 10:42:22 +0200836 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200837
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500838 shell->lock_surface_listener.func = handle_lock_surface_destroy;
839 wl_list_insert(&surface_resource->destroy_listener_list,
840 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200841
Pekka Paalanen068ae942011-11-28 14:11:15 +0200842 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200843}
844
845static void
846resume_desktop(struct wl_shell *shell)
847{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500848 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200849 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200850 struct shell_surface *tmp;
851
852 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
853 hide_screensaver(shell, tmp);
854
855 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200856
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200857 wl_list_for_each(surface, &shell->hidden_surface_list, link) {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200858 weston_surface_configure(surface, surface->geometry.x,
859 surface->geometry.y,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200860 surface->geometry.width,
861 surface->geometry.height);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200862 weston_surface_assign_output(surface);
863 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200864
Pekka Paalanenfe340832011-11-25 16:07:52 +0200865 if (wl_list_empty(&shell->backgrounds)) {
866 list = &shell->compositor->surface_list;
867 } else {
868 struct shell_surface *background;
869 background = container_of(shell->backgrounds.prev,
870 struct shell_surface, link);
871 list = background->surface->link.prev;
872 }
873
874 if (!wl_list_empty(&shell->hidden_surface_list))
875 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500876 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200877
878 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500879 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200880 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500881 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200882}
883
884static void
885desktop_shell_unlock(struct wl_client *client,
886 struct wl_resource *resource)
887{
888 struct wl_shell *shell = resource->data;
889
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200890 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200891
892 if (shell->locked)
893 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200894}
895
Kristian Høgsberg75840622011-09-06 13:48:16 -0400896static const struct desktop_shell_interface desktop_shell_implementation = {
897 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200898 desktop_shell_set_panel,
899 desktop_shell_set_lock_surface,
900 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400901};
902
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200903static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500904get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200905{
906 struct shell_surface *shsurf;
907
908 shsurf = get_shell_surface(surface);
909 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200910 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200911 return shsurf->type;
912}
913
Kristian Høgsberg75840622011-09-06 13:48:16 -0400914static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400915move_binding(struct wl_input_device *device, uint32_t time,
916 uint32_t key, uint32_t button, uint32_t state, void *data)
917{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500918 struct weston_surface *surface =
919 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500920
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100921 if (surface == NULL)
922 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400923
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200924 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100925 case SHELL_SURFACE_PANEL:
926 case SHELL_SURFACE_BACKGROUND:
927 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200928 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100929 return;
930 default:
931 break;
932 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400933
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400935}
936
937static void
938resize_binding(struct wl_input_device *device, uint32_t time,
939 uint32_t key, uint32_t button, uint32_t state, void *data)
940{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500941 struct weston_surface *surface =
942 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400943 uint32_t edges = 0;
944 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200945 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500946
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100947 if (surface == NULL)
948 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200949
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200950 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200951 if (!shsurf)
952 return;
953
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200954 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100955 case SHELL_SURFACE_PANEL:
956 case SHELL_SURFACE_BACKGROUND:
957 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200958 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100959 return;
960 default:
961 break;
962 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400963
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200964 weston_surface_from_global(surface,
965 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400966
Pekka Paalanen60921e52012-01-25 15:55:43 +0200967 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200968 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200969 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -0400970 edges |= 0;
971 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200972 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400973
Pekka Paalanen60921e52012-01-25 15:55:43 +0200974 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200975 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200976 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -0400977 edges |= 0;
978 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200979 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400980
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500981 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200982 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400983}
984
985static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500986terminate_binding(struct wl_input_device *device, uint32_t time,
987 uint32_t key, uint32_t button, uint32_t state, void *data)
988{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500989 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500990
991 if (state)
992 wl_display_terminate(compositor->wl_display);
993}
994
995static void
Pekka Paalanen460099f2012-01-20 16:48:25 +0200996rotate_grab_motion(struct wl_grab *grab,
997 uint32_t time, int32_t x, int32_t y)
998{
999 struct rotate_grab *rotate =
1000 container_of(grab, struct rotate_grab, grab);
1001 struct wl_input_device *device = grab->input_device;
1002 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001003 GLfloat cx = 0.5f * surface->surface->geometry.width;
1004 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001005 GLfloat dx, dy;
1006 GLfloat r;
1007
1008 dx = device->x - rotate->center.x;
1009 dy = device->y - rotate->center.y;
1010 r = sqrtf(dx * dx + dy * dy);
1011
1012 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001013 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001014
1015 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001016 struct weston_matrix *matrix =
1017 &surface->rotation.transform.matrix;
1018
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001019 weston_matrix_init(&rotate->rotation);
1020 rotate->rotation.d[0] = dx / r;
1021 rotate->rotation.d[4] = -dy / r;
1022 rotate->rotation.d[1] = -rotate->rotation.d[4];
1023 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001024
1025 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001026 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001027 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1028 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001029 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001030
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001031 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001032 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001033 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001034 } else {
1035 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001036 weston_matrix_init(&surface->rotation.rotation);
1037 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001038 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001039
1040 /* Repaint implies weston_surface_update_transform(), which
1041 * lazily applies the damage due to rotation update.
1042 */
1043 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001044}
1045
1046static void
1047rotate_grab_button(struct wl_grab *grab,
1048 uint32_t time, int32_t button, int32_t state)
1049{
1050 struct rotate_grab *rotate =
1051 container_of(grab, struct rotate_grab, grab);
1052 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001053 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001054
1055 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001056 weston_matrix_multiply(&surface->rotation.rotation,
1057 &rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001058 wl_input_device_end_grab(device, time);
1059 free(rotate);
1060 }
1061}
1062
1063static const struct wl_grab_interface rotate_grab_interface = {
1064 noop_grab_focus,
1065 rotate_grab_motion,
1066 rotate_grab_button,
1067};
1068
1069static void
1070rotate_binding(struct wl_input_device *device, uint32_t time,
1071 uint32_t key, uint32_t button, uint32_t state, void *data)
1072{
1073 struct weston_surface *base_surface =
1074 (struct weston_surface *) device->pointer_focus;
1075 struct shell_surface *surface;
1076 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001077 GLfloat dx, dy;
1078 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001079
1080 if (base_surface == NULL)
1081 return;
1082
1083 surface = get_shell_surface(base_surface);
1084 if (!surface)
1085 return;
1086
1087 switch (surface->type) {
1088 case SHELL_SURFACE_PANEL:
1089 case SHELL_SURFACE_BACKGROUND:
1090 case SHELL_SURFACE_FULLSCREEN:
1091 case SHELL_SURFACE_SCREENSAVER:
1092 return;
1093 default:
1094 break;
1095 }
1096
Pekka Paalanen460099f2012-01-20 16:48:25 +02001097 rotate = malloc(sizeof *rotate);
1098 if (!rotate)
1099 return;
1100
1101 rotate->grab.interface = &rotate_grab_interface;
1102 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001103
1104 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001105 surface->surface->geometry.width / 2,
1106 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001107 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001108
1109 wl_input_device_start_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001110
1111 dx = device->x - rotate->center.x;
1112 dy = device->y - rotate->center.y;
1113 r = sqrtf(dx * dx + dy * dy);
1114 if (r > 20.0f) {
1115 struct weston_matrix inverse;
1116
1117 weston_matrix_init(&inverse);
1118 inverse.d[0] = dx / r;
1119 inverse.d[4] = dy / r;
1120 inverse.d[1] = -inverse.d[4];
1121 inverse.d[5] = inverse.d[0];
1122 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1123 } else {
1124 weston_matrix_init(&surface->rotation.rotation);
1125 weston_matrix_init(&rotate->rotation);
1126 }
1127
Pekka Paalanen460099f2012-01-20 16:48:25 +02001128 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
1129}
1130
1131static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001132activate(struct weston_shell *base, struct weston_surface *es,
1133 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001134{
1135 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001136 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001137 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001138
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001139 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001140
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001141 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001142 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001143
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001144 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001145 case SHELL_SURFACE_BACKGROUND:
1146 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001147 wl_list_remove(&es->link);
1148 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001149 break;
1150 case SHELL_SURFACE_PANEL:
1151 /* already put on top */
1152 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001153 case SHELL_SURFACE_SCREENSAVER:
1154 /* always below lock surface */
1155 if (shell->lock_surface) {
1156 wl_list_remove(&es->link);
1157 wl_list_insert(&shell->lock_surface->surface->link,
1158 &es->link);
1159 }
1160 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001161 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001162 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001163 list = weston_compositor_top(compositor);
1164
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001165 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001166 struct shell_surface *panel;
1167 wl_list_for_each(panel, &shell->panels, link) {
1168 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001169 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001170 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001171 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001172 }
1173}
1174
1175static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001176click_to_activate_binding(struct wl_input_device *device,
1177 uint32_t time, uint32_t key,
1178 uint32_t button, uint32_t state, void *data)
1179{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001180 struct weston_input_device *wd = (struct weston_input_device *) device;
1181 struct weston_compositor *compositor = data;
1182 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001183
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001184 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001185 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001186 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001187}
1188
1189static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001191{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001192 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001193 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001194 struct weston_surface *cur;
1195 struct weston_surface *tmp;
1196 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001197 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001198 uint32_t time;
1199
1200 if (shell->locked)
1201 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001202
1203 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001204
1205 /* Move all surfaces from compositor's list to our hidden list,
1206 * except the background. This way nothing else can show or
1207 * receive input events while we are locked. */
1208
1209 if (!wl_list_empty(&shell->hidden_surface_list)) {
1210 fprintf(stderr,
1211 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1212 __func__);
1213 }
1214
1215 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1216 /* skip input device sprites, cur->surface is uninitialised */
1217 if (cur->surface.resource.client == NULL)
1218 continue;
1219
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001220 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001221 continue;
1222
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001223 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001224 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001225 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001226 }
1227
Pekka Paalanen77346a62011-11-30 16:26:35 +02001228 launch_screensaver(shell);
1229
1230 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1231 show_screensaver(shell, shsurf);
1232
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001233 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001234 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235 weston_compositor_wake(shell->compositor);
1236 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001237 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001238
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001239 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001240 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001241
1242 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001243 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001244 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1245 wl_input_device_set_keyboard_focus(&device->input_device,
1246 NULL, time);
1247 }
1248
1249 /* TODO: disable bindings that should not work while locked. */
1250
1251 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001252}
1253
1254static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001255unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001256{
1257 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1258
Pekka Paalanend81c2162011-11-16 13:47:34 +02001259 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001260 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001261 return;
1262 }
1263
1264 /* If desktop-shell client has gone away, unlock immediately. */
1265 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001266 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001267 return;
1268 }
1269
1270 if (shell->prepare_event_sent)
1271 return;
1272
1273 wl_resource_post_event(shell->child.desktop_shell,
1274 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1275 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001276}
1277
1278static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001279center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001280{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001281 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001282
Pekka Paalanen60921e52012-01-25 15:55:43 +02001283 surface->geometry.x =
1284 output->x + (mode->width - surface->geometry.width) / 2;
1285 surface->geometry.y =
1286 output->y + (mode->height - surface->geometry.height) / 2;
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001287 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001288}
1289
1290static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001291map(struct weston_shell *base,
1292 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001293{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001294 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001295 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001296 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001297 struct shell_surface *shsurf;
1298 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1299 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001300
Pekka Paalanen77346a62011-11-30 16:26:35 +02001301 shsurf = get_shell_surface(surface);
1302 if (shsurf)
1303 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001304
Pekka Paalanen77346a62011-11-30 16:26:35 +02001305 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001306 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001307 do_configure = 0;
1308 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001309 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001310 do_configure = 1;
1311 }
1312
Pekka Paalanen60921e52012-01-25 15:55:43 +02001313 surface->geometry.width = width;
1314 surface->geometry.height = height;
1315 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001316
1317 /* initial positioning, see also configure() */
1318 switch (surface_type) {
1319 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001320 surface->geometry.x = 10 + random() % 400;
1321 surface->geometry.y = 10 + random() % 400;
1322 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001323 break;
1324 case SHELL_SURFACE_SCREENSAVER:
1325 case SHELL_SURFACE_FULLSCREEN:
1326 center_on_output(surface, surface->fullscreen_output);
1327 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001328 case SHELL_SURFACE_LOCK:
1329 center_on_output(surface, get_default_output(compositor));
1330 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001331 default:
1332 ;
1333 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001334
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001335 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001336 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001337 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001338 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001339 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001340 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001341 break;
1342 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001343 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001344 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001345 break;
1346 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001347 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001348 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001349
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001350 weston_compositor_repick(compositor);
1351 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001352 do_configure = 1;
1353 break;
1354 case SHELL_SURFACE_SCREENSAVER:
1355 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001356 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001357 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001358 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001360 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001361 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001362 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001363 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001364 break;
1365 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001366 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001367 if (!wl_list_empty(&shell->panels)) {
1368 struct shell_surface *panel =
1369 container_of(shell->panels.prev,
1370 struct shell_surface, link);
1371 wl_list_insert(&panel->surface->link, &surface->link);
1372 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001373 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001374 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001375 }
1376
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001377 switch (surface_type) {
1378 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001379 surface->geometry.x = 10 + random() % 400;
1380 surface->geometry.y = 10 + random() % 400;
1381 surface->geometry.dirty = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001382 break;
1383 case SHELL_SURFACE_POPUP:
1384 shell_map_popup(shsurf, shsurf->popup.time);
1385 break;
1386 default:
1387 break;
1388 }
1389
Pekka Paalanen60921e52012-01-25 15:55:43 +02001390 surface->geometry.width = width;
1391 surface->geometry.height = height;
1392 surface->geometry.dirty = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001393 if (do_configure) {
Pekka Paalanenba3cf952012-01-25 16:22:05 +02001394 weston_surface_configure(surface, surface->geometry.x,
1395 surface->geometry.y,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001396 width, height);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001397 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001398 weston_compositor_repick(compositor);
1399 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001400
Juan Zhao7bb92f02011-12-15 11:31:51 -05001401 switch (surface_type) {
1402 case SHELL_SURFACE_TOPLEVEL:
1403 case SHELL_SURFACE_TRANSIENT:
1404 case SHELL_SURFACE_FULLSCREEN:
1405 if (!shell->locked)
1406 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001408 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001409 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001410 break;
1411 default:
1412 break;
1413 }
1414
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001415 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001417}
1418
1419static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001421 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001422{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001423 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1424 int do_configure = !shell->locked;
1425 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1426 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001427
Pekka Paalanen77346a62011-11-30 16:26:35 +02001428 shsurf = get_shell_surface(surface);
1429 if (shsurf)
1430 surface_type = shsurf->type;
1431
Pekka Paalanen60921e52012-01-25 15:55:43 +02001432 surface->geometry.width = width;
1433 surface->geometry.height = height;
1434 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001435
1436 switch (surface_type) {
1437 case SHELL_SURFACE_SCREENSAVER:
1438 do_configure = !do_configure;
1439 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001440 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001441 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001442 break;
1443 default:
1444 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001445 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001446
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001447 /* XXX: would a fullscreen surface need the same handling? */
Pekka Paalanen77346a62011-11-30 16:26:35 +02001448 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001450 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001451
1452 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1453 surface->output = shsurf->output;
1454 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001455}
1456
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001457static int launch_desktop_shell_process(struct wl_shell *shell);
1458
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001459static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001460desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001461{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001462 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001463 struct wl_shell *shell =
1464 container_of(process, struct wl_shell, child.process);
1465
1466 shell->child.process.pid = 0;
1467 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001468
1469 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1470 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001471 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001472 shell->child.deathstamp = time;
1473 shell->child.deathcount = 0;
1474 }
1475
1476 shell->child.deathcount++;
1477 if (shell->child.deathcount > 5) {
1478 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1479 return;
1480 }
1481
1482 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1483 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001484}
1485
1486static int
1487launch_desktop_shell_process(struct wl_shell *shell)
1488{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001489 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001490
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001491 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001492 &shell->child.process,
1493 shell_exe,
1494 desktop_shell_sigchld);
1495
1496 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001497 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001498 return 0;
1499}
1500
1501static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001502bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1503{
1504 struct wl_shell *shell = data;
1505
1506 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001507 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001508}
1509
Kristian Høgsberg75840622011-09-06 13:48:16 -04001510static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001511unbind_desktop_shell(struct wl_resource *resource)
1512{
1513 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001514
1515 if (shell->locked)
1516 resume_desktop(shell);
1517
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001518 shell->child.desktop_shell = NULL;
1519 shell->prepare_event_sent = false;
1520 free(resource);
1521}
1522
1523static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001524bind_desktop_shell(struct wl_client *client,
1525 void *data, uint32_t version, uint32_t id)
1526{
1527 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001528 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001529
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001530 resource = wl_client_add_object(client, &desktop_shell_interface,
1531 &desktop_shell_implementation,
1532 id, shell);
1533
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001534 if (client == shell->child.client) {
1535 resource->destroy = unbind_desktop_shell;
1536 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001537 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001538 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001539
1540 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1541 "permission to bind desktop_shell denied");
1542 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001543}
1544
Pekka Paalanen6e168112011-11-24 11:34:05 +02001545static void
1546screensaver_set_surface(struct wl_client *client,
1547 struct wl_resource *resource,
1548 struct wl_resource *shell_surface_resource,
1549 struct wl_resource *output_resource)
1550{
1551 struct wl_shell *shell = resource->data;
1552 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001553 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001554
Pekka Paalanen98262232011-12-01 10:42:22 +02001555 if (reset_shell_surface_type(surface))
1556 return;
1557
Pekka Paalanen77346a62011-11-30 16:26:35 +02001558 surface->type = SHELL_SURFACE_SCREENSAVER;
1559
1560 surface->surface->fullscreen_output = output;
1561 surface->output = output;
1562 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001563}
1564
1565static const struct screensaver_interface screensaver_implementation = {
1566 screensaver_set_surface
1567};
1568
1569static void
1570unbind_screensaver(struct wl_resource *resource)
1571{
1572 struct wl_shell *shell = resource->data;
1573
Pekka Paalanen77346a62011-11-30 16:26:35 +02001574 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001575 free(resource);
1576}
1577
1578static void
1579bind_screensaver(struct wl_client *client,
1580 void *data, uint32_t version, uint32_t id)
1581{
1582 struct wl_shell *shell = data;
1583 struct wl_resource *resource;
1584
1585 resource = wl_client_add_object(client, &screensaver_interface,
1586 &screensaver_implementation,
1587 id, shell);
1588
Pekka Paalanen77346a62011-11-30 16:26:35 +02001589 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001590 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001591 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001592 return;
1593 }
1594
1595 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1596 "interface object already bound");
1597 wl_resource_destroy(resource, 0);
1598}
1599
Pekka Paalanen3c647232011-12-22 13:43:43 +02001600static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001601shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001602{
1603 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1604
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001605 if (shell->child.client)
1606 wl_client_destroy(shell->child.client);
1607
Pekka Paalanen3c647232011-12-22 13:43:43 +02001608 free(shell->screensaver.path);
1609 free(shell);
1610}
1611
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001612int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001613shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001614
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001615WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001616shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001617{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001618 struct wl_shell *shell;
1619
1620 shell = malloc(sizeof *shell);
1621 if (shell == NULL)
1622 return -1;
1623
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001624 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001625 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001626 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001627 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001628 shell->shell.map = map;
1629 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001630 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001631
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001632 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001633 wl_list_init(&shell->backgrounds);
1634 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001635 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001636
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001637 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001638
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001639 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1640 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001641 return -1;
1642
Kristian Høgsberg75840622011-09-06 13:48:16 -04001643 if (wl_display_add_global(ec->wl_display,
1644 &desktop_shell_interface,
1645 shell, bind_desktop_shell) == NULL)
1646 return -1;
1647
Pekka Paalanen6e168112011-11-24 11:34:05 +02001648 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1649 shell, bind_screensaver) == NULL)
1650 return -1;
1651
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001652 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001653 if (launch_desktop_shell_process(shell) != 0)
1654 return -1;
1655
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001656 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001657 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001658 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001659 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001660 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001661 MODIFIER_CTRL | MODIFIER_ALT,
1662 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001663 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001664 click_to_activate_binding, ec);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001665 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1666 MODIFIER_SUPER | MODIFIER_ALT,
1667 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001668
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001669 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001670
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001671 return 0;
1672}