blob: cf94e8e16f83415316042bd439c94ae991e5e8b1 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050032
Pekka Paalanen50719bc2011-11-22 14:18:50 +020033#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040035#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020036#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Pekka Paalanen068ae942011-11-28 14:11:15 +020038struct shell_surface;
39
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040040struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050041 struct weston_compositor *compositor;
42 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020043
44 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050045 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020046 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020047 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020048
49 unsigned deathcount;
50 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020051 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020052
53 bool locked;
54 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020055
Pekka Paalanen068ae942011-11-28 14:11:15 +020056 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050057 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020058 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010059
60 struct wl_list backgrounds;
61 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020062
Pekka Paalanen77346a62011-11-30 16:26:35 +020063 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020064 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020065 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020066 struct wl_resource *binding;
67 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020069 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040070};
71
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050072enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020073 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050074
Pekka Paalanen57da4a82011-11-23 16:42:16 +020075 SHELL_SURFACE_PANEL,
76 SHELL_SURFACE_BACKGROUND,
77 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050079
80 SHELL_SURFACE_TOPLEVEL,
81 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050082 SHELL_SURFACE_FULLSCREEN,
83 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020084};
85
Pekka Paalanen56cdea92011-11-23 16:14:12 +020086struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020087 struct wl_resource resource;
88
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020090 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050091 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020092
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050093 enum shell_surface_type type;
94 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010095
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050096 struct {
97 struct wl_grab grab;
98 uint32_t time;
99 int32_t x, y;
100 int32_t initial_up;
101 } popup;
102
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500103 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100104 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200105};
106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500108 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500110 int32_t dx, dy;
111};
112
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500113static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200114shell_configuration(struct wl_shell *shell)
115{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200116 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200117 char *path = NULL;
118 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200119
120 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200121 { "path", CONFIG_KEY_STRING, &path },
122 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200123 };
124
125 struct config_section cs[] = {
126 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
127 };
128
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500129 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500130 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200131 free(config_file);
132
Pekka Paalanen7296e792011-12-07 16:22:00 +0200133 shell->screensaver.path = path;
134 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200135}
136
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200137static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500138noop_grab_focus(struct wl_grab *grab, uint32_t time,
139 struct wl_surface *surface, int32_t x, int32_t y)
140{
141 grab->focus = NULL;
142}
143
144static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500145move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500146 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500147{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500149 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500150 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500151
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500152 weston_surface_configure(es,
153 device->x + move->dx,
154 device->y + move->dy,
155 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500156}
157
158static void
159move_grab_button(struct wl_grab *grab,
160 uint32_t time, int32_t button, int32_t state)
161{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500162 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500163
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500164 if (device->button_count == 0 && state == 0) {
165 wl_input_device_end_grab(device, time);
166 free(grab);
167 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500168}
169
170static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500171 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172 move_grab_motion,
173 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174};
175
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400176static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500177weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500178 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500179{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500180 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500181
182 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400183 if (!move)
184 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500185
186 move->grab.interface = &move_grab_interface;
187 move->dx = es->x - wd->input_device.grab_x;
188 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500189 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500190
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500191 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500192
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400193 wl_input_device_set_pointer_focus(&wd->input_device,
194 NULL, time, 0, 0, 0, 0);
195
196 return 0;
197}
198
199static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200200shell_surface_move(struct wl_client *client, struct wl_resource *resource,
201 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400202{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500203 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200204 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400205
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500206 if (wd->input_device.button_count == 0 ||
207 wd->input_device.grab_time != time ||
208 wd->input_device.pointer_focus != &shsurf->surface->surface)
209 return;
210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500211 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400212 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500213}
214
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500215struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216 struct wl_grab grab;
217 uint32_t edges;
218 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200219 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220};
221
222static void
223resize_grab_motion(struct wl_grab *grab,
224 uint32_t time, int32_t x, int32_t y)
225{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500228 int32_t width, height;
229
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200230 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500231 width = device->grab_x - device->x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200232 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500233 width = device->x - device->grab_x + resize->width;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234 } else {
235 width = resize->width;
236 }
237
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200238 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500239 height = device->grab_y - device->y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200240 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500241 height = device->y - device->grab_y + resize->height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242 } else {
243 height = resize->height;
244 }
245
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200246 wl_resource_post_event(&resize->shsurf->resource,
247 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
248 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500249}
250
251static void
252resize_grab_button(struct wl_grab *grab,
253 uint32_t time, int32_t button, int32_t state)
254{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500255 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500257 if (device->button_count == 0 && state == 0) {
258 wl_input_device_end_grab(device, time);
259 free(grab);
260 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261}
262
263static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500264 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265 resize_grab_motion,
266 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500267};
268
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400269static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500270weston_surface_resize(struct shell_surface *shsurf,
271 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200272 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500273{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500274 struct weston_resize_grab *resize;
275 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500277 /* FIXME: Reject if fullscreen */
278
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400280 if (!resize)
281 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500282
283 resize->grab.interface = &resize_grab_interface;
284 resize->edges = edges;
285 resize->dx = es->x - wd->input_device.grab_x;
286 resize->dy = es->y - wd->input_device.grab_y;
287 resize->width = es->width;
288 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200289 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400290
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500291 if (edges == 0 || edges > 15 ||
292 (edges & 3) == 3 || (edges & 12) == 12)
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200293 goto err_out;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500294
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500295 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500296
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400297 wl_input_device_set_pointer_focus(&wd->input_device,
298 NULL, time, 0, 0, 0, 0);
299
300 return 0;
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200301
302err_out:
303 free(resize);
304 return 0;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400305}
306
307static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200308shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
309 struct wl_resource *input_resource, uint32_t time,
310 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400311{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500312 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200313 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400314
315 /* FIXME: Reject if fullscreen */
316
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500317 if (wd->input_device.button_count == 0 ||
318 wd->input_device.grab_time != time ||
319 wd->input_device.pointer_focus != &shsurf->surface->surface)
320 return;
321
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500322 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400323 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500324}
325
Pekka Paalanen98262232011-12-01 10:42:22 +0200326static int
327reset_shell_surface_type(struct shell_surface *surface)
328{
329 switch (surface->type) {
330 case SHELL_SURFACE_FULLSCREEN:
331 surface->surface->x = surface->saved_x;
332 surface->surface->y = surface->saved_y;
333 surface->surface->fullscreen_output = NULL;
334 break;
335 case SHELL_SURFACE_PANEL:
336 case SHELL_SURFACE_BACKGROUND:
337 wl_list_remove(&surface->link);
338 wl_list_init(&surface->link);
339 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200340 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200341 case SHELL_SURFACE_LOCK:
342 wl_resource_post_error(&surface->resource,
343 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200344 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200345 return -1;
346 case SHELL_SURFACE_NONE:
347 case SHELL_SURFACE_TOPLEVEL:
348 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500349 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200350 break;
351 }
352
353 surface->type = SHELL_SURFACE_NONE;
354 return 0;
355}
356
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500357static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200358shell_surface_set_toplevel(struct wl_client *client,
359 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400360
361{
Pekka Paalanen98262232011-12-01 10:42:22 +0200362 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400363
Pekka Paalanen98262232011-12-01 10:42:22 +0200364 if (reset_shell_surface_type(surface))
365 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400366
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500367 weston_surface_damage(surface->surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200368 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400369}
370
371static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200372shell_surface_set_transient(struct wl_client *client,
373 struct wl_resource *resource,
374 struct wl_resource *parent_resource,
375 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400376{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200377 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500378 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200379 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500380 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400381
Pekka Paalanen98262232011-12-01 10:42:22 +0200382 if (reset_shell_surface_type(shsurf))
383 return;
384
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400385 /* assign to parents output */
386 es->output = pes->output;
387
388 es->x = pes->x + x;
389 es->y = pes->y + y;
390
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500391 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200392 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400393}
394
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500395static struct weston_output *
396get_default_output(struct weston_compositor *compositor)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200397{
398 return container_of(compositor->output_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500399 struct weston_output, link);
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200400}
401
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400402static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200403shell_surface_set_fullscreen(struct wl_client *client,
404 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400405
406{
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;
409 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400410
Pekka Paalanen98262232011-12-01 10:42:22 +0200411 if (reset_shell_surface_type(shsurf))
412 return;
413
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400414 /* FIXME: Fullscreen on first output */
415 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200416 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400417 es->output = output;
418
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200419 shsurf->saved_x = es->x;
420 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400421 es->x = (output->current->width - es->width) / 2;
422 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400423 es->fullscreen_output = output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500424 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200425 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400426}
427
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500428static void
429popup_grab_focus(struct wl_grab *grab, uint32_t time,
430 struct wl_surface *surface, int32_t x, int32_t y)
431{
432 struct wl_input_device *device = grab->input_device;
433 struct shell_surface *priv =
434 container_of(grab, struct shell_surface, popup.grab);
435 struct wl_client *client = priv->surface->surface.resource.client;
436
Pekka Paalanencb108432012-01-19 16:25:40 +0200437 if (surface && surface->resource.client == client) {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500438 wl_input_device_set_pointer_focus(device, surface, time,
439 device->x, device->y, x, y);
440 grab->focus = surface;
441 } else {
442 wl_input_device_set_pointer_focus(device, NULL,
443 time, 0, 0, 0, 0);
444 grab->focus = NULL;
445 }
446}
447
448static void
449popup_grab_motion(struct wl_grab *grab,
450 uint32_t time, int32_t x, int32_t y)
451{
452 struct wl_input_device *device = grab->input_device;
453 struct wl_resource *resource;
454
455 resource = grab->input_device->pointer_focus_resource;
456 if (resource)
457 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
458 time, device->x, device->y, x, y);
459}
460
461static void
462popup_grab_button(struct wl_grab *grab,
463 uint32_t time, int32_t button, int32_t state)
464{
465 struct wl_resource *resource;
466 struct shell_surface *shsurf =
467 container_of(grab, struct shell_surface, popup.grab);
468
469 resource = grab->input_device->pointer_focus_resource;
470 if (resource) {
471 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
472 time, button, state);
473 } else if (state == 0 &&
474 (shsurf->popup.initial_up ||
475 time - shsurf->popup.time > 500)) {
476 wl_resource_post_event(&shsurf->resource,
477 WL_SHELL_SURFACE_POPUP_DONE);
478 wl_input_device_end_grab(grab->input_device, time);
479 shsurf->popup.grab.input_device = NULL;
480 }
481
482 if (state == 0)
483 shsurf->popup.initial_up = 1;
484}
485
486static const struct wl_grab_interface popup_grab_interface = {
487 popup_grab_focus,
488 popup_grab_motion,
489 popup_grab_button,
490};
491
492static void
493shell_map_popup(struct shell_surface *shsurf, uint32_t time)
494{
495 struct wl_input_device *device;
496 struct weston_surface *es = shsurf->surface;
497 struct weston_surface *parent = shsurf->parent->surface;
498
499 es->output = parent->output;
500
501 shsurf->popup.grab.interface = &popup_grab_interface;
502 device = es->compositor->input_device;
503
504 es->x = shsurf->parent->surface->x + shsurf->popup.x;
505 es->y = shsurf->parent->surface->y + shsurf->popup.y;
506
507 shsurf->popup.grab.input_device = device;
508 shsurf->popup.time = device->grab_time;
509 shsurf->popup.initial_up = 0;
510
511 wl_input_device_start_grab(shsurf->popup.grab.input_device,
512 &shsurf->popup.grab, shsurf->popup.time);
513}
514
515static void
516shell_surface_set_popup(struct wl_client *client,
517 struct wl_resource *resource,
518 struct wl_resource *input_device_resource,
519 uint32_t time,
520 struct wl_resource *parent_resource,
521 int32_t x, int32_t y, uint32_t flags)
522{
523 struct shell_surface *shsurf = resource->data;
524 struct weston_surface *es = shsurf->surface;
525
526 weston_surface_damage(es);
527 shsurf->type = SHELL_SURFACE_POPUP;
528 shsurf->parent = parent_resource->data;
529 shsurf->popup.x = x;
530 shsurf->popup.y = y;
531}
532
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200533static const struct wl_shell_surface_interface shell_surface_implementation = {
534 shell_surface_move,
535 shell_surface_resize,
536 shell_surface_set_toplevel,
537 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500538 shell_surface_set_fullscreen,
539 shell_surface_set_popup
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200540};
541
542static void
543destroy_shell_surface(struct wl_resource *resource)
544{
545 struct shell_surface *shsurf = resource->data;
546
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500547 if (shsurf->popup.grab.input_device)
548 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
549
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200550 /* in case cleaning up a dead client destroys shell_surface first */
551 if (shsurf->surface)
552 wl_list_remove(&shsurf->surface_destroy_listener.link);
553
554 wl_list_remove(&shsurf->link);
555 free(shsurf);
556}
557
558static void
559shell_handle_surface_destroy(struct wl_listener *listener,
560 struct wl_resource *resource, uint32_t time)
561{
562 struct shell_surface *shsurf = container_of(listener,
563 struct shell_surface,
564 surface_destroy_listener);
565
566 shsurf->surface = NULL;
567 wl_resource_destroy(&shsurf->resource, time);
568}
569
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200570static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500571get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200572{
573 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
574 struct wl_listener *listener;
575
576 /* search the destroy listener list for our callback */
577 wl_list_for_each(listener, lst, link) {
578 if (listener->func == shell_handle_surface_destroy) {
579 return container_of(listener, struct shell_surface,
580 surface_destroy_listener);
581 }
582 }
583
584 return NULL;
585}
586
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200587static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200588shell_get_shell_surface(struct wl_client *client,
589 struct wl_resource *resource,
590 uint32_t id,
591 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200592{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200594 struct shell_surface *shsurf;
595
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200596 if (get_shell_surface(surface)) {
597 wl_resource_post_error(surface_resource,
598 WL_DISPLAY_ERROR_INVALID_OBJECT,
599 "wl_shell::get_shell_surface already requested");
600 return;
601 }
602
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200603 shsurf = calloc(1, sizeof *shsurf);
604 if (!shsurf) {
605 wl_resource_post_no_memory(resource);
606 return;
607 }
608
609 shsurf->resource.destroy = destroy_shell_surface;
610 shsurf->resource.object.id = id;
611 shsurf->resource.object.interface = &wl_shell_surface_interface;
612 shsurf->resource.object.implementation =
613 (void (**)(void)) &shell_surface_implementation;
614 shsurf->resource.data = shsurf;
615
616 shsurf->surface = surface;
617 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
618 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
619 &shsurf->surface_destroy_listener.link);
620
621 /* init link so its safe to always remove it in destroy_shell_surface */
622 wl_list_init(&shsurf->link);
623
Pekka Paalanen98262232011-12-01 10:42:22 +0200624 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200625
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200626 wl_client_add_resource(client, &shsurf->resource);
627}
628
629static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200630 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500631};
632
Kristian Høgsberg07937562011-04-12 17:25:42 -0400633static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500634handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200635{
636 proc->pid = 0;
637}
638
639static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200640launch_screensaver(struct wl_shell *shell)
641{
642 if (shell->screensaver.binding)
643 return;
644
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200645 if (!shell->screensaver.path)
646 return;
647
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500648 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200649 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200650 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200651 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200652}
653
654static void
655terminate_screensaver(struct wl_shell *shell)
656{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200657 if (shell->screensaver.process.pid == 0)
658 return;
659
660 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200661}
662
663static void
664show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
665{
666 struct wl_list *list;
667
668 if (shell->lock_surface)
669 list = &shell->lock_surface->surface->link;
670 else
671 list = &shell->compositor->surface_list;
672
673 wl_list_remove(&surface->surface->link);
674 wl_list_insert(list, &surface->surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500675 weston_surface_configure(surface->surface,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200676 surface->surface->x,
677 surface->surface->y,
678 surface->surface->width,
679 surface->surface->height);
680 surface->surface->output = surface->output;
681}
682
683static void
684hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
685{
686 wl_list_remove(&surface->surface->link);
687 wl_list_init(&surface->surface->link);
688 surface->surface->output = NULL;
689}
690
691static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400692desktop_shell_set_background(struct wl_client *client,
693 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100694 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400695 struct wl_resource *surface_resource)
696{
697 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200698 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500699 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200700 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400701
Pekka Paalanen98262232011-12-01 10:42:22 +0200702 if (reset_shell_surface_type(shsurf))
703 return;
704
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100705 wl_list_for_each(priv, &shell->backgrounds, link) {
706 if (priv->output == output_resource->data) {
707 priv->surface->output = NULL;
708 wl_list_remove(&priv->surface->link);
709 wl_list_remove(&priv->link);
710 break;
711 }
712 }
713
Pekka Paalanen068ae942011-11-28 14:11:15 +0200714 shsurf->type = SHELL_SURFACE_BACKGROUND;
715 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100716
Pekka Paalanen068ae942011-11-28 14:11:15 +0200717 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100718
Pekka Paalanen068ae942011-11-28 14:11:15 +0200719 surface->x = shsurf->output->x;
720 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200721
Kristian Høgsberg75840622011-09-06 13:48:16 -0400722 wl_resource_post_event(resource,
723 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500724 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200725 shsurf->output->current->width,
726 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400727}
728
729static void
730desktop_shell_set_panel(struct wl_client *client,
731 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100732 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400733 struct wl_resource *surface_resource)
734{
735 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200736 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500737 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200738 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400739
Pekka Paalanen98262232011-12-01 10:42:22 +0200740 if (reset_shell_surface_type(shsurf))
741 return;
742
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100743 wl_list_for_each(priv, &shell->panels, link) {
744 if (priv->output == output_resource->data) {
745 priv->surface->output = NULL;
746 wl_list_remove(&priv->surface->link);
747 wl_list_remove(&priv->link);
748 break;
749 }
750 }
751
Pekka Paalanen068ae942011-11-28 14:11:15 +0200752 shsurf->type = SHELL_SURFACE_PANEL;
753 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100754
Pekka Paalanen068ae942011-11-28 14:11:15 +0200755 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100756
Pekka Paalanen068ae942011-11-28 14:11:15 +0200757 surface->x = shsurf->output->x;
758 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200759
Kristian Høgsberg75840622011-09-06 13:48:16 -0400760 wl_resource_post_event(resource,
761 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500762 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200763 shsurf->output->current->width,
764 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400765}
766
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200767static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500768handle_lock_surface_destroy(struct wl_listener *listener,
769 struct wl_resource *resource, uint32_t time)
770{
771 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200772 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500773
774 fprintf(stderr, "lock surface gone\n");
775 shell->lock_surface = NULL;
776}
777
778static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200779desktop_shell_set_lock_surface(struct wl_client *client,
780 struct wl_resource *resource,
781 struct wl_resource *surface_resource)
782{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200783 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200784 struct shell_surface *surface = surface_resource->data;
785
786 if (reset_shell_surface_type(surface))
787 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200788
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200789 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200790
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200791 if (!shell->locked)
792 return;
793
Pekka Paalanen98262232011-12-01 10:42:22 +0200794 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200795
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500796 shell->lock_surface_listener.func = handle_lock_surface_destroy;
797 wl_list_insert(&surface_resource->destroy_listener_list,
798 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200799
Pekka Paalanen068ae942011-11-28 14:11:15 +0200800 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200801}
802
803static void
804resume_desktop(struct wl_shell *shell)
805{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500806 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200807 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200808 struct shell_surface *tmp;
809
810 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
811 hide_screensaver(shell, tmp);
812
813 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200814
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500815 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500816 weston_surface_configure(surface, surface->x, surface->y,
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500817 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200818
Pekka Paalanenfe340832011-11-25 16:07:52 +0200819 if (wl_list_empty(&shell->backgrounds)) {
820 list = &shell->compositor->surface_list;
821 } else {
822 struct shell_surface *background;
823 background = container_of(shell->backgrounds.prev,
824 struct shell_surface, link);
825 list = background->surface->link.prev;
826 }
827
828 if (!wl_list_empty(&shell->hidden_surface_list))
829 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500830 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200831
832 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500833 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200834 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200836}
837
838static void
839desktop_shell_unlock(struct wl_client *client,
840 struct wl_resource *resource)
841{
842 struct wl_shell *shell = resource->data;
843
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200844 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200845
846 if (shell->locked)
847 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200848}
849
Kristian Høgsberg75840622011-09-06 13:48:16 -0400850static const struct desktop_shell_interface desktop_shell_implementation = {
851 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200852 desktop_shell_set_panel,
853 desktop_shell_set_lock_surface,
854 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400855};
856
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200857static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500858get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200859{
860 struct shell_surface *shsurf;
861
862 shsurf = get_shell_surface(surface);
863 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200864 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200865 return shsurf->type;
866}
867
Kristian Høgsberg75840622011-09-06 13:48:16 -0400868static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400869move_binding(struct wl_input_device *device, uint32_t time,
870 uint32_t key, uint32_t button, uint32_t state, void *data)
871{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500872 struct weston_surface *surface =
873 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500874
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100875 if (surface == NULL)
876 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400877
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200878 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100879 case SHELL_SURFACE_PANEL:
880 case SHELL_SURFACE_BACKGROUND:
881 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200882 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100883 return;
884 default:
885 break;
886 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400887
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500888 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400889}
890
891static void
892resize_binding(struct wl_input_device *device, uint32_t time,
893 uint32_t key, uint32_t button, uint32_t state, void *data)
894{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500895 struct weston_surface *surface =
896 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400897 uint32_t edges = 0;
898 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200899 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500900
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100901 if (surface == NULL)
902 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200903
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200904 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200905 if (!shsurf)
906 return;
907
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200908 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100909 case SHELL_SURFACE_PANEL:
910 case SHELL_SURFACE_BACKGROUND:
911 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200912 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100913 return;
914 default:
915 break;
916 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400917
Kristian Høgsberg07937562011-04-12 17:25:42 -0400918 x = device->grab_x - surface->x;
919 y = device->grab_y - surface->y;
920
921 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200922 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400923 else if (x < 2 * surface->width / 3)
924 edges |= 0;
925 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200926 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400927
928 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200929 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400930 else if (y < 2 * surface->height / 3)
931 edges |= 0;
932 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200933 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400934
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500935 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200936 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400937}
938
939static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500940terminate_binding(struct wl_input_device *device, uint32_t time,
941 uint32_t key, uint32_t button, uint32_t state, void *data)
942{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500943 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500944
945 if (state)
946 wl_display_terminate(compositor->wl_display);
947}
948
949static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950activate(struct weston_shell *base, struct weston_surface *es,
951 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -0400952{
953 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500954 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400955
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500956 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400957
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400958 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400960
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200961 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200962 case SHELL_SURFACE_BACKGROUND:
963 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400964 wl_list_remove(&es->link);
965 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200966 break;
967 case SHELL_SURFACE_PANEL:
968 /* already put on top */
969 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200970 case SHELL_SURFACE_SCREENSAVER:
971 /* always below lock surface */
972 if (shell->lock_surface) {
973 wl_list_remove(&es->link);
974 wl_list_insert(&shell->lock_surface->surface->link,
975 &es->link);
976 }
977 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200978 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100979 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200980 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100981 struct shell_surface *panel;
982 wl_list_for_each(panel, &shell->panels, link) {
983 wl_list_remove(&panel->surface->link);
984 wl_list_insert(&compositor->surface_list,
985 &panel->surface->link);
986 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200987 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400988 }
989}
990
991static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500992click_to_activate_binding(struct wl_input_device *device,
993 uint32_t time, uint32_t key,
994 uint32_t button, uint32_t state, void *data)
995{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500996 struct weston_input_device *wd = (struct weston_input_device *) device;
997 struct weston_compositor *compositor = data;
998 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500999
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001000 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001001 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001002 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001003}
1004
1005static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001006lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001007{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001008 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001009 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001010 struct weston_surface *cur;
1011 struct weston_surface *tmp;
1012 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001013 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001014 uint32_t time;
1015
1016 if (shell->locked)
1017 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001018
1019 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001020
1021 /* Move all surfaces from compositor's list to our hidden list,
1022 * except the background. This way nothing else can show or
1023 * receive input events while we are locked. */
1024
1025 if (!wl_list_empty(&shell->hidden_surface_list)) {
1026 fprintf(stderr,
1027 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1028 __func__);
1029 }
1030
1031 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1032 /* skip input device sprites, cur->surface is uninitialised */
1033 if (cur->surface.resource.client == NULL)
1034 continue;
1035
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001036 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001037 continue;
1038
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001039 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001040 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001041 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001042 }
1043
Pekka Paalanen77346a62011-11-30 16:26:35 +02001044 launch_screensaver(shell);
1045
1046 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1047 show_screensaver(shell, shsurf);
1048
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001049 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001050 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051 weston_compositor_wake(shell->compositor);
1052 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001053 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001054
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001055 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001056 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001057
1058 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001059 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001060 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1061 wl_input_device_set_keyboard_focus(&device->input_device,
1062 NULL, time);
1063 }
1064
1065 /* TODO: disable bindings that should not work while locked. */
1066
1067 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001068}
1069
1070static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001071unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001072{
1073 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1074
Pekka Paalanend81c2162011-11-16 13:47:34 +02001075 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001076 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001077 return;
1078 }
1079
1080 /* If desktop-shell client has gone away, unlock immediately. */
1081 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001082 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001083 return;
1084 }
1085
1086 if (shell->prepare_event_sent)
1087 return;
1088
1089 wl_resource_post_event(shell->child.desktop_shell,
1090 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1091 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001092}
1093
1094static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001095center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001096{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001097 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001098
1099 surface->x = output->x + (mode->width - surface->width) / 2;
1100 surface->y = output->y + (mode->height - surface->height) / 2;
1101}
1102
1103static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001104map(struct weston_shell *base,
1105 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001106{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001107 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001108 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001109 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001110 struct shell_surface *shsurf;
1111 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1112 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001113
Pekka Paalanen77346a62011-11-30 16:26:35 +02001114 shsurf = get_shell_surface(surface);
1115 if (shsurf)
1116 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001117
Pekka Paalanen77346a62011-11-30 16:26:35 +02001118 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001119 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001120 do_configure = 0;
1121 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001122 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001123 do_configure = 1;
1124 }
1125
1126 surface->width = width;
1127 surface->height = height;
1128
1129 /* initial positioning, see also configure() */
1130 switch (surface_type) {
1131 case SHELL_SURFACE_TOPLEVEL:
1132 surface->x = 10 + random() % 400;
1133 surface->y = 10 + random() % 400;
1134 break;
1135 case SHELL_SURFACE_SCREENSAVER:
1136 case SHELL_SURFACE_FULLSCREEN:
1137 center_on_output(surface, surface->fullscreen_output);
1138 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001139 case SHELL_SURFACE_LOCK:
1140 center_on_output(surface, get_default_output(compositor));
1141 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001142 default:
1143 ;
1144 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001145
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001146 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001147 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001148 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001149 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001150 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001151 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001152 break;
1153 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001154 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001155 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001156 break;
1157 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001158 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001159 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001160
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001161 weston_compositor_repick(compositor);
1162 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001163 do_configure = 1;
1164 break;
1165 case SHELL_SURFACE_SCREENSAVER:
1166 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001167 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001168 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001169 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001170 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001171 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001172 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001173 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001174 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001175 break;
1176 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001177 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001178 if (!wl_list_empty(&shell->panels)) {
1179 struct shell_surface *panel =
1180 container_of(shell->panels.prev,
1181 struct shell_surface, link);
1182 wl_list_insert(&panel->surface->link, &surface->link);
1183 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001184 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001185 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001186 }
1187
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001188 switch (surface_type) {
1189 case SHELL_SURFACE_TOPLEVEL:
1190 surface->x = 10 + random() % 400;
1191 surface->y = 10 + random() % 400;
1192 break;
1193 case SHELL_SURFACE_POPUP:
1194 shell_map_popup(shsurf, shsurf->popup.time);
1195 break;
1196 default:
1197 break;
1198 }
1199
1200 surface->width = width;
1201 surface->height = height;
1202 if (do_configure) {
1203 weston_surface_configure(surface, surface->x, surface->y,
1204 width, height);
1205 weston_compositor_repick(compositor);
1206 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001207
Juan Zhao7bb92f02011-12-15 11:31:51 -05001208 switch (surface_type) {
1209 case SHELL_SURFACE_TOPLEVEL:
1210 case SHELL_SURFACE_TRANSIENT:
1211 case SHELL_SURFACE_FULLSCREEN:
1212 if (!shell->locked)
1213 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001214 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001215 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001216 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001217 break;
1218 default:
1219 break;
1220 }
1221
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001222 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001223 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001224}
1225
1226static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001227configure(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001228 int32_t x, int32_t y, int32_t width, int32_t height)
1229{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001230 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1231 int do_configure = !shell->locked;
1232 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1233 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001234
Pekka Paalanen77346a62011-11-30 16:26:35 +02001235 shsurf = get_shell_surface(surface);
1236 if (shsurf)
1237 surface_type = shsurf->type;
1238
1239 surface->width = width;
1240 surface->height = height;
1241
1242 switch (surface_type) {
1243 case SHELL_SURFACE_SCREENSAVER:
1244 do_configure = !do_configure;
1245 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001246 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001247 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001248 break;
1249 default:
1250 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001251 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001252
Pekka Paalanen77346a62011-11-30 16:26:35 +02001253 /*
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001254 * weston_surface_configure() will assign an output, which means
Pekka Paalanen77346a62011-11-30 16:26:35 +02001255 * the surface is supposed to be in compositor->surface_list.
1256 * Be careful with that, and make sure we stay on the right output.
1257 * XXX: would a fullscreen surface need the same handling?
1258 */
1259 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001260 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001261
1262 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1263 surface->output = shsurf->output;
1264 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001265}
1266
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001267static int launch_desktop_shell_process(struct wl_shell *shell);
1268
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001269static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001270desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001271{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001272 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001273 struct wl_shell *shell =
1274 container_of(process, struct wl_shell, child.process);
1275
1276 shell->child.process.pid = 0;
1277 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001278
1279 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1280 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001281 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001282 shell->child.deathstamp = time;
1283 shell->child.deathcount = 0;
1284 }
1285
1286 shell->child.deathcount++;
1287 if (shell->child.deathcount > 5) {
1288 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1289 return;
1290 }
1291
1292 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1293 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001294}
1295
1296static int
1297launch_desktop_shell_process(struct wl_shell *shell)
1298{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001299 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001300
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001301 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001302 &shell->child.process,
1303 shell_exe,
1304 desktop_shell_sigchld);
1305
1306 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001307 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001308 return 0;
1309}
1310
1311static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001312bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1313{
1314 struct wl_shell *shell = data;
1315
1316 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001317 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001318}
1319
Kristian Høgsberg75840622011-09-06 13:48:16 -04001320static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001321unbind_desktop_shell(struct wl_resource *resource)
1322{
1323 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001324
1325 if (shell->locked)
1326 resume_desktop(shell);
1327
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001328 shell->child.desktop_shell = NULL;
1329 shell->prepare_event_sent = false;
1330 free(resource);
1331}
1332
1333static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001334bind_desktop_shell(struct wl_client *client,
1335 void *data, uint32_t version, uint32_t id)
1336{
1337 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001338 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001339
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001340 resource = wl_client_add_object(client, &desktop_shell_interface,
1341 &desktop_shell_implementation,
1342 id, shell);
1343
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001344 if (client == shell->child.client) {
1345 resource->destroy = unbind_desktop_shell;
1346 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001347 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001348 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001349
1350 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1351 "permission to bind desktop_shell denied");
1352 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001353}
1354
Pekka Paalanen6e168112011-11-24 11:34:05 +02001355static void
1356screensaver_set_surface(struct wl_client *client,
1357 struct wl_resource *resource,
1358 struct wl_resource *shell_surface_resource,
1359 struct wl_resource *output_resource)
1360{
1361 struct wl_shell *shell = resource->data;
1362 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001363 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001364
Pekka Paalanen98262232011-12-01 10:42:22 +02001365 if (reset_shell_surface_type(surface))
1366 return;
1367
Pekka Paalanen77346a62011-11-30 16:26:35 +02001368 surface->type = SHELL_SURFACE_SCREENSAVER;
1369
1370 surface->surface->fullscreen_output = output;
1371 surface->output = output;
1372 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001373}
1374
1375static const struct screensaver_interface screensaver_implementation = {
1376 screensaver_set_surface
1377};
1378
1379static void
1380unbind_screensaver(struct wl_resource *resource)
1381{
1382 struct wl_shell *shell = resource->data;
1383
Pekka Paalanen77346a62011-11-30 16:26:35 +02001384 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001385 free(resource);
1386}
1387
1388static void
1389bind_screensaver(struct wl_client *client,
1390 void *data, uint32_t version, uint32_t id)
1391{
1392 struct wl_shell *shell = data;
1393 struct wl_resource *resource;
1394
1395 resource = wl_client_add_object(client, &screensaver_interface,
1396 &screensaver_implementation,
1397 id, shell);
1398
Pekka Paalanen77346a62011-11-30 16:26:35 +02001399 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001400 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001401 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001402 return;
1403 }
1404
1405 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1406 "interface object already bound");
1407 wl_resource_destroy(resource, 0);
1408}
1409
Pekka Paalanen3c647232011-12-22 13:43:43 +02001410static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001411shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001412{
1413 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1414
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001415 if (shell->child.client)
1416 wl_client_destroy(shell->child.client);
1417
Pekka Paalanen3c647232011-12-22 13:43:43 +02001418 free(shell->screensaver.path);
1419 free(shell);
1420}
1421
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001422int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001423shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001424
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001425WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001426shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001427{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001428 struct wl_shell *shell;
1429
1430 shell = malloc(sizeof *shell);
1431 if (shell == NULL)
1432 return -1;
1433
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001434 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001435 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001436 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001437 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001438 shell->shell.map = map;
1439 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001440 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001441
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001442 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001443 wl_list_init(&shell->backgrounds);
1444 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001445 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001446
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001447 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001448
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001449 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1450 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001451 return -1;
1452
Kristian Høgsberg75840622011-09-06 13:48:16 -04001453 if (wl_display_add_global(ec->wl_display,
1454 &desktop_shell_interface,
1455 shell, bind_desktop_shell) == NULL)
1456 return -1;
1457
Pekka Paalanen6e168112011-11-24 11:34:05 +02001458 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1459 shell, bind_screensaver) == NULL)
1460 return -1;
1461
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001462 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001463 if (launch_desktop_shell_process(shell) != 0)
1464 return -1;
1465
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001467 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001469 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001471 MODIFIER_CTRL | MODIFIER_ALT,
1472 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001473 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001474 click_to_activate_binding, ec);
1475
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001476 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001477
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001478 return 0;
1479}