blob: da6e6ac39520fbbfa4dfbadc239545f32a633dc1 [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,
Juan Zhao96879df2012-02-07 08:45:41 +080084 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050085 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086};
87
Pekka Paalanen56cdea92011-11-23 16:14:12 +020088struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020089 struct wl_resource resource;
90
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020092 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020094
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050095 enum shell_surface_type type;
96 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050098 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +020099 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500100 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200101 } rotation;
102
103 struct {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500104 struct wl_grab grab;
105 uint32_t time;
106 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200107 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500108 int32_t initial_up;
109 } popup;
110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100112 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200113};
114
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500116 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500117 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500118 int32_t dx, dy;
119};
120
Pekka Paalanen460099f2012-01-20 16:48:25 +0200121struct rotate_grab {
122 struct wl_grab grab;
123 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500124 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200125 struct {
126 int32_t x;
127 int32_t y;
128 } center;
129};
130
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500131static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200132shell_configuration(struct wl_shell *shell)
133{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200134 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200135 char *path = NULL;
136 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200137
138 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200139 { "path", CONFIG_KEY_STRING, &path },
140 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200141 };
142
143 struct config_section cs[] = {
144 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
145 };
146
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500147 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500148 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200149 free(config_file);
150
Pekka Paalanen7296e792011-12-07 16:22:00 +0200151 shell->screensaver.path = path;
152 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200153}
154
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200155static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500156noop_grab_focus(struct wl_grab *grab, uint32_t time,
157 struct wl_surface *surface, int32_t x, int32_t y)
158{
159 grab->focus = NULL;
160}
161
162static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500163move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500164 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500166 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500167 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500168 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500169
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500170 weston_surface_configure(es,
171 device->x + move->dx,
172 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200173 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174}
175
176static void
177move_grab_button(struct wl_grab *grab,
178 uint32_t time, int32_t button, int32_t state)
179{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500180 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500181
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500182 if (device->button_count == 0 && state == 0) {
183 wl_input_device_end_grab(device, time);
184 free(grab);
185 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186}
187
188static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500189 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500190 move_grab_motion,
191 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500192};
193
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400194static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500196 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500198 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199
200 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400201 if (!move)
202 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203
204 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200205 move->dx = es->geometry.x - wd->input_device.grab_x;
206 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500207 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500208
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500209 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400211 wl_input_device_set_pointer_focus(&wd->input_device,
212 NULL, time, 0, 0, 0, 0);
213
214 return 0;
215}
216
217static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200218shell_surface_move(struct wl_client *client, struct wl_resource *resource,
219 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200222 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400223
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500224 if (wd->input_device.button_count == 0 ||
225 wd->input_device.grab_time != time ||
226 wd->input_device.pointer_focus != &shsurf->surface->surface)
227 return;
228
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400230 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500231}
232
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234 struct wl_grab grab;
235 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200236 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200237 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500238};
239
240static void
241resize_grab_motion(struct wl_grab *grab,
242 uint32_t time, int32_t x, int32_t y)
243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500244 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500246 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200247 int32_t from_x, from_y;
248 int32_t to_x, to_y;
249
250 weston_surface_from_global(resize->shsurf->surface,
251 device->grab_x, device->grab_y,
252 &from_x, &from_y);
253 weston_surface_from_global(resize->shsurf->surface,
254 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500255
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200256 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200257 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200258 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200259 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260 } else {
261 width = resize->width;
262 }
263
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200264 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200265 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200266 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200267 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268 } else {
269 height = resize->height;
270 }
271
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200272 wl_resource_post_event(&resize->shsurf->resource,
273 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
274 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275}
276
277static void
278resize_grab_button(struct wl_grab *grab,
279 uint32_t time, int32_t button, int32_t state)
280{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500281 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500282
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500283 if (device->button_count == 0 && state == 0) {
284 wl_input_device_end_grab(device, time);
285 free(grab);
286 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287}
288
289static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500290 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500291 resize_grab_motion,
292 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500293};
294
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400295static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500296weston_surface_resize(struct shell_surface *shsurf,
297 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200298 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500299{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500301
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500302 /* FIXME: Reject if fullscreen */
303
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200304 if (edges == 0 || edges > 15 ||
305 (edges & 3) == 3 || (edges & 12) == 12)
306 return 0;
307
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400309 if (!resize)
310 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311
312 resize->grab.interface = &resize_grab_interface;
313 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200314 resize->width = shsurf->surface->geometry.width;
315 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200316 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400317
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500318 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500319
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400320 wl_input_device_set_pointer_focus(&wd->input_device,
321 NULL, time, 0, 0, 0, 0);
322
323 return 0;
324}
325
326static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200327shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
328 struct wl_resource *input_resource, uint32_t time,
329 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400330{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500331 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200332 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400333
334 /* FIXME: Reject if fullscreen */
335
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500336 if (wd->input_device.button_count == 0 ||
337 wd->input_device.grab_time != time ||
338 wd->input_device.pointer_focus != &shsurf->surface->surface)
339 return;
340
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500341 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400342 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500343}
344
Juan Zhao96879df2012-02-07 08:45:41 +0800345static struct weston_output *
346get_default_output(struct weston_compositor *compositor)
347{
348 return container_of(compositor->output_list.next,
349 struct weston_output, link);
350}
351
Pekka Paalanen98262232011-12-01 10:42:22 +0200352static int
353reset_shell_surface_type(struct shell_surface *surface)
354{
355 switch (surface->type) {
356 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200357 weston_surface_set_position(surface->surface,
358 surface->saved_x,
359 surface->saved_y);
Pekka Paalanen98262232011-12-01 10:42:22 +0200360 surface->surface->fullscreen_output = NULL;
361 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800362 case SHELL_SURFACE_MAXIMIZED:
363 surface->output = get_default_output(surface->surface->compositor);
364 weston_surface_set_position(surface->surface,
365 surface->saved_x,
366 surface->saved_y);
367 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200368 case SHELL_SURFACE_PANEL:
369 case SHELL_SURFACE_BACKGROUND:
370 wl_list_remove(&surface->link);
371 wl_list_init(&surface->link);
372 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200373 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200374 case SHELL_SURFACE_LOCK:
375 wl_resource_post_error(&surface->resource,
376 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200377 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200378 return -1;
379 case SHELL_SURFACE_NONE:
380 case SHELL_SURFACE_TOPLEVEL:
381 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500382 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200383 break;
384 }
385
386 surface->type = SHELL_SURFACE_NONE;
387 return 0;
388}
389
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500390static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200391shell_surface_set_toplevel(struct wl_client *client,
392 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400393
394{
Pekka Paalanen98262232011-12-01 10:42:22 +0200395 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400396
Pekka Paalanen98262232011-12-01 10:42:22 +0200397 if (reset_shell_surface_type(surface))
398 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400399
Pekka Paalanen98262232011-12-01 10:42:22 +0200400 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400401}
402
403static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200404shell_surface_set_transient(struct wl_client *client,
405 struct wl_resource *resource,
406 struct wl_resource *parent_resource,
407 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400408{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200409 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500410 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200411 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500412 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400413
Pekka Paalanen98262232011-12-01 10:42:22 +0200414 if (reset_shell_surface_type(shsurf))
415 return;
416
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400417 /* assign to parents output */
418 es->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200419 weston_surface_set_position(es, pes->geometry.x + x,
420 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400421
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200422 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400423}
424
Juan Zhao96879df2012-02-07 08:45:41 +0800425static struct wl_shell *
426shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200427{
Juan Zhao96879df2012-02-07 08:45:41 +0800428 struct weston_surface *es = shsurf->surface;
429 struct weston_shell *shell = es->compositor->shell;
430
431 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
432}
433
434static int
435get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
436{
437 struct shell_surface *priv;
438 int panel_height = 0;
439
440 if (!output)
441 return 0;
442
443 wl_list_for_each(priv, &wlshell->panels, link) {
444 if (priv->output == output) {
445 panel_height = priv->surface->geometry.height;
446 break;
447 }
448 }
449 return panel_height;
450}
451
452static void
453shell_surface_set_maximized(struct wl_client *client,
454 struct wl_resource *resource,
455 struct wl_resource *output_resource )
456{
457 struct shell_surface *shsurf = resource->data;
458 struct weston_surface *es = shsurf->surface;
459 struct wl_shell *wlshell = NULL;
460 uint32_t edges = 0, panel_height = 0;
461
462 /* get the default output, if the client set it as NULL
463 check whether the ouput is available */
464 if (output_resource)
465 shsurf->output = output_resource->data;
466 else
467 shsurf->output = get_default_output(es->compositor);
468
469 if (reset_shell_surface_type(shsurf))
470 return;
471
472 shsurf->saved_x = es->geometry.x;
473 shsurf->saved_y = es->geometry.y;
474
475 wlshell = shell_surface_get_shell(shsurf);
476 panel_height = get_output_panel_height(wlshell, es->output);
477 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
478 wl_resource_post_event(&shsurf->resource,
479 WL_SHELL_SURFACE_CONFIGURE,
480 weston_compositor_get_time(), edges,
481 es->output->current->width,
482 es->output->current->height - panel_height);
483
484 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200485}
486
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400487static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200488shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500489 struct wl_resource *resource,
490 uint32_t method,
491 uint32_t framerate,
492 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400493{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200494 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500495 struct weston_surface *es = shsurf->surface;
496 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400497
Pekka Paalanen98262232011-12-01 10:42:22 +0200498 if (reset_shell_surface_type(shsurf))
499 return;
500
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400501 /* FIXME: Fullscreen on first output */
502 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200503 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400504 es->output = output;
505
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200506 shsurf->saved_x = es->geometry.x;
507 shsurf->saved_y = es->geometry.y;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200508 weston_surface_set_position(es,
509 (output->current->width - es->geometry.width) / 2,
510 (output->current->height - es->geometry.height) / 2);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400511 es->fullscreen_output = output;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200512 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400513}
514
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500515static void
516popup_grab_focus(struct wl_grab *grab, uint32_t time,
517 struct wl_surface *surface, int32_t x, int32_t y)
518{
519 struct wl_input_device *device = grab->input_device;
520 struct shell_surface *priv =
521 container_of(grab, struct shell_surface, popup.grab);
522 struct wl_client *client = priv->surface->surface.resource.client;
523
Pekka Paalanencb108432012-01-19 16:25:40 +0200524 if (surface && surface->resource.client == client) {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500525 wl_input_device_set_pointer_focus(device, surface, time,
526 device->x, device->y, x, y);
527 grab->focus = surface;
528 } else {
529 wl_input_device_set_pointer_focus(device, NULL,
530 time, 0, 0, 0, 0);
531 grab->focus = NULL;
532 }
533}
534
535static void
536popup_grab_motion(struct wl_grab *grab,
537 uint32_t time, int32_t x, int32_t y)
538{
539 struct wl_input_device *device = grab->input_device;
540 struct wl_resource *resource;
541
542 resource = grab->input_device->pointer_focus_resource;
543 if (resource)
544 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
545 time, device->x, device->y, x, y);
546}
547
548static void
549popup_grab_button(struct wl_grab *grab,
550 uint32_t time, int32_t button, int32_t state)
551{
552 struct wl_resource *resource;
553 struct shell_surface *shsurf =
554 container_of(grab, struct shell_surface, popup.grab);
555
556 resource = grab->input_device->pointer_focus_resource;
557 if (resource) {
558 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
559 time, button, state);
560 } else if (state == 0 &&
561 (shsurf->popup.initial_up ||
562 time - shsurf->popup.time > 500)) {
563 wl_resource_post_event(&shsurf->resource,
564 WL_SHELL_SURFACE_POPUP_DONE);
565 wl_input_device_end_grab(grab->input_device, time);
566 shsurf->popup.grab.input_device = NULL;
567 }
568
569 if (state == 0)
570 shsurf->popup.initial_up = 1;
571}
572
573static const struct wl_grab_interface popup_grab_interface = {
574 popup_grab_focus,
575 popup_grab_motion,
576 popup_grab_button,
577};
578
579static void
580shell_map_popup(struct shell_surface *shsurf, uint32_t time)
581{
582 struct wl_input_device *device;
583 struct weston_surface *es = shsurf->surface;
584 struct weston_surface *parent = shsurf->parent->surface;
585
586 es->output = parent->output;
587
588 shsurf->popup.grab.interface = &popup_grab_interface;
589 device = es->compositor->input_device;
590
Pekka Paalanen938269a2012-02-07 14:19:01 +0200591 weston_surface_update_transform(parent);
592 if (parent->transform.enabled) {
593 shsurf->popup.parent_transform.matrix =
594 parent->transform.matrix;
595 } else {
596 /* construct x, y translation matrix */
597 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
598 shsurf->popup.parent_transform.matrix.d[12] =
599 parent->geometry.x;
600 shsurf->popup.parent_transform.matrix.d[13] =
601 parent->geometry.y;
602 }
603 wl_list_insert(es->geometry.transformation_list.prev,
604 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200605 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500606
607 shsurf->popup.grab.input_device = device;
608 shsurf->popup.time = device->grab_time;
609 shsurf->popup.initial_up = 0;
610
611 wl_input_device_start_grab(shsurf->popup.grab.input_device,
612 &shsurf->popup.grab, shsurf->popup.time);
613}
614
615static void
616shell_surface_set_popup(struct wl_client *client,
617 struct wl_resource *resource,
618 struct wl_resource *input_device_resource,
619 uint32_t time,
620 struct wl_resource *parent_resource,
621 int32_t x, int32_t y, uint32_t flags)
622{
623 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500624
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500625 shsurf->type = SHELL_SURFACE_POPUP;
626 shsurf->parent = parent_resource->data;
627 shsurf->popup.x = x;
628 shsurf->popup.y = y;
629}
630
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200631static const struct wl_shell_surface_interface shell_surface_implementation = {
632 shell_surface_move,
633 shell_surface_resize,
634 shell_surface_set_toplevel,
635 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500636 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800637 shell_surface_set_popup,
638 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200639};
640
641static void
642destroy_shell_surface(struct wl_resource *resource)
643{
644 struct shell_surface *shsurf = resource->data;
645
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500646 if (shsurf->popup.grab.input_device)
647 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
648
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200649 /* in case cleaning up a dead client destroys shell_surface first */
650 if (shsurf->surface)
651 wl_list_remove(&shsurf->surface_destroy_listener.link);
652
653 wl_list_remove(&shsurf->link);
654 free(shsurf);
655}
656
657static void
658shell_handle_surface_destroy(struct wl_listener *listener,
659 struct wl_resource *resource, uint32_t time)
660{
661 struct shell_surface *shsurf = container_of(listener,
662 struct shell_surface,
663 surface_destroy_listener);
664
665 shsurf->surface = NULL;
666 wl_resource_destroy(&shsurf->resource, time);
667}
668
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200669static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500670get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200671{
672 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
673 struct wl_listener *listener;
674
675 /* search the destroy listener list for our callback */
676 wl_list_for_each(listener, lst, link) {
677 if (listener->func == shell_handle_surface_destroy) {
678 return container_of(listener, struct shell_surface,
679 surface_destroy_listener);
680 }
681 }
682
683 return NULL;
684}
685
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200686static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200687shell_get_shell_surface(struct wl_client *client,
688 struct wl_resource *resource,
689 uint32_t id,
690 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200691{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500692 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200693 struct shell_surface *shsurf;
694
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200695 if (get_shell_surface(surface)) {
696 wl_resource_post_error(surface_resource,
697 WL_DISPLAY_ERROR_INVALID_OBJECT,
698 "wl_shell::get_shell_surface already requested");
699 return;
700 }
701
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200702 shsurf = calloc(1, sizeof *shsurf);
703 if (!shsurf) {
704 wl_resource_post_no_memory(resource);
705 return;
706 }
707
708 shsurf->resource.destroy = destroy_shell_surface;
709 shsurf->resource.object.id = id;
710 shsurf->resource.object.interface = &wl_shell_surface_interface;
711 shsurf->resource.object.implementation =
712 (void (**)(void)) &shell_surface_implementation;
713 shsurf->resource.data = shsurf;
714
715 shsurf->surface = surface;
716 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
717 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
718 &shsurf->surface_destroy_listener.link);
719
720 /* init link so its safe to always remove it in destroy_shell_surface */
721 wl_list_init(&shsurf->link);
722
Pekka Paalanen460099f2012-01-20 16:48:25 +0200723 /* empty when not in use */
724 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500725 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200726
Pekka Paalanen98262232011-12-01 10:42:22 +0200727 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200728
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200729 wl_client_add_resource(client, &shsurf->resource);
730}
731
732static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200733 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500734};
735
Kristian Høgsberg07937562011-04-12 17:25:42 -0400736static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500737handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200738{
739 proc->pid = 0;
740}
741
742static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200743launch_screensaver(struct wl_shell *shell)
744{
745 if (shell->screensaver.binding)
746 return;
747
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200748 if (!shell->screensaver.path)
749 return;
750
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500751 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200752 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200753 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200754 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200755}
756
757static void
758terminate_screensaver(struct wl_shell *shell)
759{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200760 if (shell->screensaver.process.pid == 0)
761 return;
762
763 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200764}
765
766static void
767show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
768{
769 struct wl_list *list;
770
771 if (shell->lock_surface)
772 list = &shell->lock_surface->surface->link;
773 else
774 list = &shell->compositor->surface_list;
775
776 wl_list_remove(&surface->surface->link);
777 wl_list_insert(list, &surface->surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200778 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200779 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200780}
781
782static void
783hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
784{
785 wl_list_remove(&surface->surface->link);
786 wl_list_init(&surface->surface->link);
787 surface->surface->output = NULL;
788}
789
790static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400791desktop_shell_set_background(struct wl_client *client,
792 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100793 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400794 struct wl_resource *surface_resource)
795{
796 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200797 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500798 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200799 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400800
Pekka Paalanen98262232011-12-01 10:42:22 +0200801 if (reset_shell_surface_type(shsurf))
802 return;
803
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100804 wl_list_for_each(priv, &shell->backgrounds, link) {
805 if (priv->output == output_resource->data) {
806 priv->surface->output = NULL;
807 wl_list_remove(&priv->surface->link);
808 wl_list_remove(&priv->link);
809 break;
810 }
811 }
812
Pekka Paalanen068ae942011-11-28 14:11:15 +0200813 shsurf->type = SHELL_SURFACE_BACKGROUND;
814 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100815
Pekka Paalanen068ae942011-11-28 14:11:15 +0200816 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100817
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200818 weston_surface_set_position(surface, shsurf->output->x,
819 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200820
Kristian Høgsberg75840622011-09-06 13:48:16 -0400821 wl_resource_post_event(resource,
822 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500823 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200824 shsurf->output->current->width,
825 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400826}
827
828static void
829desktop_shell_set_panel(struct wl_client *client,
830 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100831 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400832 struct wl_resource *surface_resource)
833{
834 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200835 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500836 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200837 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400838
Pekka Paalanen98262232011-12-01 10:42:22 +0200839 if (reset_shell_surface_type(shsurf))
840 return;
841
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100842 wl_list_for_each(priv, &shell->panels, link) {
843 if (priv->output == output_resource->data) {
844 priv->surface->output = NULL;
845 wl_list_remove(&priv->surface->link);
846 wl_list_remove(&priv->link);
847 break;
848 }
849 }
850
Pekka Paalanen068ae942011-11-28 14:11:15 +0200851 shsurf->type = SHELL_SURFACE_PANEL;
852 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100853
Pekka Paalanen068ae942011-11-28 14:11:15 +0200854 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100855
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200856 weston_surface_set_position(surface, shsurf->output->x,
857 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200858
Kristian Høgsberg75840622011-09-06 13:48:16 -0400859 wl_resource_post_event(resource,
860 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500861 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200862 shsurf->output->current->width,
863 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400864}
865
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200866static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500867handle_lock_surface_destroy(struct wl_listener *listener,
868 struct wl_resource *resource, uint32_t time)
869{
870 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200871 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500872
873 fprintf(stderr, "lock surface gone\n");
874 shell->lock_surface = NULL;
875}
876
877static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200878desktop_shell_set_lock_surface(struct wl_client *client,
879 struct wl_resource *resource,
880 struct wl_resource *surface_resource)
881{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200882 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200883 struct shell_surface *surface = surface_resource->data;
884
885 if (reset_shell_surface_type(surface))
886 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200887
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200888 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200889
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200890 if (!shell->locked)
891 return;
892
Pekka Paalanen98262232011-12-01 10:42:22 +0200893 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200894
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500895 shell->lock_surface_listener.func = handle_lock_surface_destroy;
896 wl_list_insert(&surface_resource->destroy_listener_list,
897 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200898
Pekka Paalanen068ae942011-11-28 14:11:15 +0200899 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200900}
901
902static void
903resume_desktop(struct wl_shell *shell)
904{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500905 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200906 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200907 struct shell_surface *tmp;
908
909 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
910 hide_screensaver(shell, tmp);
911
912 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200913
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200914 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200915 weston_surface_assign_output(surface);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200916
Pekka Paalanenfe340832011-11-25 16:07:52 +0200917 if (wl_list_empty(&shell->backgrounds)) {
918 list = &shell->compositor->surface_list;
919 } else {
920 struct shell_surface *background;
921 background = container_of(shell->backgrounds.prev,
922 struct shell_surface, link);
923 list = background->surface->link.prev;
924 }
925
926 if (!wl_list_empty(&shell->hidden_surface_list))
927 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500928 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200929
930 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500931 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200932 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500933 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200934 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200935}
936
937static void
938desktop_shell_unlock(struct wl_client *client,
939 struct wl_resource *resource)
940{
941 struct wl_shell *shell = resource->data;
942
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200943 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200944
945 if (shell->locked)
946 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200947}
948
Kristian Høgsberg75840622011-09-06 13:48:16 -0400949static const struct desktop_shell_interface desktop_shell_implementation = {
950 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200951 desktop_shell_set_panel,
952 desktop_shell_set_lock_surface,
953 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400954};
955
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200956static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200958{
959 struct shell_surface *shsurf;
960
961 shsurf = get_shell_surface(surface);
962 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200963 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200964 return shsurf->type;
965}
966
Kristian Høgsberg75840622011-09-06 13:48:16 -0400967static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400968move_binding(struct wl_input_device *device, uint32_t time,
969 uint32_t key, uint32_t button, uint32_t state, void *data)
970{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500971 struct weston_surface *surface =
972 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500973
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100974 if (surface == NULL)
975 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400976
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200977 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100978 case SHELL_SURFACE_PANEL:
979 case SHELL_SURFACE_BACKGROUND:
980 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200981 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100982 return;
983 default:
984 break;
985 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400986
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500987 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400988}
989
990static void
991resize_binding(struct wl_input_device *device, uint32_t time,
992 uint32_t key, uint32_t button, uint32_t state, void *data)
993{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500994 struct weston_surface *surface =
995 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400996 uint32_t edges = 0;
997 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200998 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500999
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001000 if (surface == NULL)
1001 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001002
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001003 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001004 if (!shsurf)
1005 return;
1006
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001007 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001008 case SHELL_SURFACE_PANEL:
1009 case SHELL_SURFACE_BACKGROUND:
1010 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001011 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001012 return;
1013 default:
1014 break;
1015 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001016
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001017 weston_surface_from_global(surface,
1018 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001019
Pekka Paalanen60921e52012-01-25 15:55:43 +02001020 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001021 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001022 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001023 edges |= 0;
1024 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001025 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001026
Pekka Paalanen60921e52012-01-25 15:55:43 +02001027 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001028 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001029 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001030 edges |= 0;
1031 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001032 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001033
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001034 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001035 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001036}
1037
1038static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001039terminate_binding(struct wl_input_device *device, uint32_t time,
1040 uint32_t key, uint32_t button, uint32_t state, void *data)
1041{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001042 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001043
1044 if (state)
1045 wl_display_terminate(compositor->wl_display);
1046}
1047
1048static void
Pekka Paalanen460099f2012-01-20 16:48:25 +02001049rotate_grab_motion(struct wl_grab *grab,
1050 uint32_t time, int32_t x, int32_t y)
1051{
1052 struct rotate_grab *rotate =
1053 container_of(grab, struct rotate_grab, grab);
1054 struct wl_input_device *device = grab->input_device;
1055 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001056 GLfloat cx = 0.5f * surface->surface->geometry.width;
1057 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001058 GLfloat dx, dy;
1059 GLfloat r;
1060
1061 dx = device->x - rotate->center.x;
1062 dy = device->y - rotate->center.y;
1063 r = sqrtf(dx * dx + dy * dy);
1064
1065 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001066 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001067
1068 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001069 struct weston_matrix *matrix =
1070 &surface->rotation.transform.matrix;
1071
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001072 weston_matrix_init(&rotate->rotation);
1073 rotate->rotation.d[0] = dx / r;
1074 rotate->rotation.d[4] = -dy / r;
1075 rotate->rotation.d[1] = -rotate->rotation.d[4];
1076 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001077
1078 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001079 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001080 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1081 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001082 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001083
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001084 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001085 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001086 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001087 } else {
1088 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001089 weston_matrix_init(&surface->rotation.rotation);
1090 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001091 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001092
1093 /* Repaint implies weston_surface_update_transform(), which
1094 * lazily applies the damage due to rotation update.
1095 */
1096 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001097}
1098
1099static void
1100rotate_grab_button(struct wl_grab *grab,
1101 uint32_t time, int32_t button, int32_t state)
1102{
1103 struct rotate_grab *rotate =
1104 container_of(grab, struct rotate_grab, grab);
1105 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001106 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001107
1108 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001109 weston_matrix_multiply(&surface->rotation.rotation,
1110 &rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001111 wl_input_device_end_grab(device, time);
1112 free(rotate);
1113 }
1114}
1115
1116static const struct wl_grab_interface rotate_grab_interface = {
1117 noop_grab_focus,
1118 rotate_grab_motion,
1119 rotate_grab_button,
1120};
1121
1122static void
1123rotate_binding(struct wl_input_device *device, uint32_t time,
1124 uint32_t key, uint32_t button, uint32_t state, void *data)
1125{
1126 struct weston_surface *base_surface =
1127 (struct weston_surface *) device->pointer_focus;
1128 struct shell_surface *surface;
1129 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001130 GLfloat dx, dy;
1131 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001132
1133 if (base_surface == NULL)
1134 return;
1135
1136 surface = get_shell_surface(base_surface);
1137 if (!surface)
1138 return;
1139
1140 switch (surface->type) {
1141 case SHELL_SURFACE_PANEL:
1142 case SHELL_SURFACE_BACKGROUND:
1143 case SHELL_SURFACE_FULLSCREEN:
1144 case SHELL_SURFACE_SCREENSAVER:
1145 return;
1146 default:
1147 break;
1148 }
1149
Pekka Paalanen460099f2012-01-20 16:48:25 +02001150 rotate = malloc(sizeof *rotate);
1151 if (!rotate)
1152 return;
1153
1154 rotate->grab.interface = &rotate_grab_interface;
1155 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001156
1157 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001158 surface->surface->geometry.width / 2,
1159 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001160 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001161
1162 wl_input_device_start_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001163
1164 dx = device->x - rotate->center.x;
1165 dy = device->y - rotate->center.y;
1166 r = sqrtf(dx * dx + dy * dy);
1167 if (r > 20.0f) {
1168 struct weston_matrix inverse;
1169
1170 weston_matrix_init(&inverse);
1171 inverse.d[0] = dx / r;
1172 inverse.d[4] = dy / r;
1173 inverse.d[1] = -inverse.d[4];
1174 inverse.d[5] = inverse.d[0];
1175 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1176 } else {
1177 weston_matrix_init(&surface->rotation.rotation);
1178 weston_matrix_init(&rotate->rotation);
1179 }
1180
Pekka Paalanen460099f2012-01-20 16:48:25 +02001181 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
1182}
1183
1184static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001185activate(struct weston_shell *base, struct weston_surface *es,
1186 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001187{
1188 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001189 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001190 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001191
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001192 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001193
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001194 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001195 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001196
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001197 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001198 case SHELL_SURFACE_BACKGROUND:
1199 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001200 wl_list_remove(&es->link);
1201 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001202 break;
1203 case SHELL_SURFACE_PANEL:
1204 /* already put on top */
1205 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001206 case SHELL_SURFACE_SCREENSAVER:
1207 /* always below lock surface */
1208 if (shell->lock_surface) {
1209 wl_list_remove(&es->link);
1210 wl_list_insert(&shell->lock_surface->surface->link,
1211 &es->link);
1212 }
1213 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001214 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001215 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001216 list = weston_compositor_top(compositor);
1217
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001218 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001219 struct shell_surface *panel;
1220 wl_list_for_each(panel, &shell->panels, link) {
1221 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001222 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001223 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001224 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001225 }
1226}
1227
1228static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001229click_to_activate_binding(struct wl_input_device *device,
1230 uint32_t time, uint32_t key,
1231 uint32_t button, uint32_t state, void *data)
1232{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001233 struct weston_input_device *wd = (struct weston_input_device *) device;
1234 struct weston_compositor *compositor = data;
1235 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001236
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001237 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001238 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001239 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001240}
1241
1242static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001243lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001244{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001245 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001246 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001247 struct weston_surface *cur;
1248 struct weston_surface *tmp;
1249 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001250 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001251 uint32_t time;
1252
1253 if (shell->locked)
1254 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001255
1256 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001257
1258 /* Move all surfaces from compositor's list to our hidden list,
1259 * except the background. This way nothing else can show or
1260 * receive input events while we are locked. */
1261
1262 if (!wl_list_empty(&shell->hidden_surface_list)) {
1263 fprintf(stderr,
1264 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1265 __func__);
1266 }
1267
1268 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1269 /* skip input device sprites, cur->surface is uninitialised */
1270 if (cur->surface.resource.client == NULL)
1271 continue;
1272
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001273 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001274 continue;
1275
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001276 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001277 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001278 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001279 }
1280
Pekka Paalanen77346a62011-11-30 16:26:35 +02001281 launch_screensaver(shell);
1282
1283 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1284 show_screensaver(shell, shsurf);
1285
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001286 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001287 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001288 weston_compositor_wake(shell->compositor);
1289 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001290 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001291
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001292 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001294
1295 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001296 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001297 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1298 wl_input_device_set_keyboard_focus(&device->input_device,
1299 NULL, time);
1300 }
1301
1302 /* TODO: disable bindings that should not work while locked. */
1303
1304 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001305}
1306
1307static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001308unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001309{
1310 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1311
Pekka Paalanend81c2162011-11-16 13:47:34 +02001312 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001313 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001314 return;
1315 }
1316
1317 /* If desktop-shell client has gone away, unlock immediately. */
1318 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001319 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001320 return;
1321 }
1322
1323 if (shell->prepare_event_sent)
1324 return;
1325
1326 wl_resource_post_event(shell->child.desktop_shell,
1327 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1328 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001329}
1330
1331static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001332center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001333{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001334 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001335 GLfloat x = (mode->width - surface->geometry.width) / 2;
1336 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001337
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001338 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001339}
1340
1341static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001342map(struct weston_shell *base,
1343 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001344{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001345 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001346 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001347 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001348 struct shell_surface *shsurf;
1349 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1350 int do_configure;
Juan Zhao96879df2012-02-07 08:45:41 +08001351 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001352
Pekka Paalanen77346a62011-11-30 16:26:35 +02001353 shsurf = get_shell_surface(surface);
1354 if (shsurf)
1355 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001356
Pekka Paalanen77346a62011-11-30 16:26:35 +02001357 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001358 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001359 do_configure = 0;
1360 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001361 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001362 do_configure = 1;
1363 }
1364
Pekka Paalanen60921e52012-01-25 15:55:43 +02001365 surface->geometry.width = width;
1366 surface->geometry.height = height;
1367 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001368
1369 /* initial positioning, see also configure() */
1370 switch (surface_type) {
1371 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001372 weston_surface_set_position(surface, 10 + random() % 400,
1373 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001374 break;
1375 case SHELL_SURFACE_SCREENSAVER:
1376 case SHELL_SURFACE_FULLSCREEN:
1377 center_on_output(surface, surface->fullscreen_output);
1378 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001379 case SHELL_SURFACE_MAXIMIZED:
1380 /*use surface configure to set the geometry*/
1381 panel_height = get_output_panel_height(shell,surface->output);
1382 weston_surface_set_position(surface, surface->output->x,
1383 surface->output->y + panel_height);
1384 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001385 case SHELL_SURFACE_LOCK:
1386 center_on_output(surface, get_default_output(compositor));
1387 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001388 case SHELL_SURFACE_POPUP:
1389 shell_map_popup(shsurf, shsurf->popup.time);
1390 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001391 default:
1392 ;
1393 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001394
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001395 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001396 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001397 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001398 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001399 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001400 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001401 break;
1402 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001403 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001404 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001405 break;
1406 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001407 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001408 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001409
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001411 do_configure = 1;
1412 break;
1413 case SHELL_SURFACE_SCREENSAVER:
1414 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001415 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001416 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001417 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001418 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001419 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001421 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001422 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001423 break;
1424 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001425 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001426 if (!wl_list_empty(&shell->panels)) {
1427 struct shell_surface *panel =
1428 container_of(shell->panels.prev,
1429 struct shell_surface, link);
1430 wl_list_insert(&panel->surface->link, &surface->link);
1431 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001432 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001433 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001434 }
1435
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001436 if (do_configure) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001437 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001438 weston_compositor_repick(compositor);
Juan Zhao96879df2012-02-07 08:45:41 +08001439 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1440 surface->output = shsurf->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001441 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001442
Juan Zhao7bb92f02011-12-15 11:31:51 -05001443 switch (surface_type) {
1444 case SHELL_SURFACE_TOPLEVEL:
1445 case SHELL_SURFACE_TRANSIENT:
1446 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001447 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001448 if (!shell->locked)
1449 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001450 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001451 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001453 break;
1454 default:
1455 break;
1456 }
1457
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001458 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001459 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001460}
1461
1462static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001463configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001464 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001465{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001466 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1467 int do_configure = !shell->locked;
1468 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1469 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001470
Pekka Paalanen77346a62011-11-30 16:26:35 +02001471 shsurf = get_shell_surface(surface);
1472 if (shsurf)
1473 surface_type = shsurf->type;
1474
Pekka Paalanen60921e52012-01-25 15:55:43 +02001475 surface->geometry.width = width;
1476 surface->geometry.height = height;
1477 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001478
1479 switch (surface_type) {
1480 case SHELL_SURFACE_SCREENSAVER:
1481 do_configure = !do_configure;
1482 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001483 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001484 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001485 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001486 case SHELL_SURFACE_MAXIMIZED:
1487 /*setting x, y and using configure to change that geometry*/
1488 x = surface->output->x;
1489 y = surface->output->y + get_output_panel_height(shell,surface->output);
1490 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001491 default:
1492 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001493 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001494
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001495 /* XXX: would a fullscreen surface need the same handling? */
Pekka Paalanen77346a62011-11-30 16:26:35 +02001496 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001497 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001498 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001499
1500 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1501 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001502 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1503 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001504 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001505}
1506
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001507static int launch_desktop_shell_process(struct wl_shell *shell);
1508
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001509static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001511{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001512 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001513 struct wl_shell *shell =
1514 container_of(process, struct wl_shell, child.process);
1515
1516 shell->child.process.pid = 0;
1517 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001518
1519 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1520 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001521 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001522 shell->child.deathstamp = time;
1523 shell->child.deathcount = 0;
1524 }
1525
1526 shell->child.deathcount++;
1527 if (shell->child.deathcount > 5) {
1528 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1529 return;
1530 }
1531
1532 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1533 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001534}
1535
1536static int
1537launch_desktop_shell_process(struct wl_shell *shell)
1538{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001539 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001540
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001541 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001542 &shell->child.process,
1543 shell_exe,
1544 desktop_shell_sigchld);
1545
1546 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001547 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001548 return 0;
1549}
1550
1551static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001552bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1553{
1554 struct wl_shell *shell = data;
1555
1556 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001557 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001558}
1559
Kristian Høgsberg75840622011-09-06 13:48:16 -04001560static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001561unbind_desktop_shell(struct wl_resource *resource)
1562{
1563 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001564
1565 if (shell->locked)
1566 resume_desktop(shell);
1567
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001568 shell->child.desktop_shell = NULL;
1569 shell->prepare_event_sent = false;
1570 free(resource);
1571}
1572
1573static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001574bind_desktop_shell(struct wl_client *client,
1575 void *data, uint32_t version, uint32_t id)
1576{
1577 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001578 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001579
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001580 resource = wl_client_add_object(client, &desktop_shell_interface,
1581 &desktop_shell_implementation,
1582 id, shell);
1583
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001584 if (client == shell->child.client) {
1585 resource->destroy = unbind_desktop_shell;
1586 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001587 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001588 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001589
1590 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1591 "permission to bind desktop_shell denied");
1592 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001593}
1594
Pekka Paalanen6e168112011-11-24 11:34:05 +02001595static void
1596screensaver_set_surface(struct wl_client *client,
1597 struct wl_resource *resource,
1598 struct wl_resource *shell_surface_resource,
1599 struct wl_resource *output_resource)
1600{
1601 struct wl_shell *shell = resource->data;
1602 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001603 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001604
Pekka Paalanen98262232011-12-01 10:42:22 +02001605 if (reset_shell_surface_type(surface))
1606 return;
1607
Pekka Paalanen77346a62011-11-30 16:26:35 +02001608 surface->type = SHELL_SURFACE_SCREENSAVER;
1609
1610 surface->surface->fullscreen_output = output;
1611 surface->output = output;
1612 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001613}
1614
1615static const struct screensaver_interface screensaver_implementation = {
1616 screensaver_set_surface
1617};
1618
1619static void
1620unbind_screensaver(struct wl_resource *resource)
1621{
1622 struct wl_shell *shell = resource->data;
1623
Pekka Paalanen77346a62011-11-30 16:26:35 +02001624 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001625 free(resource);
1626}
1627
1628static void
1629bind_screensaver(struct wl_client *client,
1630 void *data, uint32_t version, uint32_t id)
1631{
1632 struct wl_shell *shell = data;
1633 struct wl_resource *resource;
1634
1635 resource = wl_client_add_object(client, &screensaver_interface,
1636 &screensaver_implementation,
1637 id, shell);
1638
Pekka Paalanen77346a62011-11-30 16:26:35 +02001639 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001640 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001641 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001642 return;
1643 }
1644
1645 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1646 "interface object already bound");
1647 wl_resource_destroy(resource, 0);
1648}
1649
Pekka Paalanen3c647232011-12-22 13:43:43 +02001650static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001651shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001652{
1653 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1654
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001655 if (shell->child.client)
1656 wl_client_destroy(shell->child.client);
1657
Pekka Paalanen3c647232011-12-22 13:43:43 +02001658 free(shell->screensaver.path);
1659 free(shell);
1660}
1661
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001662int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001663shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001664
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001665WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001666shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001668 struct wl_shell *shell;
1669
1670 shell = malloc(sizeof *shell);
1671 if (shell == NULL)
1672 return -1;
1673
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001674 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001675 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001676 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001677 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001678 shell->shell.map = map;
1679 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001680 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001681
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001682 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001683 wl_list_init(&shell->backgrounds);
1684 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001685 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001686
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001687 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001688
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001689 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1690 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001691 return -1;
1692
Kristian Høgsberg75840622011-09-06 13:48:16 -04001693 if (wl_display_add_global(ec->wl_display,
1694 &desktop_shell_interface,
1695 shell, bind_desktop_shell) == NULL)
1696 return -1;
1697
Pekka Paalanen6e168112011-11-24 11:34:05 +02001698 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1699 shell, bind_screensaver) == NULL)
1700 return -1;
1701
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001702 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001703 if (launch_desktop_shell_process(shell) != 0)
1704 return -1;
1705
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001706 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001707 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001709 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001710 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001711 MODIFIER_CTRL | MODIFIER_ALT,
1712 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001713 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001714 click_to_activate_binding, ec);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001715 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1716 MODIFIER_SUPER | MODIFIER_ALT,
1717 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001718
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001719 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001720
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001721 return 0;
1722}