blob: ee71dcc1339d52175f027a4f2af4d7e049e7521d [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
Scott Moreauccbf29d2012-02-22 14:21:41 -07001038zoom_in_binding(struct wl_input_device *device, uint32_t time,
1039 uint32_t key, uint32_t button, uint32_t state, void *data)
1040{
1041 struct weston_input_device *wd = (struct weston_input_device *) device;
1042 struct weston_compositor *compositor = wd->compositor;
1043 struct weston_output *output;
1044
1045 wl_list_for_each(output, &compositor->output_list, link) {
1046 if (pixman_region32_contains_point(&output->region,
1047 device->x, device->y, NULL)) {
1048 output->zoom.active = 1;
1049 output->zoom.level -= output->zoom.increment;
1050
1051 if (output->zoom.level < output->zoom.increment)
1052 output->zoom.level = output->zoom.increment;
1053
1054 weston_output_update_zoom(output, device->x, device->y);
1055 }
1056 }
1057}
1058
1059static void
1060zoom_out_binding(struct wl_input_device *device, uint32_t time,
1061 uint32_t key, uint32_t button, uint32_t state, void *data)
1062{
1063 struct weston_input_device *wd = (struct weston_input_device *) device;
1064 struct weston_compositor *compositor = wd->compositor;
1065 struct weston_output *output;
1066
1067 wl_list_for_each(output, &compositor->output_list, link) {
1068 if (pixman_region32_contains_point(&output->region,
1069 device->x, device->y, NULL)) {
1070 output->zoom.level += output->zoom.increment;
1071 if (output->zoom.level >= 1.0) {
1072 output->zoom.active = 0;
1073 output->zoom.level = 1.0;
1074 }
1075
1076 weston_output_update_zoom(output, device->x, device->y);
1077 }
1078 }
1079}
1080
1081static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001082terminate_binding(struct wl_input_device *device, uint32_t time,
1083 uint32_t key, uint32_t button, uint32_t state, void *data)
1084{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001085 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001086
1087 if (state)
1088 wl_display_terminate(compositor->wl_display);
1089}
1090
1091static void
Scott Moreau447013d2012-02-18 05:05:29 -07001092rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001093 uint32_t time, int32_t x, int32_t y)
1094{
1095 struct rotate_grab *rotate =
1096 container_of(grab, struct rotate_grab, grab);
1097 struct wl_input_device *device = grab->input_device;
1098 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001099 GLfloat cx = 0.5f * surface->surface->geometry.width;
1100 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001101 GLfloat dx, dy;
1102 GLfloat r;
1103
1104 dx = device->x - rotate->center.x;
1105 dy = device->y - rotate->center.y;
1106 r = sqrtf(dx * dx + dy * dy);
1107
1108 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001109 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001110
1111 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001112 struct weston_matrix *matrix =
1113 &surface->rotation.transform.matrix;
1114
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001115 weston_matrix_init(&rotate->rotation);
1116 rotate->rotation.d[0] = dx / r;
1117 rotate->rotation.d[4] = -dy / r;
1118 rotate->rotation.d[1] = -rotate->rotation.d[4];
1119 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001120
1121 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001122 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001123 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1124 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001125 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001126
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001127 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001128 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001129 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001130 } else {
1131 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001132 weston_matrix_init(&surface->rotation.rotation);
1133 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001134 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001135
1136 /* Repaint implies weston_surface_update_transform(), which
1137 * lazily applies the damage due to rotation update.
1138 */
1139 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001140}
1141
1142static void
Scott Moreau447013d2012-02-18 05:05:29 -07001143rotate_grab_button(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001144 uint32_t time, int32_t button, int32_t state)
1145{
1146 struct rotate_grab *rotate =
1147 container_of(grab, struct rotate_grab, grab);
1148 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001149 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001150
1151 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001152 weston_matrix_multiply(&surface->rotation.rotation,
1153 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001154 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001155 free(rotate);
1156 }
1157}
1158
Scott Moreau447013d2012-02-18 05:05:29 -07001159static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001160 noop_grab_focus,
1161 rotate_grab_motion,
1162 rotate_grab_button,
1163};
1164
1165static void
1166rotate_binding(struct wl_input_device *device, uint32_t time,
1167 uint32_t key, uint32_t button, uint32_t state, void *data)
1168{
1169 struct weston_surface *base_surface =
1170 (struct weston_surface *) device->pointer_focus;
1171 struct shell_surface *surface;
1172 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001173 GLfloat dx, dy;
1174 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001175
1176 if (base_surface == NULL)
1177 return;
1178
1179 surface = get_shell_surface(base_surface);
1180 if (!surface)
1181 return;
1182
1183 switch (surface->type) {
1184 case SHELL_SURFACE_PANEL:
1185 case SHELL_SURFACE_BACKGROUND:
1186 case SHELL_SURFACE_FULLSCREEN:
1187 case SHELL_SURFACE_SCREENSAVER:
1188 return;
1189 default:
1190 break;
1191 }
1192
Pekka Paalanen460099f2012-01-20 16:48:25 +02001193 rotate = malloc(sizeof *rotate);
1194 if (!rotate)
1195 return;
1196
1197 rotate->grab.interface = &rotate_grab_interface;
1198 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001199
1200 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001201 surface->surface->geometry.width / 2,
1202 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001203 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001204
Scott Moreau447013d2012-02-18 05:05:29 -07001205 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001206
1207 dx = device->x - rotate->center.x;
1208 dy = device->y - rotate->center.y;
1209 r = sqrtf(dx * dx + dy * dy);
1210 if (r > 20.0f) {
1211 struct weston_matrix inverse;
1212
1213 weston_matrix_init(&inverse);
1214 inverse.d[0] = dx / r;
1215 inverse.d[4] = dy / r;
1216 inverse.d[1] = -inverse.d[4];
1217 inverse.d[5] = inverse.d[0];
1218 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1219 } else {
1220 weston_matrix_init(&surface->rotation.rotation);
1221 weston_matrix_init(&rotate->rotation);
1222 }
1223
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001224 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001225}
1226
1227static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001228activate(struct weston_shell *base, struct weston_surface *es,
1229 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001230{
1231 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001232 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001233 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001234
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001236
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001237 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001238 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001239
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001240 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001241 case SHELL_SURFACE_BACKGROUND:
1242 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001243 wl_list_remove(&es->link);
1244 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001245 break;
1246 case SHELL_SURFACE_PANEL:
1247 /* already put on top */
1248 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001249 case SHELL_SURFACE_SCREENSAVER:
1250 /* always below lock surface */
1251 if (shell->lock_surface) {
1252 wl_list_remove(&es->link);
1253 wl_list_insert(&shell->lock_surface->surface->link,
1254 &es->link);
1255 }
1256 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001257 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001258 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001259 list = weston_compositor_top(compositor);
1260
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001261 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001262 struct shell_surface *panel;
1263 wl_list_for_each(panel, &shell->panels, link) {
1264 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001265 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001266 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001267 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001268 }
1269}
1270
1271static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001272click_to_activate_binding(struct wl_input_device *device,
1273 uint32_t time, uint32_t key,
1274 uint32_t button, uint32_t state, void *data)
1275{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001276 struct weston_input_device *wd = (struct weston_input_device *) device;
1277 struct weston_compositor *compositor = data;
1278 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001279
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001280 focus = (struct weston_surface *) device->pointer_focus;
Scott Moreau447013d2012-02-18 05:05:29 -07001281 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001282 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001283}
1284
1285static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001286lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001287{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001288 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001289 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001290 struct weston_surface *cur;
1291 struct weston_surface *tmp;
1292 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001293 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001294 uint32_t time;
1295
1296 if (shell->locked)
1297 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001298
1299 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001300
1301 /* Move all surfaces from compositor's list to our hidden list,
1302 * except the background. This way nothing else can show or
1303 * receive input events while we are locked. */
1304
1305 if (!wl_list_empty(&shell->hidden_surface_list)) {
1306 fprintf(stderr,
1307 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1308 __func__);
1309 }
1310
1311 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1312 /* skip input device sprites, cur->surface is uninitialised */
1313 if (cur->surface.resource.client == NULL)
1314 continue;
1315
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001316 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001317 continue;
1318
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001319 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001320 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001321 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001322 }
1323
Pekka Paalanen77346a62011-11-30 16:26:35 +02001324 launch_screensaver(shell);
1325
1326 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1327 show_screensaver(shell, shsurf);
1328
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001329 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001330 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001331 weston_compositor_wake(shell->compositor);
1332 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001333 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001334
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001335 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001336 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001337
1338 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001339 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001340 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1341 wl_input_device_set_keyboard_focus(&device->input_device,
1342 NULL, time);
1343 }
1344
1345 /* TODO: disable bindings that should not work while locked. */
1346
1347 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001348}
1349
1350static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001351unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001352{
1353 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1354
Pekka Paalanend81c2162011-11-16 13:47:34 +02001355 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001356 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001357 return;
1358 }
1359
1360 /* If desktop-shell client has gone away, unlock immediately. */
1361 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001362 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001363 return;
1364 }
1365
1366 if (shell->prepare_event_sent)
1367 return;
1368
1369 wl_resource_post_event(shell->child.desktop_shell,
1370 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1371 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001372}
1373
1374static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001376{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001377 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001378 GLfloat x = (mode->width - surface->geometry.width) / 2;
1379 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001380
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001381 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001382}
1383
1384static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001385map(struct weston_shell *base, struct weston_surface *surface,
1386 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001387{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001388 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001389 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001390 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001391 struct shell_surface *shsurf;
1392 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1393 int do_configure;
Juan Zhao96879df2012-02-07 08:45:41 +08001394 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001395
Pekka Paalanen77346a62011-11-30 16:26:35 +02001396 shsurf = get_shell_surface(surface);
1397 if (shsurf)
1398 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001399
Pekka Paalanen77346a62011-11-30 16:26:35 +02001400 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001401 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001402 do_configure = 0;
1403 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001404 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001405 do_configure = 1;
1406 }
1407
Pekka Paalanen60921e52012-01-25 15:55:43 +02001408 surface->geometry.width = width;
1409 surface->geometry.height = height;
1410 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001411
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001412 weston_compositor_update_drag_surfaces(compositor);
1413
Pekka Paalanen77346a62011-11-30 16:26:35 +02001414 /* initial positioning, see also configure() */
1415 switch (surface_type) {
1416 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001417 weston_surface_set_position(surface, 10 + random() % 400,
1418 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001419 break;
1420 case SHELL_SURFACE_SCREENSAVER:
1421 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001422 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001423 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001424 case SHELL_SURFACE_MAXIMIZED:
1425 /*use surface configure to set the geometry*/
1426 panel_height = get_output_panel_height(shell,surface->output);
1427 weston_surface_set_position(surface, surface->output->x,
1428 surface->output->y + panel_height);
1429 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001430 case SHELL_SURFACE_LOCK:
1431 center_on_output(surface, get_default_output(compositor));
1432 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001433 case SHELL_SURFACE_POPUP:
1434 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001435 case SHELL_SURFACE_NONE:
1436 weston_surface_set_position(surface,
1437 surface->geometry.x + sx,
1438 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001439 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001440 default:
1441 ;
1442 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001443
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001444 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001445 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001446 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001447 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001448 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001449 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001450 break;
1451 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001452 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001453 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001454 break;
1455 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001456 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001457 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001458
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001459 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001460 do_configure = 1;
1461 break;
1462 case SHELL_SURFACE_SCREENSAVER:
1463 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001464 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001465 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001466 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001467 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001468 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001469 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001470 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001471 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001472 break;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001473 case SHELL_SURFACE_NONE:
Ander Conselvan de Oliveiraba727bd2012-02-23 13:29:25 +02001474 do_configure = 0;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001475 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001476 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001477 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001478 if (!wl_list_empty(&shell->panels)) {
1479 struct shell_surface *panel =
1480 container_of(shell->panels.prev,
1481 struct shell_surface, link);
1482 wl_list_insert(&panel->surface->link, &surface->link);
1483 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001484 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001485 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001486 }
1487
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001488 if (do_configure) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001489 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001490 weston_compositor_repick(compositor);
Juan Zhao96879df2012-02-07 08:45:41 +08001491 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1492 surface->output = shsurf->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001493 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001494
Juan Zhao7bb92f02011-12-15 11:31:51 -05001495 switch (surface_type) {
1496 case SHELL_SURFACE_TOPLEVEL:
1497 case SHELL_SURFACE_TRANSIENT:
1498 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001499 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001500 if (!shell->locked)
1501 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001502 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001503 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001505 break;
1506 default:
1507 break;
1508 }
1509
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001510 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001511 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001512}
1513
1514static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001515configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001516 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001517{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001518 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001519 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1520 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001521
Pekka Paalanen77346a62011-11-30 16:26:35 +02001522 shsurf = get_shell_surface(surface);
1523 if (shsurf)
1524 surface_type = shsurf->type;
1525
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001526 surface->geometry.x = x;
1527 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001528 surface->geometry.width = width;
1529 surface->geometry.height = height;
1530 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001531
1532 switch (surface_type) {
1533 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001534 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001535 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001536 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001537 case SHELL_SURFACE_MAXIMIZED:
1538 /*setting x, y and using configure to change that geometry*/
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001539 surface->geometry.x = surface->output->x;
1540 surface->geometry.y = surface->output->y +
1541 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001542 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001543 default:
1544 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001545 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001546
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001547 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001548 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001549 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001550
1551 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1552 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001553 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1554 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001555 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001556}
1557
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001558static int launch_desktop_shell_process(struct wl_shell *shell);
1559
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001560static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001561desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001562{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001563 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001564 struct wl_shell *shell =
1565 container_of(process, struct wl_shell, child.process);
1566
1567 shell->child.process.pid = 0;
1568 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001569
1570 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1571 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001572 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001573 shell->child.deathstamp = time;
1574 shell->child.deathcount = 0;
1575 }
1576
1577 shell->child.deathcount++;
1578 if (shell->child.deathcount > 5) {
1579 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1580 return;
1581 }
1582
1583 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1584 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001585}
1586
1587static int
1588launch_desktop_shell_process(struct wl_shell *shell)
1589{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001590 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001591
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001592 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001593 &shell->child.process,
1594 shell_exe,
1595 desktop_shell_sigchld);
1596
1597 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001598 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001599 return 0;
1600}
1601
1602static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001603bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1604{
1605 struct wl_shell *shell = data;
1606
1607 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001608 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001609}
1610
Kristian Høgsberg75840622011-09-06 13:48:16 -04001611static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001612unbind_desktop_shell(struct wl_resource *resource)
1613{
1614 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001615
1616 if (shell->locked)
1617 resume_desktop(shell);
1618
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001619 shell->child.desktop_shell = NULL;
1620 shell->prepare_event_sent = false;
1621 free(resource);
1622}
1623
1624static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001625bind_desktop_shell(struct wl_client *client,
1626 void *data, uint32_t version, uint32_t id)
1627{
1628 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001629 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001630
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001631 resource = wl_client_add_object(client, &desktop_shell_interface,
1632 &desktop_shell_implementation,
1633 id, shell);
1634
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001635 if (client == shell->child.client) {
1636 resource->destroy = unbind_desktop_shell;
1637 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001638 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001639 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001640
1641 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1642 "permission to bind desktop_shell denied");
1643 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001644}
1645
Pekka Paalanen6e168112011-11-24 11:34:05 +02001646static void
1647screensaver_set_surface(struct wl_client *client,
1648 struct wl_resource *resource,
1649 struct wl_resource *shell_surface_resource,
1650 struct wl_resource *output_resource)
1651{
1652 struct wl_shell *shell = resource->data;
1653 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001654 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001655
Pekka Paalanen98262232011-12-01 10:42:22 +02001656 if (reset_shell_surface_type(surface))
1657 return;
1658
Pekka Paalanen77346a62011-11-30 16:26:35 +02001659 surface->type = SHELL_SURFACE_SCREENSAVER;
1660
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001661 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001662 surface->output = output;
1663 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001664}
1665
1666static const struct screensaver_interface screensaver_implementation = {
1667 screensaver_set_surface
1668};
1669
1670static void
1671unbind_screensaver(struct wl_resource *resource)
1672{
1673 struct wl_shell *shell = resource->data;
1674
Pekka Paalanen77346a62011-11-30 16:26:35 +02001675 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001676 free(resource);
1677}
1678
1679static void
1680bind_screensaver(struct wl_client *client,
1681 void *data, uint32_t version, uint32_t id)
1682{
1683 struct wl_shell *shell = data;
1684 struct wl_resource *resource;
1685
1686 resource = wl_client_add_object(client, &screensaver_interface,
1687 &screensaver_implementation,
1688 id, shell);
1689
Pekka Paalanen77346a62011-11-30 16:26:35 +02001690 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001691 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001692 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001693 return;
1694 }
1695
1696 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1697 "interface object already bound");
1698 wl_resource_destroy(resource, 0);
1699}
1700
Kristian Høgsberg07045392012-02-19 18:52:44 -05001701struct switcher {
1702 struct weston_compositor *compositor;
1703 struct weston_surface *current;
1704 struct wl_listener listener;
1705 struct wl_keyboard_grab grab;
1706};
1707
1708static void
1709switcher_next(struct switcher *switcher)
1710{
1711 struct weston_compositor *compositor = switcher->compositor;
1712 struct weston_surface *surface;
1713 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1714
1715 wl_list_for_each(surface, &compositor->surface_list, link) {
1716 /* Workaround for cursor surfaces. */
1717 if (surface->surface.resource.destroy_listener_list.next == NULL)
1718 continue;
1719
1720 switch (get_shell_surface_type(surface)) {
1721 case SHELL_SURFACE_TOPLEVEL:
1722 case SHELL_SURFACE_FULLSCREEN:
1723 case SHELL_SURFACE_MAXIMIZED:
1724 if (first == NULL)
1725 first = surface;
1726 if (prev == switcher->current)
1727 next = surface;
1728 prev = surface;
1729 surface->alpha = 64;
1730 weston_surface_damage(surface);
1731 break;
1732 default:
1733 break;
1734 }
1735 }
1736
1737 if (next == NULL)
1738 next = first;
1739
1740 wl_list_remove(&switcher->listener.link);
1741 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1742 &switcher->listener.link);
1743
1744 switcher->current = next;
1745 next->alpha = 255;
1746}
1747
1748static void
1749switcher_handle_surface_destroy(struct wl_listener *listener,
1750 struct wl_resource *resource, uint32_t time)
1751{
1752 struct switcher *switcher =
1753 container_of(listener, struct switcher, listener);
1754
1755 switcher_next(switcher);
1756}
1757
1758static void
1759switcher_destroy(struct switcher *switcher, uint32_t time)
1760{
1761 struct weston_compositor *compositor = switcher->compositor;
1762 struct weston_surface *surface;
1763 struct weston_input_device *device =
1764 (struct weston_input_device *) switcher->grab.input_device;
1765
1766 wl_list_for_each(surface, &compositor->surface_list, link) {
1767 surface->alpha = 255;
1768 weston_surface_damage(surface);
1769 }
1770
1771 activate(compositor->shell, switcher->current, device, time);
1772 wl_list_remove(&switcher->listener.link);
1773 wl_input_device_end_keyboard_grab(&device->input_device, time);
1774 free(switcher);
1775}
1776
1777static void
1778switcher_key(struct wl_keyboard_grab *grab,
1779 uint32_t time, uint32_t key, int32_t state)
1780{
1781 struct switcher *switcher = container_of(grab, struct switcher, grab);
1782 struct weston_input_device *device =
1783 (struct weston_input_device *) grab->input_device;
1784
1785 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1786 switcher_destroy(switcher, time);
1787 } else if (key == KEY_TAB && state) {
1788 switcher_next(switcher);
1789 }
1790};
1791
1792static const struct wl_keyboard_grab_interface switcher_grab = {
1793 switcher_key
1794};
1795
1796static void
1797switcher_binding(struct wl_input_device *device, uint32_t time,
1798 uint32_t key, uint32_t button,
1799 uint32_t state, void *data)
1800{
1801 struct weston_compositor *compositor = data;
1802 struct switcher *switcher;
1803
1804 switcher = malloc(sizeof *switcher);
1805 switcher->compositor = compositor;
1806 switcher->current = NULL;
1807 switcher->listener.func = switcher_handle_surface_destroy;
1808 wl_list_init(&switcher->listener.link);
1809
1810 switcher->grab.interface = &switcher_grab;
1811 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1812 switcher_next(switcher);
1813}
1814
Pekka Paalanen3c647232011-12-22 13:43:43 +02001815static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001816shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001817{
1818 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1819
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001820 if (shell->child.client)
1821 wl_client_destroy(shell->child.client);
1822
Pekka Paalanen3c647232011-12-22 13:43:43 +02001823 free(shell->screensaver.path);
1824 free(shell);
1825}
1826
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001827int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001828shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001829
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001830WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001831shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001832{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001833 struct wl_shell *shell;
1834
1835 shell = malloc(sizeof *shell);
1836 if (shell == NULL)
1837 return -1;
1838
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001839 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001840 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001841 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001842 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001843 shell->shell.map = map;
1844 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001845 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001846
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001847 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001848 wl_list_init(&shell->backgrounds);
1849 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001850 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001851
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001852 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001853
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001854 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1855 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001856 return -1;
1857
Kristian Høgsberg75840622011-09-06 13:48:16 -04001858 if (wl_display_add_global(ec->wl_display,
1859 &desktop_shell_interface,
1860 shell, bind_desktop_shell) == NULL)
1861 return -1;
1862
Pekka Paalanen6e168112011-11-24 11:34:05 +02001863 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1864 shell, bind_screensaver) == NULL)
1865 return -1;
1866
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001867 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001868 if (launch_desktop_shell_process(shell) != 0)
1869 return -1;
1870
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001871 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001872 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001874 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001875 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001876 MODIFIER_CTRL | MODIFIER_ALT,
1877 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001878 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001879 click_to_activate_binding, ec);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001880 weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
1881 zoom_in_binding, shell);
1882 weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
1883 zoom_out_binding, shell);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001884 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1885 MODIFIER_SUPER | MODIFIER_ALT,
1886 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001887
Kristian Høgsberg07045392012-02-19 18:52:44 -05001888 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
1889 switcher_binding, ec);
1890
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001891 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001892
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001893 return 0;
1894}