blob: 9219699491e32b633bfcef796b640e2562ba36ef [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øgsberga82c4862012-01-26 01:03:58 -0500955 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400956
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400958
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400959 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500960 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400961
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200962 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200963 case SHELL_SURFACE_BACKGROUND:
964 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400965 wl_list_remove(&es->link);
966 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200967 break;
968 case SHELL_SURFACE_PANEL:
969 /* already put on top */
970 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200971 case SHELL_SURFACE_SCREENSAVER:
972 /* always below lock surface */
973 if (shell->lock_surface) {
974 wl_list_remove(&es->link);
975 wl_list_insert(&shell->lock_surface->surface->link,
976 &es->link);
977 }
978 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200979 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100980 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500981 list = weston_compositor_top(compositor);
982
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200983 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100984 struct shell_surface *panel;
985 wl_list_for_each(panel, &shell->panels, link) {
986 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -0500987 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100988 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200989 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400990 }
991}
992
993static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500994click_to_activate_binding(struct wl_input_device *device,
995 uint32_t time, uint32_t key,
996 uint32_t button, uint32_t state, void *data)
997{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500998 struct weston_input_device *wd = (struct weston_input_device *) device;
999 struct weston_compositor *compositor = data;
1000 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001001
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001003 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001004 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001005}
1006
1007static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001008lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001009{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001010 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001011 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012 struct weston_surface *cur;
1013 struct weston_surface *tmp;
1014 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001015 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001016 uint32_t time;
1017
1018 if (shell->locked)
1019 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001020
1021 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001022
1023 /* Move all surfaces from compositor's list to our hidden list,
1024 * except the background. This way nothing else can show or
1025 * receive input events while we are locked. */
1026
1027 if (!wl_list_empty(&shell->hidden_surface_list)) {
1028 fprintf(stderr,
1029 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1030 __func__);
1031 }
1032
1033 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1034 /* skip input device sprites, cur->surface is uninitialised */
1035 if (cur->surface.resource.client == NULL)
1036 continue;
1037
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001038 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001039 continue;
1040
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001041 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001042 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001043 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001044 }
1045
Pekka Paalanen77346a62011-11-30 16:26:35 +02001046 launch_screensaver(shell);
1047
1048 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1049 show_screensaver(shell, shsurf);
1050
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001051 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001052 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 weston_compositor_wake(shell->compositor);
1054 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001055 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001056
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001057 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001058 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001059
1060 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001062 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1063 wl_input_device_set_keyboard_focus(&device->input_device,
1064 NULL, time);
1065 }
1066
1067 /* TODO: disable bindings that should not work while locked. */
1068
1069 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001070}
1071
1072static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001073unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001074{
1075 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1076
Pekka Paalanend81c2162011-11-16 13:47:34 +02001077 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001078 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001079 return;
1080 }
1081
1082 /* If desktop-shell client has gone away, unlock immediately. */
1083 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001084 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001085 return;
1086 }
1087
1088 if (shell->prepare_event_sent)
1089 return;
1090
1091 wl_resource_post_event(shell->child.desktop_shell,
1092 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1093 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001094}
1095
1096static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001097center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001098{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001099 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001100
1101 surface->x = output->x + (mode->width - surface->width) / 2;
1102 surface->y = output->y + (mode->height - surface->height) / 2;
1103}
1104
1105static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001106map(struct weston_shell *base,
1107 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001108{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001109 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001111 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001112 struct shell_surface *shsurf;
1113 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1114 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001115
Pekka Paalanen77346a62011-11-30 16:26:35 +02001116 shsurf = get_shell_surface(surface);
1117 if (shsurf)
1118 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001119
Pekka Paalanen77346a62011-11-30 16:26:35 +02001120 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001121 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001122 do_configure = 0;
1123 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001124 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001125 do_configure = 1;
1126 }
1127
1128 surface->width = width;
1129 surface->height = height;
1130
1131 /* initial positioning, see also configure() */
1132 switch (surface_type) {
1133 case SHELL_SURFACE_TOPLEVEL:
1134 surface->x = 10 + random() % 400;
1135 surface->y = 10 + random() % 400;
1136 break;
1137 case SHELL_SURFACE_SCREENSAVER:
1138 case SHELL_SURFACE_FULLSCREEN:
1139 center_on_output(surface, surface->fullscreen_output);
1140 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001141 case SHELL_SURFACE_LOCK:
1142 center_on_output(surface, get_default_output(compositor));
1143 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001144 default:
1145 ;
1146 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001147
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001148 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001149 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001150 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001151 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001152 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001153 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001154 break;
1155 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001156 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001157 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001158 break;
1159 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001160 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001161 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001162
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001163 weston_compositor_repick(compositor);
1164 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001165 do_configure = 1;
1166 break;
1167 case SHELL_SURFACE_SCREENSAVER:
1168 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001169 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001170 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001171 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001172 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001173 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001174 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001175 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001176 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001177 break;
1178 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001179 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001180 if (!wl_list_empty(&shell->panels)) {
1181 struct shell_surface *panel =
1182 container_of(shell->panels.prev,
1183 struct shell_surface, link);
1184 wl_list_insert(&panel->surface->link, &surface->link);
1185 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001186 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001187 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001188 }
1189
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001190 switch (surface_type) {
1191 case SHELL_SURFACE_TOPLEVEL:
1192 surface->x = 10 + random() % 400;
1193 surface->y = 10 + random() % 400;
1194 break;
1195 case SHELL_SURFACE_POPUP:
1196 shell_map_popup(shsurf, shsurf->popup.time);
1197 break;
1198 default:
1199 break;
1200 }
1201
1202 surface->width = width;
1203 surface->height = height;
1204 if (do_configure) {
1205 weston_surface_configure(surface, surface->x, surface->y,
1206 width, height);
1207 weston_compositor_repick(compositor);
1208 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001209
Juan Zhao7bb92f02011-12-15 11:31:51 -05001210 switch (surface_type) {
1211 case SHELL_SURFACE_TOPLEVEL:
1212 case SHELL_SURFACE_TRANSIENT:
1213 case SHELL_SURFACE_FULLSCREEN:
1214 if (!shell->locked)
1215 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001216 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001217 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001218 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001219 break;
1220 default:
1221 break;
1222 }
1223
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001224 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001225 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001226}
1227
1228static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001229configure(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001230 int32_t x, int32_t y, int32_t width, int32_t height)
1231{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001232 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1233 int do_configure = !shell->locked;
1234 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1235 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001236
Pekka Paalanen77346a62011-11-30 16:26:35 +02001237 shsurf = get_shell_surface(surface);
1238 if (shsurf)
1239 surface_type = shsurf->type;
1240
1241 surface->width = width;
1242 surface->height = height;
1243
1244 switch (surface_type) {
1245 case SHELL_SURFACE_SCREENSAVER:
1246 do_configure = !do_configure;
1247 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001248 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001249 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001250 break;
1251 default:
1252 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001253 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001254
Pekka Paalanen77346a62011-11-30 16:26:35 +02001255 /*
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001256 * weston_surface_configure() will assign an output, which means
Pekka Paalanen77346a62011-11-30 16:26:35 +02001257 * the surface is supposed to be in compositor->surface_list.
1258 * Be careful with that, and make sure we stay on the right output.
1259 * XXX: would a fullscreen surface need the same handling?
1260 */
1261 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001262 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001263
1264 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1265 surface->output = shsurf->output;
1266 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001267}
1268
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001269static int launch_desktop_shell_process(struct wl_shell *shell);
1270
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001271static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001272desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001273{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001274 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001275 struct wl_shell *shell =
1276 container_of(process, struct wl_shell, child.process);
1277
1278 shell->child.process.pid = 0;
1279 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001280
1281 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1282 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001283 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001284 shell->child.deathstamp = time;
1285 shell->child.deathcount = 0;
1286 }
1287
1288 shell->child.deathcount++;
1289 if (shell->child.deathcount > 5) {
1290 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1291 return;
1292 }
1293
1294 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1295 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001296}
1297
1298static int
1299launch_desktop_shell_process(struct wl_shell *shell)
1300{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001301 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001302
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001303 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001304 &shell->child.process,
1305 shell_exe,
1306 desktop_shell_sigchld);
1307
1308 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001309 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001310 return 0;
1311}
1312
1313static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001314bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1315{
1316 struct wl_shell *shell = data;
1317
1318 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001319 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001320}
1321
Kristian Høgsberg75840622011-09-06 13:48:16 -04001322static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001323unbind_desktop_shell(struct wl_resource *resource)
1324{
1325 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001326
1327 if (shell->locked)
1328 resume_desktop(shell);
1329
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001330 shell->child.desktop_shell = NULL;
1331 shell->prepare_event_sent = false;
1332 free(resource);
1333}
1334
1335static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001336bind_desktop_shell(struct wl_client *client,
1337 void *data, uint32_t version, uint32_t id)
1338{
1339 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001340 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001341
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001342 resource = wl_client_add_object(client, &desktop_shell_interface,
1343 &desktop_shell_implementation,
1344 id, shell);
1345
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001346 if (client == shell->child.client) {
1347 resource->destroy = unbind_desktop_shell;
1348 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001349 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001350 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001351
1352 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1353 "permission to bind desktop_shell denied");
1354 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001355}
1356
Pekka Paalanen6e168112011-11-24 11:34:05 +02001357static void
1358screensaver_set_surface(struct wl_client *client,
1359 struct wl_resource *resource,
1360 struct wl_resource *shell_surface_resource,
1361 struct wl_resource *output_resource)
1362{
1363 struct wl_shell *shell = resource->data;
1364 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001365 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001366
Pekka Paalanen98262232011-12-01 10:42:22 +02001367 if (reset_shell_surface_type(surface))
1368 return;
1369
Pekka Paalanen77346a62011-11-30 16:26:35 +02001370 surface->type = SHELL_SURFACE_SCREENSAVER;
1371
1372 surface->surface->fullscreen_output = output;
1373 surface->output = output;
1374 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001375}
1376
1377static const struct screensaver_interface screensaver_implementation = {
1378 screensaver_set_surface
1379};
1380
1381static void
1382unbind_screensaver(struct wl_resource *resource)
1383{
1384 struct wl_shell *shell = resource->data;
1385
Pekka Paalanen77346a62011-11-30 16:26:35 +02001386 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001387 free(resource);
1388}
1389
1390static void
1391bind_screensaver(struct wl_client *client,
1392 void *data, uint32_t version, uint32_t id)
1393{
1394 struct wl_shell *shell = data;
1395 struct wl_resource *resource;
1396
1397 resource = wl_client_add_object(client, &screensaver_interface,
1398 &screensaver_implementation,
1399 id, shell);
1400
Pekka Paalanen77346a62011-11-30 16:26:35 +02001401 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001402 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001403 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001404 return;
1405 }
1406
1407 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1408 "interface object already bound");
1409 wl_resource_destroy(resource, 0);
1410}
1411
Pekka Paalanen3c647232011-12-22 13:43:43 +02001412static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001413shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001414{
1415 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1416
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001417 if (shell->child.client)
1418 wl_client_destroy(shell->child.client);
1419
Pekka Paalanen3c647232011-12-22 13:43:43 +02001420 free(shell->screensaver.path);
1421 free(shell);
1422}
1423
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001424int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001426
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001427WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001428shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001429{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001430 struct wl_shell *shell;
1431
1432 shell = malloc(sizeof *shell);
1433 if (shell == NULL)
1434 return -1;
1435
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001436 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001437 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001438 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001439 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001440 shell->shell.map = map;
1441 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001442 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001443
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001444 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001445 wl_list_init(&shell->backgrounds);
1446 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001447 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001448
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001449 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001450
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001451 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1452 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001453 return -1;
1454
Kristian Høgsberg75840622011-09-06 13:48:16 -04001455 if (wl_display_add_global(ec->wl_display,
1456 &desktop_shell_interface,
1457 shell, bind_desktop_shell) == NULL)
1458 return -1;
1459
Pekka Paalanen6e168112011-11-24 11:34:05 +02001460 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1461 shell, bind_screensaver) == NULL)
1462 return -1;
1463
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001464 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001465 if (launch_desktop_shell_process(shell) != 0)
1466 return -1;
1467
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001469 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001471 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001472 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001473 MODIFIER_CTRL | MODIFIER_ALT,
1474 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001475 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001476 click_to_activate_binding, ec);
1477
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001478 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001479
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001480 return 0;
1481}