blob: 86284627bffedc02d3ad04769c3d0f7c7f0fc5f6 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
43 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
45 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020048 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020049
50 unsigned deathcount;
51 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020053
54 bool locked;
55 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020056
Pekka Paalanen068ae942011-11-28 14:11:15 +020057 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050058 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020059 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060
61 struct wl_list backgrounds;
62 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020063
Pekka Paalanen77346a62011-11-30 16:26:35 +020064 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020065 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020066 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020067 struct wl_resource *binding;
68 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020070 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040071};
72
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050073enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020074 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050075
Pekka Paalanen57da4a82011-11-23 16:42:16 +020076 SHELL_SURFACE_PANEL,
77 SHELL_SURFACE_BACKGROUND,
78 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020079 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080
81 SHELL_SURFACE_TOPLEVEL,
82 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050083 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080084 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050085 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086};
87
Pekka Paalanen56cdea92011-11-23 16:14:12 +020088struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020089 struct wl_resource resource;
90
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020092 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020094
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050095 enum shell_surface_type type;
96 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050098 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +020099 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500100 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200101 } rotation;
102
103 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700104 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500105 uint32_t time;
106 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200107 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500108 int32_t initial_up;
109 } popup;
110
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500111 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100113 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200114};
115
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116struct weston_move_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700117 struct wl_pointer_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500118 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500119 int32_t dx, dy;
120};
121
Pekka Paalanen460099f2012-01-20 16:48:25 +0200122struct rotate_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700123 struct wl_pointer_grab grab;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200124 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500125 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126 struct {
127 int32_t x;
128 int32_t y;
129 } center;
130};
131
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500132static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200133shell_configuration(struct wl_shell *shell)
134{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200135 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200136 char *path = NULL;
137 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200138
139 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200140 { "path", CONFIG_KEY_STRING, &path },
141 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200142 };
143
144 struct config_section cs[] = {
145 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
146 };
147
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500148 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500149 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200150 free(config_file);
151
Pekka Paalanen7296e792011-12-07 16:22:00 +0200152 shell->screensaver.path = path;
153 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200154}
155
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200156static void
Scott Moreau447013d2012-02-18 05:05:29 -0700157noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500158 struct wl_surface *surface, int32_t x, int32_t y)
159{
160 grab->focus = NULL;
161}
162
163static void
Scott Moreau447013d2012-02-18 05:05:29 -0700164move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500165 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500168 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500169 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500170
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500171 weston_surface_configure(es,
172 device->x + move->dx,
173 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200174 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500175}
176
177static void
Scott Moreau447013d2012-02-18 05:05:29 -0700178move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500179 uint32_t time, int32_t button, int32_t state)
180{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500181 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500183 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700184 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500185 free(grab);
186 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500187}
188
Scott Moreau447013d2012-02-18 05:05:29 -0700189static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500190 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500191 move_grab_motion,
192 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193};
194
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400195static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500196weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500197 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500200
201 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400202 if (!move)
203 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500204
205 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200206 move->dx = es->geometry.x - wd->input_device.grab_x;
207 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500208 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209
Scott Moreau447013d2012-02-18 05:05:29 -0700210 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500211
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200212 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400213
214 return 0;
215}
216
217static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200218shell_surface_move(struct wl_client *client, struct wl_resource *resource,
219 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200222 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400223
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500224 if (wd->input_device.button_count == 0 ||
225 wd->input_device.grab_time != time ||
226 wd->input_device.pointer_focus != &shsurf->surface->surface)
227 return;
228
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400230 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500231}
232
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233struct weston_resize_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700234 struct wl_pointer_grab grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500235 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200236 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200237 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500238};
239
240static void
Scott Moreau447013d2012-02-18 05:05:29 -0700241resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242 uint32_t time, int32_t x, int32_t y)
243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500244 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500246 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200247 int32_t from_x, from_y;
248 int32_t to_x, to_y;
249
250 weston_surface_from_global(resize->shsurf->surface,
251 device->grab_x, device->grab_y,
252 &from_x, &from_y);
253 weston_surface_from_global(resize->shsurf->surface,
254 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500255
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200256 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200257 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200258 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200259 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260 } else {
261 width = resize->width;
262 }
263
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200264 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200265 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200266 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200267 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268 } else {
269 height = resize->height;
270 }
271
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200272 wl_resource_post_event(&resize->shsurf->resource,
273 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
274 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275}
276
277static void
Scott Moreau447013d2012-02-18 05:05:29 -0700278resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279 uint32_t time, int32_t button, int32_t state)
280{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500281 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500282
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500283 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700284 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500285 free(grab);
286 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287}
288
Scott Moreau447013d2012-02-18 05:05:29 -0700289static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500290 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500291 resize_grab_motion,
292 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500293};
294
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400295static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500296weston_surface_resize(struct shell_surface *shsurf,
297 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200298 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500299{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500301
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500302 /* FIXME: Reject if fullscreen */
303
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200304 if (edges == 0 || edges > 15 ||
305 (edges & 3) == 3 || (edges & 12) == 12)
306 return 0;
307
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400309 if (!resize)
310 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311
312 resize->grab.interface = &resize_grab_interface;
313 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200314 resize->width = shsurf->surface->geometry.width;
315 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200316 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400317
Scott Moreau447013d2012-02-18 05:05:29 -0700318 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500319
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200320 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400321
322 return 0;
323}
324
325static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200326shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
327 struct wl_resource *input_resource, uint32_t time,
328 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400329{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500330 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200331 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400332
333 /* FIXME: Reject if fullscreen */
334
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500335 if (wd->input_device.button_count == 0 ||
336 wd->input_device.grab_time != time ||
337 wd->input_device.pointer_focus != &shsurf->surface->surface)
338 return;
339
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500340 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400341 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342}
343
Juan Zhao96879df2012-02-07 08:45:41 +0800344static struct weston_output *
345get_default_output(struct weston_compositor *compositor)
346{
347 return container_of(compositor->output_list.next,
348 struct weston_output, link);
349}
350
Pekka Paalanen98262232011-12-01 10:42:22 +0200351static int
352reset_shell_surface_type(struct shell_surface *surface)
353{
354 switch (surface->type) {
355 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200356 weston_surface_set_position(surface->surface,
357 surface->saved_x,
358 surface->saved_y);
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500359 surface->fullscreen_output = NULL;
Pekka Paalanen98262232011-12-01 10:42:22 +0200360 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800361 case SHELL_SURFACE_MAXIMIZED:
362 surface->output = get_default_output(surface->surface->compositor);
363 weston_surface_set_position(surface->surface,
364 surface->saved_x,
365 surface->saved_y);
366 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200367 case SHELL_SURFACE_PANEL:
368 case SHELL_SURFACE_BACKGROUND:
369 wl_list_remove(&surface->link);
370 wl_list_init(&surface->link);
371 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200372 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200373 case SHELL_SURFACE_LOCK:
374 wl_resource_post_error(&surface->resource,
375 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200376 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200377 return -1;
378 case SHELL_SURFACE_NONE:
379 case SHELL_SURFACE_TOPLEVEL:
380 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500381 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200382 break;
383 }
384
385 surface->type = SHELL_SURFACE_NONE;
386 return 0;
387}
388
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500389static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200390shell_surface_set_toplevel(struct wl_client *client,
391 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400392
393{
Pekka Paalanen98262232011-12-01 10:42:22 +0200394 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400395
Pekka Paalanen98262232011-12-01 10:42:22 +0200396 if (reset_shell_surface_type(surface))
397 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400398
Pekka Paalanen98262232011-12-01 10:42:22 +0200399 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400400}
401
402static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200403shell_surface_set_transient(struct wl_client *client,
404 struct wl_resource *resource,
405 struct wl_resource *parent_resource,
406 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400407{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200408 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500409 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200410 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400412
Pekka Paalanen98262232011-12-01 10:42:22 +0200413 if (reset_shell_surface_type(shsurf))
414 return;
415
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400416 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800417 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200418 weston_surface_set_position(es, pes->geometry.x + x,
419 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400420
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200421 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400422}
423
Juan Zhao96879df2012-02-07 08:45:41 +0800424static struct wl_shell *
425shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200426{
Juan Zhao96879df2012-02-07 08:45:41 +0800427 struct weston_surface *es = shsurf->surface;
428 struct weston_shell *shell = es->compositor->shell;
429
430 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
431}
432
433static int
434get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
435{
436 struct shell_surface *priv;
437 int panel_height = 0;
438
439 if (!output)
440 return 0;
441
442 wl_list_for_each(priv, &wlshell->panels, link) {
443 if (priv->output == output) {
444 panel_height = priv->surface->geometry.height;
445 break;
446 }
447 }
448 return panel_height;
449}
450
451static void
452shell_surface_set_maximized(struct wl_client *client,
453 struct wl_resource *resource,
454 struct wl_resource *output_resource )
455{
456 struct shell_surface *shsurf = resource->data;
457 struct weston_surface *es = shsurf->surface;
458 struct wl_shell *wlshell = NULL;
459 uint32_t edges = 0, panel_height = 0;
460
461 /* get the default output, if the client set it as NULL
462 check whether the ouput is available */
463 if (output_resource)
464 shsurf->output = output_resource->data;
465 else
466 shsurf->output = get_default_output(es->compositor);
467
468 if (reset_shell_surface_type(shsurf))
469 return;
470
471 shsurf->saved_x = es->geometry.x;
472 shsurf->saved_y = es->geometry.y;
473
474 wlshell = shell_surface_get_shell(shsurf);
475 panel_height = get_output_panel_height(wlshell, es->output);
476 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
477 wl_resource_post_event(&shsurf->resource,
478 WL_SHELL_SURFACE_CONFIGURE,
479 weston_compositor_get_time(), edges,
480 es->output->current->width,
481 es->output->current->height - panel_height);
482
483 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200484}
485
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400486static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200487shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500488 struct wl_resource *resource,
489 uint32_t method,
490 uint32_t framerate,
491 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400492{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200493 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500494 struct weston_surface *es = shsurf->surface;
495 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400496
Pekka Paalanen98262232011-12-01 10:42:22 +0200497 if (reset_shell_surface_type(shsurf))
498 return;
499
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400500 /* FIXME: Fullscreen on first output */
501 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200502 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400503
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200504 shsurf->saved_x = es->geometry.x;
505 shsurf->saved_y = es->geometry.y;
Alex Wu88277d12012-02-22 14:50:46 +0800506 shsurf->output = output;
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500507 shsurf->fullscreen_output = output;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200508 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500509
510 wl_resource_post_event(resource,
511 WL_SHELL_SURFACE_CONFIGURE,
512 weston_compositor_get_time(), 0,
Alex Wu88277d12012-02-22 14:50:46 +0800513 shsurf->output->current->width,
514 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400515}
516
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500517static void
Scott Moreau447013d2012-02-18 05:05:29 -0700518popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500519 struct wl_surface *surface, int32_t x, int32_t y)
520{
521 struct wl_input_device *device = grab->input_device;
522 struct shell_surface *priv =
523 container_of(grab, struct shell_surface, popup.grab);
524 struct wl_client *client = priv->surface->surface.resource.client;
525
Pekka Paalanencb108432012-01-19 16:25:40 +0200526 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200527 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500528 grab->focus = surface;
529 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200530 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500531 grab->focus = NULL;
532 }
533}
534
535static void
Scott Moreau447013d2012-02-18 05:05:29 -0700536popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200537 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500538{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500539 struct wl_resource *resource;
540
541 resource = grab->input_device->pointer_focus_resource;
542 if (resource)
543 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200544 time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500545}
546
547static void
Scott Moreau447013d2012-02-18 05:05:29 -0700548popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500549 uint32_t time, int32_t button, int32_t state)
550{
551 struct wl_resource *resource;
552 struct shell_surface *shsurf =
553 container_of(grab, struct shell_surface, popup.grab);
554
555 resource = grab->input_device->pointer_focus_resource;
556 if (resource) {
557 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
558 time, button, state);
559 } else if (state == 0 &&
560 (shsurf->popup.initial_up ||
561 time - shsurf->popup.time > 500)) {
562 wl_resource_post_event(&shsurf->resource,
563 WL_SHELL_SURFACE_POPUP_DONE);
Scott Moreau447013d2012-02-18 05:05:29 -0700564 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500565 shsurf->popup.grab.input_device = NULL;
566 }
567
568 if (state == 0)
569 shsurf->popup.initial_up = 1;
570}
571
Scott Moreau447013d2012-02-18 05:05:29 -0700572static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500573 popup_grab_focus,
574 popup_grab_motion,
575 popup_grab_button,
576};
577
578static void
579shell_map_popup(struct shell_surface *shsurf, uint32_t time)
580{
581 struct wl_input_device *device;
582 struct weston_surface *es = shsurf->surface;
583 struct weston_surface *parent = shsurf->parent->surface;
584
585 es->output = parent->output;
586
587 shsurf->popup.grab.interface = &popup_grab_interface;
588 device = es->compositor->input_device;
589
Pekka Paalanen938269a2012-02-07 14:19:01 +0200590 weston_surface_update_transform(parent);
591 if (parent->transform.enabled) {
592 shsurf->popup.parent_transform.matrix =
593 parent->transform.matrix;
594 } else {
595 /* construct x, y translation matrix */
596 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
597 shsurf->popup.parent_transform.matrix.d[12] =
598 parent->geometry.x;
599 shsurf->popup.parent_transform.matrix.d[13] =
600 parent->geometry.y;
601 }
602 wl_list_insert(es->geometry.transformation_list.prev,
603 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200604 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500605
606 shsurf->popup.grab.input_device = device;
607 shsurf->popup.time = device->grab_time;
608 shsurf->popup.initial_up = 0;
609
Scott Moreau447013d2012-02-18 05:05:29 -0700610 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500611 &shsurf->popup.grab, shsurf->popup.time);
612}
613
614static void
615shell_surface_set_popup(struct wl_client *client,
616 struct wl_resource *resource,
617 struct wl_resource *input_device_resource,
618 uint32_t time,
619 struct wl_resource *parent_resource,
620 int32_t x, int32_t y, uint32_t flags)
621{
622 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500623
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500624 shsurf->type = SHELL_SURFACE_POPUP;
625 shsurf->parent = parent_resource->data;
626 shsurf->popup.x = x;
627 shsurf->popup.y = y;
628}
629
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200630static const struct wl_shell_surface_interface shell_surface_implementation = {
631 shell_surface_move,
632 shell_surface_resize,
633 shell_surface_set_toplevel,
634 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500635 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800636 shell_surface_set_popup,
637 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200638};
639
640static void
641destroy_shell_surface(struct wl_resource *resource)
642{
643 struct shell_surface *shsurf = resource->data;
644
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500645 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700646 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500647
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200648 /* in case cleaning up a dead client destroys shell_surface first */
649 if (shsurf->surface)
650 wl_list_remove(&shsurf->surface_destroy_listener.link);
651
652 wl_list_remove(&shsurf->link);
653 free(shsurf);
654}
655
656static void
657shell_handle_surface_destroy(struct wl_listener *listener,
658 struct wl_resource *resource, uint32_t time)
659{
660 struct shell_surface *shsurf = container_of(listener,
661 struct shell_surface,
662 surface_destroy_listener);
663
664 shsurf->surface = NULL;
665 wl_resource_destroy(&shsurf->resource, time);
666}
667
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200668static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500669get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200670{
671 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
672 struct wl_listener *listener;
673
674 /* search the destroy listener list for our callback */
675 wl_list_for_each(listener, lst, link) {
676 if (listener->func == shell_handle_surface_destroy) {
677 return container_of(listener, struct shell_surface,
678 surface_destroy_listener);
679 }
680 }
681
682 return NULL;
683}
684
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200685static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200686shell_get_shell_surface(struct wl_client *client,
687 struct wl_resource *resource,
688 uint32_t id,
689 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200690{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500691 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200692 struct shell_surface *shsurf;
693
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200694 if (get_shell_surface(surface)) {
695 wl_resource_post_error(surface_resource,
696 WL_DISPLAY_ERROR_INVALID_OBJECT,
697 "wl_shell::get_shell_surface already requested");
698 return;
699 }
700
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200701 shsurf = calloc(1, sizeof *shsurf);
702 if (!shsurf) {
703 wl_resource_post_no_memory(resource);
704 return;
705 }
706
707 shsurf->resource.destroy = destroy_shell_surface;
708 shsurf->resource.object.id = id;
709 shsurf->resource.object.interface = &wl_shell_surface_interface;
710 shsurf->resource.object.implementation =
711 (void (**)(void)) &shell_surface_implementation;
712 shsurf->resource.data = shsurf;
713
714 shsurf->surface = surface;
715 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
716 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
717 &shsurf->surface_destroy_listener.link);
718
719 /* init link so its safe to always remove it in destroy_shell_surface */
720 wl_list_init(&shsurf->link);
721
Pekka Paalanen460099f2012-01-20 16:48:25 +0200722 /* empty when not in use */
723 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500724 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200725
Pekka Paalanen98262232011-12-01 10:42:22 +0200726 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200727
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200728 wl_client_add_resource(client, &shsurf->resource);
729}
730
731static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200732 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500733};
734
Kristian Høgsberg07937562011-04-12 17:25:42 -0400735static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500736handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200737{
738 proc->pid = 0;
739}
740
741static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200742launch_screensaver(struct wl_shell *shell)
743{
744 if (shell->screensaver.binding)
745 return;
746
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200747 if (!shell->screensaver.path)
748 return;
749
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500750 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200751 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200752 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200753 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200754}
755
756static void
757terminate_screensaver(struct wl_shell *shell)
758{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200759 if (shell->screensaver.process.pid == 0)
760 return;
761
762 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200763}
764
765static void
766show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
767{
768 struct wl_list *list;
769
770 if (shell->lock_surface)
771 list = &shell->lock_surface->surface->link;
772 else
773 list = &shell->compositor->surface_list;
774
775 wl_list_remove(&surface->surface->link);
776 wl_list_insert(list, &surface->surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200777 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200778 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200779}
780
781static void
782hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
783{
784 wl_list_remove(&surface->surface->link);
785 wl_list_init(&surface->surface->link);
786 surface->surface->output = NULL;
787}
788
789static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400790desktop_shell_set_background(struct wl_client *client,
791 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100792 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400793 struct wl_resource *surface_resource)
794{
795 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200796 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500797 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200798 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400799
Pekka Paalanen98262232011-12-01 10:42:22 +0200800 if (reset_shell_surface_type(shsurf))
801 return;
802
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100803 wl_list_for_each(priv, &shell->backgrounds, link) {
804 if (priv->output == output_resource->data) {
805 priv->surface->output = NULL;
806 wl_list_remove(&priv->surface->link);
807 wl_list_remove(&priv->link);
808 break;
809 }
810 }
811
Pekka Paalanen068ae942011-11-28 14:11:15 +0200812 shsurf->type = SHELL_SURFACE_BACKGROUND;
813 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100814
Pekka Paalanen068ae942011-11-28 14:11:15 +0200815 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100816
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200817 weston_surface_set_position(surface, shsurf->output->x,
818 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200819
Kristian Høgsberg75840622011-09-06 13:48:16 -0400820 wl_resource_post_event(resource,
821 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500822 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200823 shsurf->output->current->width,
824 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400825}
826
827static void
828desktop_shell_set_panel(struct wl_client *client,
829 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100830 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400831 struct wl_resource *surface_resource)
832{
833 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200834 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200836 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400837
Pekka Paalanen98262232011-12-01 10:42:22 +0200838 if (reset_shell_surface_type(shsurf))
839 return;
840
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100841 wl_list_for_each(priv, &shell->panels, link) {
842 if (priv->output == output_resource->data) {
843 priv->surface->output = NULL;
844 wl_list_remove(&priv->surface->link);
845 wl_list_remove(&priv->link);
846 break;
847 }
848 }
849
Pekka Paalanen068ae942011-11-28 14:11:15 +0200850 shsurf->type = SHELL_SURFACE_PANEL;
851 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100852
Pekka Paalanen068ae942011-11-28 14:11:15 +0200853 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100854
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200855 weston_surface_set_position(surface, shsurf->output->x,
856 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200857
Kristian Høgsberg75840622011-09-06 13:48:16 -0400858 wl_resource_post_event(resource,
859 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500860 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200861 shsurf->output->current->width,
862 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400863}
864
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200865static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500866handle_lock_surface_destroy(struct wl_listener *listener,
867 struct wl_resource *resource, uint32_t time)
868{
869 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200870 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500871
872 fprintf(stderr, "lock surface gone\n");
873 shell->lock_surface = NULL;
874}
875
876static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200877desktop_shell_set_lock_surface(struct wl_client *client,
878 struct wl_resource *resource,
879 struct wl_resource *surface_resource)
880{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200881 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200882 struct shell_surface *surface = surface_resource->data;
883
884 if (reset_shell_surface_type(surface))
885 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200886
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200887 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200888
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200889 if (!shell->locked)
890 return;
891
Pekka Paalanen98262232011-12-01 10:42:22 +0200892 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200893
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500894 shell->lock_surface_listener.func = handle_lock_surface_destroy;
895 wl_list_insert(&surface_resource->destroy_listener_list,
896 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200897
Pekka Paalanen068ae942011-11-28 14:11:15 +0200898 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200899}
900
901static void
902resume_desktop(struct wl_shell *shell)
903{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500904 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200905 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200906 struct shell_surface *tmp;
907
908 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
909 hide_screensaver(shell, tmp);
910
911 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200912
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200913 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200914 weston_surface_assign_output(surface);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200915
Pekka Paalanenfe340832011-11-25 16:07:52 +0200916 if (wl_list_empty(&shell->backgrounds)) {
917 list = &shell->compositor->surface_list;
918 } else {
919 struct shell_surface *background;
920 background = container_of(shell->backgrounds.prev,
921 struct shell_surface, link);
922 list = background->surface->link.prev;
923 }
924
925 if (!wl_list_empty(&shell->hidden_surface_list))
926 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500927 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200928
929 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500930 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200931 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500932 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200933 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200934}
935
936static void
937desktop_shell_unlock(struct wl_client *client,
938 struct wl_resource *resource)
939{
940 struct wl_shell *shell = resource->data;
941
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200942 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200943
944 if (shell->locked)
945 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200946}
947
Kristian Høgsberg75840622011-09-06 13:48:16 -0400948static const struct desktop_shell_interface desktop_shell_implementation = {
949 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200950 desktop_shell_set_panel,
951 desktop_shell_set_lock_surface,
952 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400953};
954
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200955static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500956get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200957{
958 struct shell_surface *shsurf;
959
960 shsurf = get_shell_surface(surface);
961 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200962 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200963 return shsurf->type;
964}
965
Kristian Høgsberg75840622011-09-06 13:48:16 -0400966static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400967move_binding(struct wl_input_device *device, uint32_t time,
968 uint32_t key, uint32_t button, uint32_t state, void *data)
969{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500970 struct weston_surface *surface =
971 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500972
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100973 if (surface == NULL)
974 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400975
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200976 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100977 case SHELL_SURFACE_PANEL:
978 case SHELL_SURFACE_BACKGROUND:
979 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200980 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100981 return;
982 default:
983 break;
984 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400985
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500986 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400987}
988
989static void
990resize_binding(struct wl_input_device *device, uint32_t time,
991 uint32_t key, uint32_t button, uint32_t state, void *data)
992{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500993 struct weston_surface *surface =
994 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400995 uint32_t edges = 0;
996 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200997 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500998
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100999 if (surface == NULL)
1000 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001001
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001002 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001003 if (!shsurf)
1004 return;
1005
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001006 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001007 case SHELL_SURFACE_PANEL:
1008 case SHELL_SURFACE_BACKGROUND:
1009 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001010 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001011 return;
1012 default:
1013 break;
1014 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001015
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001016 weston_surface_from_global(surface,
1017 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001018
Pekka Paalanen60921e52012-01-25 15:55:43 +02001019 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001020 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001021 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001022 edges |= 0;
1023 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001024 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001025
Pekka Paalanen60921e52012-01-25 15:55:43 +02001026 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001027 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001028 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001029 edges |= 0;
1030 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001031 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001032
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001033 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001034 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001035}
1036
1037static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001038terminate_binding(struct wl_input_device *device, uint32_t time,
1039 uint32_t key, uint32_t button, uint32_t state, void *data)
1040{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001041 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001042
1043 if (state)
1044 wl_display_terminate(compositor->wl_display);
1045}
1046
1047static void
Scott Moreau447013d2012-02-18 05:05:29 -07001048rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001049 uint32_t time, int32_t x, int32_t y)
1050{
1051 struct rotate_grab *rotate =
1052 container_of(grab, struct rotate_grab, grab);
1053 struct wl_input_device *device = grab->input_device;
1054 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001055 GLfloat cx = 0.5f * surface->surface->geometry.width;
1056 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001057 GLfloat dx, dy;
1058 GLfloat r;
1059
1060 dx = device->x - rotate->center.x;
1061 dy = device->y - rotate->center.y;
1062 r = sqrtf(dx * dx + dy * dy);
1063
1064 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001065 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001066
1067 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001068 struct weston_matrix *matrix =
1069 &surface->rotation.transform.matrix;
1070
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001071 weston_matrix_init(&rotate->rotation);
1072 rotate->rotation.d[0] = dx / r;
1073 rotate->rotation.d[4] = -dy / r;
1074 rotate->rotation.d[1] = -rotate->rotation.d[4];
1075 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001076
1077 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001078 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001079 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1080 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001081 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001082
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001083 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001084 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001085 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001086 } else {
1087 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001088 weston_matrix_init(&surface->rotation.rotation);
1089 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001090 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001091
1092 /* Repaint implies weston_surface_update_transform(), which
1093 * lazily applies the damage due to rotation update.
1094 */
1095 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001096}
1097
1098static void
Scott Moreau447013d2012-02-18 05:05:29 -07001099rotate_grab_button(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001100 uint32_t time, int32_t button, int32_t state)
1101{
1102 struct rotate_grab *rotate =
1103 container_of(grab, struct rotate_grab, grab);
1104 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001105 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001106
1107 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001108 weston_matrix_multiply(&surface->rotation.rotation,
1109 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001110 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001111 free(rotate);
1112 }
1113}
1114
Scott Moreau447013d2012-02-18 05:05:29 -07001115static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001116 noop_grab_focus,
1117 rotate_grab_motion,
1118 rotate_grab_button,
1119};
1120
1121static void
1122rotate_binding(struct wl_input_device *device, uint32_t time,
1123 uint32_t key, uint32_t button, uint32_t state, void *data)
1124{
1125 struct weston_surface *base_surface =
1126 (struct weston_surface *) device->pointer_focus;
1127 struct shell_surface *surface;
1128 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001129 GLfloat dx, dy;
1130 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001131
1132 if (base_surface == NULL)
1133 return;
1134
1135 surface = get_shell_surface(base_surface);
1136 if (!surface)
1137 return;
1138
1139 switch (surface->type) {
1140 case SHELL_SURFACE_PANEL:
1141 case SHELL_SURFACE_BACKGROUND:
1142 case SHELL_SURFACE_FULLSCREEN:
1143 case SHELL_SURFACE_SCREENSAVER:
1144 return;
1145 default:
1146 break;
1147 }
1148
Pekka Paalanen460099f2012-01-20 16:48:25 +02001149 rotate = malloc(sizeof *rotate);
1150 if (!rotate)
1151 return;
1152
1153 rotate->grab.interface = &rotate_grab_interface;
1154 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001155
1156 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001157 surface->surface->geometry.width / 2,
1158 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001159 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001160
Scott Moreau447013d2012-02-18 05:05:29 -07001161 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001162
1163 dx = device->x - rotate->center.x;
1164 dy = device->y - rotate->center.y;
1165 r = sqrtf(dx * dx + dy * dy);
1166 if (r > 20.0f) {
1167 struct weston_matrix inverse;
1168
1169 weston_matrix_init(&inverse);
1170 inverse.d[0] = dx / r;
1171 inverse.d[4] = dy / r;
1172 inverse.d[1] = -inverse.d[4];
1173 inverse.d[5] = inverse.d[0];
1174 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1175 } else {
1176 weston_matrix_init(&surface->rotation.rotation);
1177 weston_matrix_init(&rotate->rotation);
1178 }
1179
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001180 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001181}
1182
1183static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001184activate(struct weston_shell *base, struct weston_surface *es,
1185 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001186{
1187 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001188 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001189 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001190
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001191 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001192
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001193 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001194 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001195
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001196 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001197 case SHELL_SURFACE_BACKGROUND:
1198 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001199 wl_list_remove(&es->link);
1200 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001201 break;
1202 case SHELL_SURFACE_PANEL:
1203 /* already put on top */
1204 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001205 case SHELL_SURFACE_SCREENSAVER:
1206 /* always below lock surface */
1207 if (shell->lock_surface) {
1208 wl_list_remove(&es->link);
1209 wl_list_insert(&shell->lock_surface->surface->link,
1210 &es->link);
1211 }
1212 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001213 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001214 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001215 list = weston_compositor_top(compositor);
1216
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001217 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001218 struct shell_surface *panel;
1219 wl_list_for_each(panel, &shell->panels, link) {
1220 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001221 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001222 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001223 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001224 }
1225}
1226
1227static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001228click_to_activate_binding(struct wl_input_device *device,
1229 uint32_t time, uint32_t key,
1230 uint32_t button, uint32_t state, void *data)
1231{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001232 struct weston_input_device *wd = (struct weston_input_device *) device;
1233 struct weston_compositor *compositor = data;
1234 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001235
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001236 focus = (struct weston_surface *) device->pointer_focus;
Scott Moreau447013d2012-02-18 05:05:29 -07001237 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001238 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001239}
1240
1241static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001242lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001243{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001244 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001245 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246 struct weston_surface *cur;
1247 struct weston_surface *tmp;
1248 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001249 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001250 uint32_t time;
1251
1252 if (shell->locked)
1253 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001254
1255 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001256
1257 /* Move all surfaces from compositor's list to our hidden list,
1258 * except the background. This way nothing else can show or
1259 * receive input events while we are locked. */
1260
1261 if (!wl_list_empty(&shell->hidden_surface_list)) {
1262 fprintf(stderr,
1263 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1264 __func__);
1265 }
1266
1267 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1268 /* skip input device sprites, cur->surface is uninitialised */
1269 if (cur->surface.resource.client == NULL)
1270 continue;
1271
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001272 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001273 continue;
1274
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001275 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001276 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001277 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001278 }
1279
Pekka Paalanen77346a62011-11-30 16:26:35 +02001280 launch_screensaver(shell);
1281
1282 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1283 show_screensaver(shell, shsurf);
1284
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001285 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001286 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001287 weston_compositor_wake(shell->compositor);
1288 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001289 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001290
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001291 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001293
1294 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001295 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001296 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1297 wl_input_device_set_keyboard_focus(&device->input_device,
1298 NULL, time);
1299 }
1300
1301 /* TODO: disable bindings that should not work while locked. */
1302
1303 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001304}
1305
1306static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001307unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001308{
1309 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1310
Pekka Paalanend81c2162011-11-16 13:47:34 +02001311 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001312 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001313 return;
1314 }
1315
1316 /* If desktop-shell client has gone away, unlock immediately. */
1317 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001318 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001319 return;
1320 }
1321
1322 if (shell->prepare_event_sent)
1323 return;
1324
1325 wl_resource_post_event(shell->child.desktop_shell,
1326 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1327 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001328}
1329
1330static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001331center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001332{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001333 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001334 GLfloat x = (mode->width - surface->geometry.width) / 2;
1335 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001336
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001337 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001338}
1339
1340static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001341map(struct weston_shell *base, struct weston_surface *surface,
1342 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001343{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001344 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001345 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001346 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001347 struct shell_surface *shsurf;
1348 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1349 int do_configure;
Juan Zhao96879df2012-02-07 08:45:41 +08001350 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001351
Pekka Paalanen77346a62011-11-30 16:26:35 +02001352 shsurf = get_shell_surface(surface);
1353 if (shsurf)
1354 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001355
Pekka Paalanen77346a62011-11-30 16:26:35 +02001356 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001357 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001358 do_configure = 0;
1359 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001360 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001361 do_configure = 1;
1362 }
1363
Pekka Paalanen60921e52012-01-25 15:55:43 +02001364 surface->geometry.width = width;
1365 surface->geometry.height = height;
1366 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001367
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001368 weston_compositor_update_drag_surfaces(compositor);
1369
Pekka Paalanen77346a62011-11-30 16:26:35 +02001370 /* initial positioning, see also configure() */
1371 switch (surface_type) {
1372 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001373 weston_surface_set_position(surface, 10 + random() % 400,
1374 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001375 break;
1376 case SHELL_SURFACE_SCREENSAVER:
1377 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001378 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001379 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001380 case SHELL_SURFACE_MAXIMIZED:
1381 /*use surface configure to set the geometry*/
1382 panel_height = get_output_panel_height(shell,surface->output);
1383 weston_surface_set_position(surface, surface->output->x,
1384 surface->output->y + panel_height);
1385 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001386 case SHELL_SURFACE_LOCK:
1387 center_on_output(surface, get_default_output(compositor));
1388 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001389 case SHELL_SURFACE_POPUP:
1390 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001391 case SHELL_SURFACE_NONE:
1392 weston_surface_set_position(surface,
1393 surface->geometry.x + sx,
1394 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001395 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001396 default:
1397 ;
1398 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001399
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001400 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001401 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001402 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001403 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001404 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001405 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001406 break;
1407 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001408 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001409 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001410 break;
1411 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001412 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001413 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001414
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001415 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001416 do_configure = 1;
1417 break;
1418 case SHELL_SURFACE_SCREENSAVER:
1419 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001420 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001421 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001422 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001423 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001424 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001426 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001427 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001428 break;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001429 case SHELL_SURFACE_NONE:
Ander Conselvan de Oliveiraba727bd2012-02-23 13:29:25 +02001430 do_configure = 0;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001431 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001432 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001433 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001434 if (!wl_list_empty(&shell->panels)) {
1435 struct shell_surface *panel =
1436 container_of(shell->panels.prev,
1437 struct shell_surface, link);
1438 wl_list_insert(&panel->surface->link, &surface->link);
1439 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001440 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001441 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001442 }
1443
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001444 if (do_configure) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001445 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001446 weston_compositor_repick(compositor);
Juan Zhao96879df2012-02-07 08:45:41 +08001447 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1448 surface->output = shsurf->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001449 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001450
Juan Zhao7bb92f02011-12-15 11:31:51 -05001451 switch (surface_type) {
1452 case SHELL_SURFACE_TOPLEVEL:
1453 case SHELL_SURFACE_TRANSIENT:
1454 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001455 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001456 if (!shell->locked)
1457 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001458 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001459 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001460 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001461 break;
1462 default:
1463 break;
1464 }
1465
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001466 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001467 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001468}
1469
1470static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001471configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001472 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001473{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001474 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001475 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1476 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001477
Pekka Paalanen77346a62011-11-30 16:26:35 +02001478 shsurf = get_shell_surface(surface);
1479 if (shsurf)
1480 surface_type = shsurf->type;
1481
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001482 surface->geometry.x = x;
1483 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001484 surface->geometry.width = width;
1485 surface->geometry.height = height;
1486 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001487
1488 switch (surface_type) {
1489 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001490 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001491 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001492 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001493 case SHELL_SURFACE_MAXIMIZED:
1494 /*setting x, y and using configure to change that geometry*/
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001495 surface->geometry.x = surface->output->x;
1496 surface->geometry.y = surface->output->y +
1497 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001498 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001499 default:
1500 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001501 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001502
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001503 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001504 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001505 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001506
1507 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1508 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001509 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1510 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001511 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001512}
1513
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001514static int launch_desktop_shell_process(struct wl_shell *shell);
1515
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001516static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001517desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001518{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001519 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001520 struct wl_shell *shell =
1521 container_of(process, struct wl_shell, child.process);
1522
1523 shell->child.process.pid = 0;
1524 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001525
1526 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1527 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001528 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001529 shell->child.deathstamp = time;
1530 shell->child.deathcount = 0;
1531 }
1532
1533 shell->child.deathcount++;
1534 if (shell->child.deathcount > 5) {
1535 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1536 return;
1537 }
1538
1539 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1540 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001541}
1542
1543static int
1544launch_desktop_shell_process(struct wl_shell *shell)
1545{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001546 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001547
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001548 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001549 &shell->child.process,
1550 shell_exe,
1551 desktop_shell_sigchld);
1552
1553 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001554 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001555 return 0;
1556}
1557
1558static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001559bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1560{
1561 struct wl_shell *shell = data;
1562
1563 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001564 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001565}
1566
Kristian Høgsberg75840622011-09-06 13:48:16 -04001567static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001568unbind_desktop_shell(struct wl_resource *resource)
1569{
1570 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001571
1572 if (shell->locked)
1573 resume_desktop(shell);
1574
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001575 shell->child.desktop_shell = NULL;
1576 shell->prepare_event_sent = false;
1577 free(resource);
1578}
1579
1580static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001581bind_desktop_shell(struct wl_client *client,
1582 void *data, uint32_t version, uint32_t id)
1583{
1584 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001585 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001586
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001587 resource = wl_client_add_object(client, &desktop_shell_interface,
1588 &desktop_shell_implementation,
1589 id, shell);
1590
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001591 if (client == shell->child.client) {
1592 resource->destroy = unbind_desktop_shell;
1593 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001594 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001595 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001596
1597 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1598 "permission to bind desktop_shell denied");
1599 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001600}
1601
Pekka Paalanen6e168112011-11-24 11:34:05 +02001602static void
1603screensaver_set_surface(struct wl_client *client,
1604 struct wl_resource *resource,
1605 struct wl_resource *shell_surface_resource,
1606 struct wl_resource *output_resource)
1607{
1608 struct wl_shell *shell = resource->data;
1609 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001610 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001611
Pekka Paalanen98262232011-12-01 10:42:22 +02001612 if (reset_shell_surface_type(surface))
1613 return;
1614
Pekka Paalanen77346a62011-11-30 16:26:35 +02001615 surface->type = SHELL_SURFACE_SCREENSAVER;
1616
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001617 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001618 surface->output = output;
1619 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001620}
1621
1622static const struct screensaver_interface screensaver_implementation = {
1623 screensaver_set_surface
1624};
1625
1626static void
1627unbind_screensaver(struct wl_resource *resource)
1628{
1629 struct wl_shell *shell = resource->data;
1630
Pekka Paalanen77346a62011-11-30 16:26:35 +02001631 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001632 free(resource);
1633}
1634
1635static void
1636bind_screensaver(struct wl_client *client,
1637 void *data, uint32_t version, uint32_t id)
1638{
1639 struct wl_shell *shell = data;
1640 struct wl_resource *resource;
1641
1642 resource = wl_client_add_object(client, &screensaver_interface,
1643 &screensaver_implementation,
1644 id, shell);
1645
Pekka Paalanen77346a62011-11-30 16:26:35 +02001646 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001647 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001648 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001649 return;
1650 }
1651
1652 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1653 "interface object already bound");
1654 wl_resource_destroy(resource, 0);
1655}
1656
Kristian Høgsberg07045392012-02-19 18:52:44 -05001657struct switcher {
1658 struct weston_compositor *compositor;
1659 struct weston_surface *current;
1660 struct wl_listener listener;
1661 struct wl_keyboard_grab grab;
1662};
1663
1664static void
1665switcher_next(struct switcher *switcher)
1666{
1667 struct weston_compositor *compositor = switcher->compositor;
1668 struct weston_surface *surface;
1669 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1670
1671 wl_list_for_each(surface, &compositor->surface_list, link) {
1672 /* Workaround for cursor surfaces. */
1673 if (surface->surface.resource.destroy_listener_list.next == NULL)
1674 continue;
1675
1676 switch (get_shell_surface_type(surface)) {
1677 case SHELL_SURFACE_TOPLEVEL:
1678 case SHELL_SURFACE_FULLSCREEN:
1679 case SHELL_SURFACE_MAXIMIZED:
1680 if (first == NULL)
1681 first = surface;
1682 if (prev == switcher->current)
1683 next = surface;
1684 prev = surface;
1685 surface->alpha = 64;
1686 weston_surface_damage(surface);
1687 break;
1688 default:
1689 break;
1690 }
1691 }
1692
1693 if (next == NULL)
1694 next = first;
1695
1696 wl_list_remove(&switcher->listener.link);
1697 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1698 &switcher->listener.link);
1699
1700 switcher->current = next;
1701 next->alpha = 255;
1702}
1703
1704static void
1705switcher_handle_surface_destroy(struct wl_listener *listener,
1706 struct wl_resource *resource, uint32_t time)
1707{
1708 struct switcher *switcher =
1709 container_of(listener, struct switcher, listener);
1710
1711 switcher_next(switcher);
1712}
1713
1714static void
1715switcher_destroy(struct switcher *switcher, uint32_t time)
1716{
1717 struct weston_compositor *compositor = switcher->compositor;
1718 struct weston_surface *surface;
1719 struct weston_input_device *device =
1720 (struct weston_input_device *) switcher->grab.input_device;
1721
1722 wl_list_for_each(surface, &compositor->surface_list, link) {
1723 surface->alpha = 255;
1724 weston_surface_damage(surface);
1725 }
1726
1727 activate(compositor->shell, switcher->current, device, time);
1728 wl_list_remove(&switcher->listener.link);
1729 wl_input_device_end_keyboard_grab(&device->input_device, time);
1730 free(switcher);
1731}
1732
1733static void
1734switcher_key(struct wl_keyboard_grab *grab,
1735 uint32_t time, uint32_t key, int32_t state)
1736{
1737 struct switcher *switcher = container_of(grab, struct switcher, grab);
1738 struct weston_input_device *device =
1739 (struct weston_input_device *) grab->input_device;
1740
1741 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1742 switcher_destroy(switcher, time);
1743 } else if (key == KEY_TAB && state) {
1744 switcher_next(switcher);
1745 }
1746};
1747
1748static const struct wl_keyboard_grab_interface switcher_grab = {
1749 switcher_key
1750};
1751
1752static void
1753switcher_binding(struct wl_input_device *device, uint32_t time,
1754 uint32_t key, uint32_t button,
1755 uint32_t state, void *data)
1756{
1757 struct weston_compositor *compositor = data;
1758 struct switcher *switcher;
1759
1760 switcher = malloc(sizeof *switcher);
1761 switcher->compositor = compositor;
1762 switcher->current = NULL;
1763 switcher->listener.func = switcher_handle_surface_destroy;
1764 wl_list_init(&switcher->listener.link);
1765
1766 switcher->grab.interface = &switcher_grab;
1767 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1768 switcher_next(switcher);
1769}
1770
Pekka Paalanen3c647232011-12-22 13:43:43 +02001771static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001772shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001773{
1774 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1775
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001776 if (shell->child.client)
1777 wl_client_destroy(shell->child.client);
1778
Pekka Paalanen3c647232011-12-22 13:43:43 +02001779 free(shell->screensaver.path);
1780 free(shell);
1781}
1782
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001783int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001784shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001785
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001786WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001787shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001788{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001789 struct wl_shell *shell;
1790
1791 shell = malloc(sizeof *shell);
1792 if (shell == NULL)
1793 return -1;
1794
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001795 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001796 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001797 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001798 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001799 shell->shell.map = map;
1800 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001801 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001802
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001803 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001804 wl_list_init(&shell->backgrounds);
1805 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001806 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001807
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001808 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001809
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001810 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1811 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001812 return -1;
1813
Kristian Høgsberg75840622011-09-06 13:48:16 -04001814 if (wl_display_add_global(ec->wl_display,
1815 &desktop_shell_interface,
1816 shell, bind_desktop_shell) == NULL)
1817 return -1;
1818
Pekka Paalanen6e168112011-11-24 11:34:05 +02001819 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1820 shell, bind_screensaver) == NULL)
1821 return -1;
1822
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001823 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001824 if (launch_desktop_shell_process(shell) != 0)
1825 return -1;
1826
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001827 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001828 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001829 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001830 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001831 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001832 MODIFIER_CTRL | MODIFIER_ALT,
1833 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001834 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001835 click_to_activate_binding, ec);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001836 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1837 MODIFIER_SUPER | MODIFIER_ALT,
1838 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001839
Kristian Høgsberg07045392012-02-19 18:52:44 -05001840 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
1841 switcher_binding, ec);
1842
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001843 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001844
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001845 return 0;
1846}