blob: d5da6649ed743d244b2d194b6902cb6add211c24 [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
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200211 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400212
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
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200319 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400320
321 return 0;
322}
323
324static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200325shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
326 struct wl_resource *input_resource, uint32_t time,
327 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400328{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500329 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200330 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400331
332 /* FIXME: Reject if fullscreen */
333
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500334 if (wd->input_device.button_count == 0 ||
335 wd->input_device.grab_time != time ||
336 wd->input_device.pointer_focus != &shsurf->surface->surface)
337 return;
338
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500339 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400340 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500341}
342
Juan Zhao96879df2012-02-07 08:45:41 +0800343static struct weston_output *
344get_default_output(struct weston_compositor *compositor)
345{
346 return container_of(compositor->output_list.next,
347 struct weston_output, link);
348}
349
Pekka Paalanen98262232011-12-01 10:42:22 +0200350static int
351reset_shell_surface_type(struct shell_surface *surface)
352{
353 switch (surface->type) {
354 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200355 weston_surface_set_position(surface->surface,
356 surface->saved_x,
357 surface->saved_y);
Pekka Paalanen98262232011-12-01 10:42:22 +0200358 surface->surface->fullscreen_output = NULL;
359 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800360 case SHELL_SURFACE_MAXIMIZED:
361 surface->output = get_default_output(surface->surface->compositor);
362 weston_surface_set_position(surface->surface,
363 surface->saved_x,
364 surface->saved_y);
365 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200366 case SHELL_SURFACE_PANEL:
367 case SHELL_SURFACE_BACKGROUND:
368 wl_list_remove(&surface->link);
369 wl_list_init(&surface->link);
370 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200371 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200372 case SHELL_SURFACE_LOCK:
373 wl_resource_post_error(&surface->resource,
374 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200375 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200376 return -1;
377 case SHELL_SURFACE_NONE:
378 case SHELL_SURFACE_TOPLEVEL:
379 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500380 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200381 break;
382 }
383
384 surface->type = SHELL_SURFACE_NONE;
385 return 0;
386}
387
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500388static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200389shell_surface_set_toplevel(struct wl_client *client,
390 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400391
392{
Pekka Paalanen98262232011-12-01 10:42:22 +0200393 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400394
Pekka Paalanen98262232011-12-01 10:42:22 +0200395 if (reset_shell_surface_type(surface))
396 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400397
Pekka Paalanen98262232011-12-01 10:42:22 +0200398 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400399}
400
401static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200402shell_surface_set_transient(struct wl_client *client,
403 struct wl_resource *resource,
404 struct wl_resource *parent_resource,
405 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400406{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200407 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500408 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200409 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500410 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400411
Pekka Paalanen98262232011-12-01 10:42:22 +0200412 if (reset_shell_surface_type(shsurf))
413 return;
414
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400415 /* assign to parents output */
416 es->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200417 weston_surface_set_position(es, pes->geometry.x + x,
418 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400419
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200420 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400421}
422
Juan Zhao96879df2012-02-07 08:45:41 +0800423static struct wl_shell *
424shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200425{
Juan Zhao96879df2012-02-07 08:45:41 +0800426 struct weston_surface *es = shsurf->surface;
427 struct weston_shell *shell = es->compositor->shell;
428
429 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
430}
431
432static int
433get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
434{
435 struct shell_surface *priv;
436 int panel_height = 0;
437
438 if (!output)
439 return 0;
440
441 wl_list_for_each(priv, &wlshell->panels, link) {
442 if (priv->output == output) {
443 panel_height = priv->surface->geometry.height;
444 break;
445 }
446 }
447 return panel_height;
448}
449
450static void
451shell_surface_set_maximized(struct wl_client *client,
452 struct wl_resource *resource,
453 struct wl_resource *output_resource )
454{
455 struct shell_surface *shsurf = resource->data;
456 struct weston_surface *es = shsurf->surface;
457 struct wl_shell *wlshell = NULL;
458 uint32_t edges = 0, panel_height = 0;
459
460 /* get the default output, if the client set it as NULL
461 check whether the ouput is available */
462 if (output_resource)
463 shsurf->output = output_resource->data;
464 else
465 shsurf->output = get_default_output(es->compositor);
466
467 if (reset_shell_surface_type(shsurf))
468 return;
469
470 shsurf->saved_x = es->geometry.x;
471 shsurf->saved_y = es->geometry.y;
472
473 wlshell = shell_surface_get_shell(shsurf);
474 panel_height = get_output_panel_height(wlshell, es->output);
475 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
476 wl_resource_post_event(&shsurf->resource,
477 WL_SHELL_SURFACE_CONFIGURE,
478 weston_compositor_get_time(), edges,
479 es->output->current->width,
480 es->output->current->height - panel_height);
481
482 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200483}
484
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400485static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200486shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500487 struct wl_resource *resource,
488 uint32_t method,
489 uint32_t framerate,
490 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400491{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200492 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500493 struct weston_surface *es = shsurf->surface;
494 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400495
Pekka Paalanen98262232011-12-01 10:42:22 +0200496 if (reset_shell_surface_type(shsurf))
497 return;
498
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400499 /* FIXME: Fullscreen on first output */
500 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200501 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400502 es->output = output;
503
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200504 shsurf->saved_x = es->geometry.x;
505 shsurf->saved_y = es->geometry.y;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200506 weston_surface_set_position(es,
507 (output->current->width - es->geometry.width) / 2,
508 (output->current->height - es->geometry.height) / 2);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400509 es->fullscreen_output = output;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200510 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400511}
512
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500513static void
514popup_grab_focus(struct wl_grab *grab, uint32_t time,
515 struct wl_surface *surface, int32_t x, int32_t y)
516{
517 struct wl_input_device *device = grab->input_device;
518 struct shell_surface *priv =
519 container_of(grab, struct shell_surface, popup.grab);
520 struct wl_client *client = priv->surface->surface.resource.client;
521
Pekka Paalanencb108432012-01-19 16:25:40 +0200522 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200523 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500524 grab->focus = surface;
525 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200526 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500527 grab->focus = NULL;
528 }
529}
530
531static void
532popup_grab_motion(struct wl_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200533 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500534{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500535 struct wl_resource *resource;
536
537 resource = grab->input_device->pointer_focus_resource;
538 if (resource)
539 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200540 time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500541}
542
543static void
544popup_grab_button(struct wl_grab *grab,
545 uint32_t time, int32_t button, int32_t state)
546{
547 struct wl_resource *resource;
548 struct shell_surface *shsurf =
549 container_of(grab, struct shell_surface, popup.grab);
550
551 resource = grab->input_device->pointer_focus_resource;
552 if (resource) {
553 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
554 time, button, state);
555 } else if (state == 0 &&
556 (shsurf->popup.initial_up ||
557 time - shsurf->popup.time > 500)) {
558 wl_resource_post_event(&shsurf->resource,
559 WL_SHELL_SURFACE_POPUP_DONE);
560 wl_input_device_end_grab(grab->input_device, time);
561 shsurf->popup.grab.input_device = NULL;
562 }
563
564 if (state == 0)
565 shsurf->popup.initial_up = 1;
566}
567
568static const struct wl_grab_interface popup_grab_interface = {
569 popup_grab_focus,
570 popup_grab_motion,
571 popup_grab_button,
572};
573
574static void
575shell_map_popup(struct shell_surface *shsurf, uint32_t time)
576{
577 struct wl_input_device *device;
578 struct weston_surface *es = shsurf->surface;
579 struct weston_surface *parent = shsurf->parent->surface;
580
581 es->output = parent->output;
582
583 shsurf->popup.grab.interface = &popup_grab_interface;
584 device = es->compositor->input_device;
585
Pekka Paalanen938269a2012-02-07 14:19:01 +0200586 weston_surface_update_transform(parent);
587 if (parent->transform.enabled) {
588 shsurf->popup.parent_transform.matrix =
589 parent->transform.matrix;
590 } else {
591 /* construct x, y translation matrix */
592 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
593 shsurf->popup.parent_transform.matrix.d[12] =
594 parent->geometry.x;
595 shsurf->popup.parent_transform.matrix.d[13] =
596 parent->geometry.y;
597 }
598 wl_list_insert(es->geometry.transformation_list.prev,
599 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200600 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500601
602 shsurf->popup.grab.input_device = device;
603 shsurf->popup.time = device->grab_time;
604 shsurf->popup.initial_up = 0;
605
606 wl_input_device_start_grab(shsurf->popup.grab.input_device,
607 &shsurf->popup.grab, shsurf->popup.time);
608}
609
610static void
611shell_surface_set_popup(struct wl_client *client,
612 struct wl_resource *resource,
613 struct wl_resource *input_device_resource,
614 uint32_t time,
615 struct wl_resource *parent_resource,
616 int32_t x, int32_t y, uint32_t flags)
617{
618 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500619
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500620 shsurf->type = SHELL_SURFACE_POPUP;
621 shsurf->parent = parent_resource->data;
622 shsurf->popup.x = x;
623 shsurf->popup.y = y;
624}
625
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200626static const struct wl_shell_surface_interface shell_surface_implementation = {
627 shell_surface_move,
628 shell_surface_resize,
629 shell_surface_set_toplevel,
630 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500631 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800632 shell_surface_set_popup,
633 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200634};
635
636static void
637destroy_shell_surface(struct wl_resource *resource)
638{
639 struct shell_surface *shsurf = resource->data;
640
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500641 if (shsurf->popup.grab.input_device)
642 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
643
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200644 /* in case cleaning up a dead client destroys shell_surface first */
645 if (shsurf->surface)
646 wl_list_remove(&shsurf->surface_destroy_listener.link);
647
648 wl_list_remove(&shsurf->link);
649 free(shsurf);
650}
651
652static void
653shell_handle_surface_destroy(struct wl_listener *listener,
654 struct wl_resource *resource, uint32_t time)
655{
656 struct shell_surface *shsurf = container_of(listener,
657 struct shell_surface,
658 surface_destroy_listener);
659
660 shsurf->surface = NULL;
661 wl_resource_destroy(&shsurf->resource, time);
662}
663
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200664static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500665get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200666{
667 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
668 struct wl_listener *listener;
669
670 /* search the destroy listener list for our callback */
671 wl_list_for_each(listener, lst, link) {
672 if (listener->func == shell_handle_surface_destroy) {
673 return container_of(listener, struct shell_surface,
674 surface_destroy_listener);
675 }
676 }
677
678 return NULL;
679}
680
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200681static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200682shell_get_shell_surface(struct wl_client *client,
683 struct wl_resource *resource,
684 uint32_t id,
685 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200686{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500687 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200688 struct shell_surface *shsurf;
689
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200690 if (get_shell_surface(surface)) {
691 wl_resource_post_error(surface_resource,
692 WL_DISPLAY_ERROR_INVALID_OBJECT,
693 "wl_shell::get_shell_surface already requested");
694 return;
695 }
696
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200697 shsurf = calloc(1, sizeof *shsurf);
698 if (!shsurf) {
699 wl_resource_post_no_memory(resource);
700 return;
701 }
702
703 shsurf->resource.destroy = destroy_shell_surface;
704 shsurf->resource.object.id = id;
705 shsurf->resource.object.interface = &wl_shell_surface_interface;
706 shsurf->resource.object.implementation =
707 (void (**)(void)) &shell_surface_implementation;
708 shsurf->resource.data = shsurf;
709
710 shsurf->surface = surface;
711 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
712 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
713 &shsurf->surface_destroy_listener.link);
714
715 /* init link so its safe to always remove it in destroy_shell_surface */
716 wl_list_init(&shsurf->link);
717
Pekka Paalanen460099f2012-01-20 16:48:25 +0200718 /* empty when not in use */
719 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500720 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200721
Pekka Paalanen98262232011-12-01 10:42:22 +0200722 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200723
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200724 wl_client_add_resource(client, &shsurf->resource);
725}
726
727static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200728 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500729};
730
Kristian Høgsberg07937562011-04-12 17:25:42 -0400731static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500732handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200733{
734 proc->pid = 0;
735}
736
737static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200738launch_screensaver(struct wl_shell *shell)
739{
740 if (shell->screensaver.binding)
741 return;
742
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200743 if (!shell->screensaver.path)
744 return;
745
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500746 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200747 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200748 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200749 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200750}
751
752static void
753terminate_screensaver(struct wl_shell *shell)
754{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200755 if (shell->screensaver.process.pid == 0)
756 return;
757
758 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200759}
760
761static void
762show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
763{
764 struct wl_list *list;
765
766 if (shell->lock_surface)
767 list = &shell->lock_surface->surface->link;
768 else
769 list = &shell->compositor->surface_list;
770
771 wl_list_remove(&surface->surface->link);
772 wl_list_insert(list, &surface->surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200773 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200774 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200775}
776
777static void
778hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
779{
780 wl_list_remove(&surface->surface->link);
781 wl_list_init(&surface->surface->link);
782 surface->surface->output = NULL;
783}
784
785static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400786desktop_shell_set_background(struct wl_client *client,
787 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100788 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400789 struct wl_resource *surface_resource)
790{
791 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200792 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500793 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200794 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400795
Pekka Paalanen98262232011-12-01 10:42:22 +0200796 if (reset_shell_surface_type(shsurf))
797 return;
798
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100799 wl_list_for_each(priv, &shell->backgrounds, link) {
800 if (priv->output == output_resource->data) {
801 priv->surface->output = NULL;
802 wl_list_remove(&priv->surface->link);
803 wl_list_remove(&priv->link);
804 break;
805 }
806 }
807
Pekka Paalanen068ae942011-11-28 14:11:15 +0200808 shsurf->type = SHELL_SURFACE_BACKGROUND;
809 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100810
Pekka Paalanen068ae942011-11-28 14:11:15 +0200811 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100812
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200813 weston_surface_set_position(surface, shsurf->output->x,
814 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200815
Kristian Høgsberg75840622011-09-06 13:48:16 -0400816 wl_resource_post_event(resource,
817 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500818 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200819 shsurf->output->current->width,
820 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400821}
822
823static void
824desktop_shell_set_panel(struct wl_client *client,
825 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100826 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400827 struct wl_resource *surface_resource)
828{
829 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200830 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500831 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200832 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400833
Pekka Paalanen98262232011-12-01 10:42:22 +0200834 if (reset_shell_surface_type(shsurf))
835 return;
836
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100837 wl_list_for_each(priv, &shell->panels, link) {
838 if (priv->output == output_resource->data) {
839 priv->surface->output = NULL;
840 wl_list_remove(&priv->surface->link);
841 wl_list_remove(&priv->link);
842 break;
843 }
844 }
845
Pekka Paalanen068ae942011-11-28 14:11:15 +0200846 shsurf->type = SHELL_SURFACE_PANEL;
847 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100848
Pekka Paalanen068ae942011-11-28 14:11:15 +0200849 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100850
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200851 weston_surface_set_position(surface, shsurf->output->x,
852 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200853
Kristian Høgsberg75840622011-09-06 13:48:16 -0400854 wl_resource_post_event(resource,
855 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500856 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200857 shsurf->output->current->width,
858 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400859}
860
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200861static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500862handle_lock_surface_destroy(struct wl_listener *listener,
863 struct wl_resource *resource, uint32_t time)
864{
865 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200866 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500867
868 fprintf(stderr, "lock surface gone\n");
869 shell->lock_surface = NULL;
870}
871
872static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200873desktop_shell_set_lock_surface(struct wl_client *client,
874 struct wl_resource *resource,
875 struct wl_resource *surface_resource)
876{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200877 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200878 struct shell_surface *surface = surface_resource->data;
879
880 if (reset_shell_surface_type(surface))
881 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200882
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200883 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200884
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200885 if (!shell->locked)
886 return;
887
Pekka Paalanen98262232011-12-01 10:42:22 +0200888 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200889
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500890 shell->lock_surface_listener.func = handle_lock_surface_destroy;
891 wl_list_insert(&surface_resource->destroy_listener_list,
892 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200893
Pekka Paalanen068ae942011-11-28 14:11:15 +0200894 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200895}
896
897static void
898resume_desktop(struct wl_shell *shell)
899{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500900 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200901 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200902 struct shell_surface *tmp;
903
904 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
905 hide_screensaver(shell, tmp);
906
907 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200908
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200909 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200910 weston_surface_assign_output(surface);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200911
Pekka Paalanenfe340832011-11-25 16:07:52 +0200912 if (wl_list_empty(&shell->backgrounds)) {
913 list = &shell->compositor->surface_list;
914 } else {
915 struct shell_surface *background;
916 background = container_of(shell->backgrounds.prev,
917 struct shell_surface, link);
918 list = background->surface->link.prev;
919 }
920
921 if (!wl_list_empty(&shell->hidden_surface_list))
922 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500923 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200924
925 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500926 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200927 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500928 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200929 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200930}
931
932static void
933desktop_shell_unlock(struct wl_client *client,
934 struct wl_resource *resource)
935{
936 struct wl_shell *shell = resource->data;
937
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200938 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200939
940 if (shell->locked)
941 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200942}
943
Kristian Høgsberg75840622011-09-06 13:48:16 -0400944static const struct desktop_shell_interface desktop_shell_implementation = {
945 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200946 desktop_shell_set_panel,
947 desktop_shell_set_lock_surface,
948 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400949};
950
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200951static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200953{
954 struct shell_surface *shsurf;
955
956 shsurf = get_shell_surface(surface);
957 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200958 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200959 return shsurf->type;
960}
961
Kristian Høgsberg75840622011-09-06 13:48:16 -0400962static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400963move_binding(struct wl_input_device *device, uint32_t time,
964 uint32_t key, uint32_t button, uint32_t state, void *data)
965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500966 struct weston_surface *surface =
967 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500968
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100969 if (surface == NULL)
970 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400971
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200972 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100973 case SHELL_SURFACE_PANEL:
974 case SHELL_SURFACE_BACKGROUND:
975 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200976 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100977 return;
978 default:
979 break;
980 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400981
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500982 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400983}
984
985static void
986resize_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_surface *surface =
990 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400991 uint32_t edges = 0;
992 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200993 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500994
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100995 if (surface == NULL)
996 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200997
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200998 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200999 if (!shsurf)
1000 return;
1001
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001002 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001003 case SHELL_SURFACE_PANEL:
1004 case SHELL_SURFACE_BACKGROUND:
1005 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001006 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001007 return;
1008 default:
1009 break;
1010 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001011
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001012 weston_surface_from_global(surface,
1013 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001014
Pekka Paalanen60921e52012-01-25 15:55:43 +02001015 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001016 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001017 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001018 edges |= 0;
1019 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001020 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001021
Pekka Paalanen60921e52012-01-25 15:55:43 +02001022 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001023 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001024 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001025 edges |= 0;
1026 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001027 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001028
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001029 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001030 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001031}
1032
1033static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001034terminate_binding(struct wl_input_device *device, uint32_t time,
1035 uint32_t key, uint32_t button, uint32_t state, void *data)
1036{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001037 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001038
1039 if (state)
1040 wl_display_terminate(compositor->wl_display);
1041}
1042
1043static void
Pekka Paalanen460099f2012-01-20 16:48:25 +02001044rotate_grab_motion(struct wl_grab *grab,
1045 uint32_t time, int32_t x, int32_t y)
1046{
1047 struct rotate_grab *rotate =
1048 container_of(grab, struct rotate_grab, grab);
1049 struct wl_input_device *device = grab->input_device;
1050 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001051 GLfloat cx = 0.5f * surface->surface->geometry.width;
1052 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001053 GLfloat dx, dy;
1054 GLfloat r;
1055
1056 dx = device->x - rotate->center.x;
1057 dy = device->y - rotate->center.y;
1058 r = sqrtf(dx * dx + dy * dy);
1059
1060 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001061 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001062
1063 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001064 struct weston_matrix *matrix =
1065 &surface->rotation.transform.matrix;
1066
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001067 weston_matrix_init(&rotate->rotation);
1068 rotate->rotation.d[0] = dx / r;
1069 rotate->rotation.d[4] = -dy / r;
1070 rotate->rotation.d[1] = -rotate->rotation.d[4];
1071 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001072
1073 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001074 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001075 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1076 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001077 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001078
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001079 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001080 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001081 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001082 } else {
1083 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001084 weston_matrix_init(&surface->rotation.rotation);
1085 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001086 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001087
1088 /* Repaint implies weston_surface_update_transform(), which
1089 * lazily applies the damage due to rotation update.
1090 */
1091 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001092}
1093
1094static void
1095rotate_grab_button(struct wl_grab *grab,
1096 uint32_t time, int32_t button, int32_t state)
1097{
1098 struct rotate_grab *rotate =
1099 container_of(grab, struct rotate_grab, grab);
1100 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001101 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001102
1103 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001104 weston_matrix_multiply(&surface->rotation.rotation,
1105 &rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001106 wl_input_device_end_grab(device, time);
1107 free(rotate);
1108 }
1109}
1110
1111static const struct wl_grab_interface rotate_grab_interface = {
1112 noop_grab_focus,
1113 rotate_grab_motion,
1114 rotate_grab_button,
1115};
1116
1117static void
1118rotate_binding(struct wl_input_device *device, uint32_t time,
1119 uint32_t key, uint32_t button, uint32_t state, void *data)
1120{
1121 struct weston_surface *base_surface =
1122 (struct weston_surface *) device->pointer_focus;
1123 struct shell_surface *surface;
1124 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001125 GLfloat dx, dy;
1126 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001127
1128 if (base_surface == NULL)
1129 return;
1130
1131 surface = get_shell_surface(base_surface);
1132 if (!surface)
1133 return;
1134
1135 switch (surface->type) {
1136 case SHELL_SURFACE_PANEL:
1137 case SHELL_SURFACE_BACKGROUND:
1138 case SHELL_SURFACE_FULLSCREEN:
1139 case SHELL_SURFACE_SCREENSAVER:
1140 return;
1141 default:
1142 break;
1143 }
1144
Pekka Paalanen460099f2012-01-20 16:48:25 +02001145 rotate = malloc(sizeof *rotate);
1146 if (!rotate)
1147 return;
1148
1149 rotate->grab.interface = &rotate_grab_interface;
1150 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001151
1152 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001153 surface->surface->geometry.width / 2,
1154 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001155 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001156
1157 wl_input_device_start_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001158
1159 dx = device->x - rotate->center.x;
1160 dy = device->y - rotate->center.y;
1161 r = sqrtf(dx * dx + dy * dy);
1162 if (r > 20.0f) {
1163 struct weston_matrix inverse;
1164
1165 weston_matrix_init(&inverse);
1166 inverse.d[0] = dx / r;
1167 inverse.d[4] = dy / r;
1168 inverse.d[1] = -inverse.d[4];
1169 inverse.d[5] = inverse.d[0];
1170 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1171 } else {
1172 weston_matrix_init(&surface->rotation.rotation);
1173 weston_matrix_init(&rotate->rotation);
1174 }
1175
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001176 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001177}
1178
1179static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001180activate(struct weston_shell *base, struct weston_surface *es,
1181 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001182{
1183 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001184 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001185 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001186
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001187 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001188
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001189 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001191
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001192 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001193 case SHELL_SURFACE_BACKGROUND:
1194 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001195 wl_list_remove(&es->link);
1196 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001197 break;
1198 case SHELL_SURFACE_PANEL:
1199 /* already put on top */
1200 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001201 case SHELL_SURFACE_SCREENSAVER:
1202 /* always below lock surface */
1203 if (shell->lock_surface) {
1204 wl_list_remove(&es->link);
1205 wl_list_insert(&shell->lock_surface->surface->link,
1206 &es->link);
1207 }
1208 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001209 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001210 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001211 list = weston_compositor_top(compositor);
1212
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001213 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001214 struct shell_surface *panel;
1215 wl_list_for_each(panel, &shell->panels, link) {
1216 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001217 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001218 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001219 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001220 }
1221}
1222
1223static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001224click_to_activate_binding(struct wl_input_device *device,
1225 uint32_t time, uint32_t key,
1226 uint32_t button, uint32_t state, void *data)
1227{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001228 struct weston_input_device *wd = (struct weston_input_device *) device;
1229 struct weston_compositor *compositor = data;
1230 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001231
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001232 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001233 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001234 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001235}
1236
1237static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001238lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001239{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001240 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001241 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001242 struct weston_surface *cur;
1243 struct weston_surface *tmp;
1244 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001245 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001246 uint32_t time;
1247
1248 if (shell->locked)
1249 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001250
1251 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001252
1253 /* Move all surfaces from compositor's list to our hidden list,
1254 * except the background. This way nothing else can show or
1255 * receive input events while we are locked. */
1256
1257 if (!wl_list_empty(&shell->hidden_surface_list)) {
1258 fprintf(stderr,
1259 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1260 __func__);
1261 }
1262
1263 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1264 /* skip input device sprites, cur->surface is uninitialised */
1265 if (cur->surface.resource.client == NULL)
1266 continue;
1267
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001268 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001269 continue;
1270
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001271 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001272 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001273 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001274 }
1275
Pekka Paalanen77346a62011-11-30 16:26:35 +02001276 launch_screensaver(shell);
1277
1278 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1279 show_screensaver(shell, shsurf);
1280
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001281 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001282 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001283 weston_compositor_wake(shell->compositor);
1284 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001285 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001286
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001287 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001288 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001289
1290 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001291 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001292 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1293 wl_input_device_set_keyboard_focus(&device->input_device,
1294 NULL, time);
1295 }
1296
1297 /* TODO: disable bindings that should not work while locked. */
1298
1299 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001300}
1301
1302static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001303unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001304{
1305 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1306
Pekka Paalanend81c2162011-11-16 13:47:34 +02001307 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001308 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001309 return;
1310 }
1311
1312 /* If desktop-shell client has gone away, unlock immediately. */
1313 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001314 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001315 return;
1316 }
1317
1318 if (shell->prepare_event_sent)
1319 return;
1320
1321 wl_resource_post_event(shell->child.desktop_shell,
1322 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1323 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001324}
1325
1326static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001327center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001328{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001329 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001330 GLfloat x = (mode->width - surface->geometry.width) / 2;
1331 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001332
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001333 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001334}
1335
1336static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001337map(struct weston_shell *base, struct weston_surface *surface,
1338 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001339{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001340 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001341 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001342 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001343 struct shell_surface *shsurf;
1344 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1345 int do_configure;
Juan Zhao96879df2012-02-07 08:45:41 +08001346 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001347
Pekka Paalanen77346a62011-11-30 16:26:35 +02001348 shsurf = get_shell_surface(surface);
1349 if (shsurf)
1350 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001351
Pekka Paalanen77346a62011-11-30 16:26:35 +02001352 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001353 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001354 do_configure = 0;
1355 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001356 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001357 do_configure = 1;
1358 }
1359
Pekka Paalanen60921e52012-01-25 15:55:43 +02001360 surface->geometry.width = width;
1361 surface->geometry.height = height;
1362 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001363
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001364 weston_compositor_update_drag_surfaces(compositor);
1365
Pekka Paalanen77346a62011-11-30 16:26:35 +02001366 /* initial positioning, see also configure() */
1367 switch (surface_type) {
1368 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001369 weston_surface_set_position(surface, 10 + random() % 400,
1370 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001371 break;
1372 case SHELL_SURFACE_SCREENSAVER:
1373 case SHELL_SURFACE_FULLSCREEN:
1374 center_on_output(surface, surface->fullscreen_output);
1375 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001376 case SHELL_SURFACE_MAXIMIZED:
1377 /*use surface configure to set the geometry*/
1378 panel_height = get_output_panel_height(shell,surface->output);
1379 weston_surface_set_position(surface, surface->output->x,
1380 surface->output->y + panel_height);
1381 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001382 case SHELL_SURFACE_LOCK:
1383 center_on_output(surface, get_default_output(compositor));
1384 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001385 case SHELL_SURFACE_POPUP:
1386 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001387 case SHELL_SURFACE_NONE:
1388 weston_surface_set_position(surface,
1389 surface->geometry.x + sx,
1390 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001391 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001392 default:
1393 ;
1394 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001395
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001396 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001397 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001398 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001399 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001400 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001401 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001402 break;
1403 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001404 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001405 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001406 break;
1407 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001408 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001409 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001410
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001411 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001412 do_configure = 1;
1413 break;
1414 case SHELL_SURFACE_SCREENSAVER:
1415 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001416 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001417 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001418 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001420 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001421 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001422 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001423 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001424 break;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001425 case SHELL_SURFACE_NONE:
1426 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001427 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001428 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001429 if (!wl_list_empty(&shell->panels)) {
1430 struct shell_surface *panel =
1431 container_of(shell->panels.prev,
1432 struct shell_surface, link);
1433 wl_list_insert(&panel->surface->link, &surface->link);
1434 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001435 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001436 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001437 }
1438
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001439 if (do_configure) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001440 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001441 weston_compositor_repick(compositor);
Juan Zhao96879df2012-02-07 08:45:41 +08001442 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1443 surface->output = shsurf->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001444 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001445
Juan Zhao7bb92f02011-12-15 11:31:51 -05001446 switch (surface_type) {
1447 case SHELL_SURFACE_TOPLEVEL:
1448 case SHELL_SURFACE_TRANSIENT:
1449 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001450 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001451 if (!shell->locked)
1452 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001453 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001454 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001455 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001456 break;
1457 default:
1458 break;
1459 }
1460
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001461 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001462 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001463}
1464
1465static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001467 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001468{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001469 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1470 int do_configure = !shell->locked;
1471 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1472 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001473
Pekka Paalanen77346a62011-11-30 16:26:35 +02001474 shsurf = get_shell_surface(surface);
1475 if (shsurf)
1476 surface_type = shsurf->type;
1477
Pekka Paalanen60921e52012-01-25 15:55:43 +02001478 surface->geometry.width = width;
1479 surface->geometry.height = height;
1480 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001481
1482 switch (surface_type) {
1483 case SHELL_SURFACE_SCREENSAVER:
1484 do_configure = !do_configure;
1485 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001486 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001487 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001488 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001489 case SHELL_SURFACE_MAXIMIZED:
1490 /*setting x, y and using configure to change that geometry*/
1491 x = surface->output->x;
1492 y = surface->output->y + get_output_panel_height(shell,surface->output);
1493 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001494 default:
1495 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001496 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001497
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001498 /* XXX: would a fullscreen surface need the same handling? */
Pekka Paalanen77346a62011-11-30 16:26:35 +02001499 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001500 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001501 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001502
1503 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1504 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001505 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1506 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001507 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001508}
1509
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001510static int launch_desktop_shell_process(struct wl_shell *shell);
1511
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001512static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001513desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001514{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001515 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001516 struct wl_shell *shell =
1517 container_of(process, struct wl_shell, child.process);
1518
1519 shell->child.process.pid = 0;
1520 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001521
1522 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1523 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001524 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001525 shell->child.deathstamp = time;
1526 shell->child.deathcount = 0;
1527 }
1528
1529 shell->child.deathcount++;
1530 if (shell->child.deathcount > 5) {
1531 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1532 return;
1533 }
1534
1535 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1536 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001537}
1538
1539static int
1540launch_desktop_shell_process(struct wl_shell *shell)
1541{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001542 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001543
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001544 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001545 &shell->child.process,
1546 shell_exe,
1547 desktop_shell_sigchld);
1548
1549 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001550 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001551 return 0;
1552}
1553
1554static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001555bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1556{
1557 struct wl_shell *shell = data;
1558
1559 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001560 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001561}
1562
Kristian Høgsberg75840622011-09-06 13:48:16 -04001563static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001564unbind_desktop_shell(struct wl_resource *resource)
1565{
1566 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001567
1568 if (shell->locked)
1569 resume_desktop(shell);
1570
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001571 shell->child.desktop_shell = NULL;
1572 shell->prepare_event_sent = false;
1573 free(resource);
1574}
1575
1576static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001577bind_desktop_shell(struct wl_client *client,
1578 void *data, uint32_t version, uint32_t id)
1579{
1580 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001581 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001582
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001583 resource = wl_client_add_object(client, &desktop_shell_interface,
1584 &desktop_shell_implementation,
1585 id, shell);
1586
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001587 if (client == shell->child.client) {
1588 resource->destroy = unbind_desktop_shell;
1589 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001590 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001591 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001592
1593 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1594 "permission to bind desktop_shell denied");
1595 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001596}
1597
Pekka Paalanen6e168112011-11-24 11:34:05 +02001598static void
1599screensaver_set_surface(struct wl_client *client,
1600 struct wl_resource *resource,
1601 struct wl_resource *shell_surface_resource,
1602 struct wl_resource *output_resource)
1603{
1604 struct wl_shell *shell = resource->data;
1605 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001606 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001607
Pekka Paalanen98262232011-12-01 10:42:22 +02001608 if (reset_shell_surface_type(surface))
1609 return;
1610
Pekka Paalanen77346a62011-11-30 16:26:35 +02001611 surface->type = SHELL_SURFACE_SCREENSAVER;
1612
1613 surface->surface->fullscreen_output = output;
1614 surface->output = output;
1615 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001616}
1617
1618static const struct screensaver_interface screensaver_implementation = {
1619 screensaver_set_surface
1620};
1621
1622static void
1623unbind_screensaver(struct wl_resource *resource)
1624{
1625 struct wl_shell *shell = resource->data;
1626
Pekka Paalanen77346a62011-11-30 16:26:35 +02001627 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001628 free(resource);
1629}
1630
1631static void
1632bind_screensaver(struct wl_client *client,
1633 void *data, uint32_t version, uint32_t id)
1634{
1635 struct wl_shell *shell = data;
1636 struct wl_resource *resource;
1637
1638 resource = wl_client_add_object(client, &screensaver_interface,
1639 &screensaver_implementation,
1640 id, shell);
1641
Pekka Paalanen77346a62011-11-30 16:26:35 +02001642 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001643 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001644 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001645 return;
1646 }
1647
1648 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1649 "interface object already bound");
1650 wl_resource_destroy(resource, 0);
1651}
1652
Pekka Paalanen3c647232011-12-22 13:43:43 +02001653static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001654shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001655{
1656 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1657
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001658 if (shell->child.client)
1659 wl_client_destroy(shell->child.client);
1660
Pekka Paalanen3c647232011-12-22 13:43:43 +02001661 free(shell->screensaver.path);
1662 free(shell);
1663}
1664
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001665int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001666shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001667
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001668WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001669shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001670{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001671 struct wl_shell *shell;
1672
1673 shell = malloc(sizeof *shell);
1674 if (shell == NULL)
1675 return -1;
1676
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001677 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001678 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001679 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001680 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001681 shell->shell.map = map;
1682 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001683 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001684
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001685 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001686 wl_list_init(&shell->backgrounds);
1687 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001688 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001689
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001690 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001691
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001692 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1693 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001694 return -1;
1695
Kristian Høgsberg75840622011-09-06 13:48:16 -04001696 if (wl_display_add_global(ec->wl_display,
1697 &desktop_shell_interface,
1698 shell, bind_desktop_shell) == NULL)
1699 return -1;
1700
Pekka Paalanen6e168112011-11-24 11:34:05 +02001701 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1702 shell, bind_screensaver) == NULL)
1703 return -1;
1704
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001705 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001706 if (launch_desktop_shell_process(shell) != 0)
1707 return -1;
1708
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001709 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001710 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001711 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001712 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001713 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001714 MODIFIER_CTRL | MODIFIER_ALT,
1715 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001717 click_to_activate_binding, ec);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001718 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1719 MODIFIER_SUPER | MODIFIER_ALT,
1720 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001721
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001722 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001723
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001724 return 0;
1725}