blob: d949d0c360adbdb3b742251337b319dab9ba92a3 [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
Scott Moreauc6d7f602012-02-23 22:28:37 -0700122struct weston_zoom_grab {
123 struct wl_keyboard_grab grab;
124};
125
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126struct rotate_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700127 struct wl_pointer_grab grab;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200128 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500129 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200130 struct {
131 int32_t x;
132 int32_t y;
133 } center;
134};
135
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500136static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200137shell_configuration(struct wl_shell *shell)
138{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200139 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200140 char *path = NULL;
141 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200142
143 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200144 { "path", CONFIG_KEY_STRING, &path },
145 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200146 };
147
148 struct config_section cs[] = {
149 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
150 };
151
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500152 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500153 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200154 free(config_file);
155
Pekka Paalanen7296e792011-12-07 16:22:00 +0200156 shell->screensaver.path = path;
157 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200158}
159
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200160static void
Scott Moreau447013d2012-02-18 05:05:29 -0700161noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500162 struct wl_surface *surface, int32_t x, int32_t y)
163{
164 grab->focus = NULL;
165}
166
167static void
Scott Moreau447013d2012-02-18 05:05:29 -0700168move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500169 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500170{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500171 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500172 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500173 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500175 weston_surface_configure(es,
176 device->x + move->dx,
177 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200178 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500179}
180
181static void
Scott Moreau447013d2012-02-18 05:05:29 -0700182move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500183 uint32_t time, int32_t button, int32_t state)
184{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500185 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500187 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700188 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500189 free(grab);
190 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500191}
192
Scott Moreau447013d2012-02-18 05:05:29 -0700193static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500194 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500195 move_grab_motion,
196 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197};
198
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400199static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500200weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500201 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500203 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500204
205 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400206 if (!move)
207 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500208
209 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200210 move->dx = es->geometry.x - wd->input_device.grab_x;
211 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500212 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500213
Scott Moreau447013d2012-02-18 05:05:29 -0700214 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200216 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400217
218 return 0;
219}
220
221static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200222shell_surface_move(struct wl_client *client, struct wl_resource *resource,
223 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400224{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200226 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400227
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500228 if (wd->input_device.button_count == 0 ||
229 wd->input_device.grab_time != time ||
230 wd->input_device.pointer_focus != &shsurf->surface->surface)
231 return;
232
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400234 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500235}
236
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500237struct weston_resize_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700238 struct wl_pointer_grab grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200240 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200241 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242};
243
244static void
Scott Moreau447013d2012-02-18 05:05:29 -0700245resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500246 uint32_t time, int32_t x, int32_t y)
247{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500248 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500249 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500250 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200251 int32_t from_x, from_y;
252 int32_t to_x, to_y;
253
254 weston_surface_from_global(resize->shsurf->surface,
255 device->grab_x, device->grab_y,
256 &from_x, &from_y);
257 weston_surface_from_global(resize->shsurf->surface,
258 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200260 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200261 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200262 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200263 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500264 } else {
265 width = resize->width;
266 }
267
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200268 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200269 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200270 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200271 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500272 } else {
273 height = resize->height;
274 }
275
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200276 wl_resource_post_event(&resize->shsurf->resource,
277 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
278 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279}
280
281static void
Scott Moreau447013d2012-02-18 05:05:29 -0700282resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500283 uint32_t time, int32_t button, int32_t state)
284{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500285 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500286
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500287 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700288 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500289 free(grab);
290 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500291}
292
Scott Moreau447013d2012-02-18 05:05:29 -0700293static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500294 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500295 resize_grab_motion,
296 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500297};
298
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400299static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300weston_surface_resize(struct shell_surface *shsurf,
301 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200302 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500303{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500304 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500305
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500306 /* FIXME: Reject if fullscreen */
307
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200308 if (edges == 0 || edges > 15 ||
309 (edges & 3) == 3 || (edges & 12) == 12)
310 return 0;
311
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500312 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400313 if (!resize)
314 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315
316 resize->grab.interface = &resize_grab_interface;
317 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200318 resize->width = shsurf->surface->geometry.width;
319 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200320 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400321
Scott Moreau447013d2012-02-18 05:05:29 -0700322 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500323
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200324 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400325
326 return 0;
327}
328
329static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200330shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
331 struct wl_resource *input_resource, uint32_t time,
332 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400333{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500334 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200335 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400336
337 /* FIXME: Reject if fullscreen */
338
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500339 if (wd->input_device.button_count == 0 ||
340 wd->input_device.grab_time != time ||
341 wd->input_device.pointer_focus != &shsurf->surface->surface)
342 return;
343
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500344 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400345 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500346}
347
Juan Zhao96879df2012-02-07 08:45:41 +0800348static struct weston_output *
349get_default_output(struct weston_compositor *compositor)
350{
351 return container_of(compositor->output_list.next,
352 struct weston_output, link);
353}
354
Pekka Paalanen98262232011-12-01 10:42:22 +0200355static int
356reset_shell_surface_type(struct shell_surface *surface)
357{
358 switch (surface->type) {
359 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200360 weston_surface_set_position(surface->surface,
361 surface->saved_x,
362 surface->saved_y);
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500363 surface->fullscreen_output = NULL;
Pekka Paalanen98262232011-12-01 10:42:22 +0200364 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800365 case SHELL_SURFACE_MAXIMIZED:
366 surface->output = get_default_output(surface->surface->compositor);
367 weston_surface_set_position(surface->surface,
368 surface->saved_x,
369 surface->saved_y);
370 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200371 case SHELL_SURFACE_PANEL:
372 case SHELL_SURFACE_BACKGROUND:
373 wl_list_remove(&surface->link);
374 wl_list_init(&surface->link);
375 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200376 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200377 case SHELL_SURFACE_LOCK:
378 wl_resource_post_error(&surface->resource,
379 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200380 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200381 return -1;
382 case SHELL_SURFACE_NONE:
383 case SHELL_SURFACE_TOPLEVEL:
384 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500385 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200386 break;
387 }
388
389 surface->type = SHELL_SURFACE_NONE;
390 return 0;
391}
392
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500393static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200394shell_surface_set_toplevel(struct wl_client *client,
395 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400396
397{
Pekka Paalanen98262232011-12-01 10:42:22 +0200398 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400399
Pekka Paalanen98262232011-12-01 10:42:22 +0200400 if (reset_shell_surface_type(surface))
401 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400402
Pekka Paalanen98262232011-12-01 10:42:22 +0200403 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400404}
405
406static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200407shell_surface_set_transient(struct wl_client *client,
408 struct wl_resource *resource,
409 struct wl_resource *parent_resource,
410 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400411{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200412 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500413 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200414 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500415 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400416
Pekka Paalanen98262232011-12-01 10:42:22 +0200417 if (reset_shell_surface_type(shsurf))
418 return;
419
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400420 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800421 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200422 weston_surface_set_position(es, pes->geometry.x + x,
423 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400424
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200425 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400426}
427
Juan Zhao96879df2012-02-07 08:45:41 +0800428static struct wl_shell *
429shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200430{
Juan Zhao96879df2012-02-07 08:45:41 +0800431 struct weston_surface *es = shsurf->surface;
432 struct weston_shell *shell = es->compositor->shell;
433
434 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
435}
436
437static int
438get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
439{
440 struct shell_surface *priv;
441 int panel_height = 0;
442
443 if (!output)
444 return 0;
445
446 wl_list_for_each(priv, &wlshell->panels, link) {
447 if (priv->output == output) {
448 panel_height = priv->surface->geometry.height;
449 break;
450 }
451 }
452 return panel_height;
453}
454
455static void
456shell_surface_set_maximized(struct wl_client *client,
457 struct wl_resource *resource,
458 struct wl_resource *output_resource )
459{
460 struct shell_surface *shsurf = resource->data;
461 struct weston_surface *es = shsurf->surface;
462 struct wl_shell *wlshell = NULL;
463 uint32_t edges = 0, panel_height = 0;
464
465 /* get the default output, if the client set it as NULL
466 check whether the ouput is available */
467 if (output_resource)
468 shsurf->output = output_resource->data;
469 else
470 shsurf->output = get_default_output(es->compositor);
471
472 if (reset_shell_surface_type(shsurf))
473 return;
474
475 shsurf->saved_x = es->geometry.x;
476 shsurf->saved_y = es->geometry.y;
477
478 wlshell = shell_surface_get_shell(shsurf);
479 panel_height = get_output_panel_height(wlshell, es->output);
480 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
481 wl_resource_post_event(&shsurf->resource,
482 WL_SHELL_SURFACE_CONFIGURE,
483 weston_compositor_get_time(), edges,
484 es->output->current->width,
485 es->output->current->height - panel_height);
486
487 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200488}
489
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400490static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200491shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500492 struct wl_resource *resource,
493 uint32_t method,
494 uint32_t framerate,
495 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400496{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200497 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500498 struct weston_surface *es = shsurf->surface;
499 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400500
Pekka Paalanen98262232011-12-01 10:42:22 +0200501 if (reset_shell_surface_type(shsurf))
502 return;
503
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400504 /* FIXME: Fullscreen on first output */
505 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200506 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400507
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200508 shsurf->saved_x = es->geometry.x;
509 shsurf->saved_y = es->geometry.y;
Alex Wu88277d12012-02-22 14:50:46 +0800510 shsurf->output = output;
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500511 shsurf->fullscreen_output = output;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200512 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500513
514 wl_resource_post_event(resource,
515 WL_SHELL_SURFACE_CONFIGURE,
516 weston_compositor_get_time(), 0,
Alex Wu88277d12012-02-22 14:50:46 +0800517 shsurf->output->current->width,
518 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400519}
520
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500521static void
Scott Moreau447013d2012-02-18 05:05:29 -0700522popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500523 struct wl_surface *surface, int32_t x, int32_t y)
524{
525 struct wl_input_device *device = grab->input_device;
526 struct shell_surface *priv =
527 container_of(grab, struct shell_surface, popup.grab);
528 struct wl_client *client = priv->surface->surface.resource.client;
529
Pekka Paalanencb108432012-01-19 16:25:40 +0200530 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200531 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500532 grab->focus = surface;
533 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200534 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500535 grab->focus = NULL;
536 }
537}
538
539static void
Scott Moreau447013d2012-02-18 05:05:29 -0700540popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200541 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500542{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500543 struct wl_resource *resource;
544
545 resource = grab->input_device->pointer_focus_resource;
546 if (resource)
547 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200548 time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500549}
550
551static void
Scott Moreau447013d2012-02-18 05:05:29 -0700552popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500553 uint32_t time, int32_t button, int32_t state)
554{
555 struct wl_resource *resource;
556 struct shell_surface *shsurf =
557 container_of(grab, struct shell_surface, popup.grab);
558
559 resource = grab->input_device->pointer_focus_resource;
560 if (resource) {
561 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
562 time, button, state);
563 } else if (state == 0 &&
564 (shsurf->popup.initial_up ||
565 time - shsurf->popup.time > 500)) {
566 wl_resource_post_event(&shsurf->resource,
567 WL_SHELL_SURFACE_POPUP_DONE);
Scott Moreau447013d2012-02-18 05:05:29 -0700568 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500569 shsurf->popup.grab.input_device = NULL;
570 }
571
572 if (state == 0)
573 shsurf->popup.initial_up = 1;
574}
575
Scott Moreau447013d2012-02-18 05:05:29 -0700576static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500577 popup_grab_focus,
578 popup_grab_motion,
579 popup_grab_button,
580};
581
582static void
583shell_map_popup(struct shell_surface *shsurf, uint32_t time)
584{
585 struct wl_input_device *device;
586 struct weston_surface *es = shsurf->surface;
587 struct weston_surface *parent = shsurf->parent->surface;
588
589 es->output = parent->output;
590
591 shsurf->popup.grab.interface = &popup_grab_interface;
592 device = es->compositor->input_device;
593
Pekka Paalanen938269a2012-02-07 14:19:01 +0200594 weston_surface_update_transform(parent);
595 if (parent->transform.enabled) {
596 shsurf->popup.parent_transform.matrix =
597 parent->transform.matrix;
598 } else {
599 /* construct x, y translation matrix */
600 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
601 shsurf->popup.parent_transform.matrix.d[12] =
602 parent->geometry.x;
603 shsurf->popup.parent_transform.matrix.d[13] =
604 parent->geometry.y;
605 }
606 wl_list_insert(es->geometry.transformation_list.prev,
607 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200608 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500609
610 shsurf->popup.grab.input_device = device;
611 shsurf->popup.time = device->grab_time;
612 shsurf->popup.initial_up = 0;
613
Scott Moreau447013d2012-02-18 05:05:29 -0700614 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500615 &shsurf->popup.grab, shsurf->popup.time);
616}
617
618static void
619shell_surface_set_popup(struct wl_client *client,
620 struct wl_resource *resource,
621 struct wl_resource *input_device_resource,
622 uint32_t time,
623 struct wl_resource *parent_resource,
624 int32_t x, int32_t y, uint32_t flags)
625{
626 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500627
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500628 shsurf->type = SHELL_SURFACE_POPUP;
629 shsurf->parent = parent_resource->data;
630 shsurf->popup.x = x;
631 shsurf->popup.y = y;
632}
633
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200634static const struct wl_shell_surface_interface shell_surface_implementation = {
635 shell_surface_move,
636 shell_surface_resize,
637 shell_surface_set_toplevel,
638 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500639 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800640 shell_surface_set_popup,
641 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200642};
643
644static void
645destroy_shell_surface(struct wl_resource *resource)
646{
647 struct shell_surface *shsurf = resource->data;
648
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500649 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700650 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500651
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200652 /* in case cleaning up a dead client destroys shell_surface first */
653 if (shsurf->surface)
654 wl_list_remove(&shsurf->surface_destroy_listener.link);
655
656 wl_list_remove(&shsurf->link);
657 free(shsurf);
658}
659
660static void
661shell_handle_surface_destroy(struct wl_listener *listener,
662 struct wl_resource *resource, uint32_t time)
663{
664 struct shell_surface *shsurf = container_of(listener,
665 struct shell_surface,
666 surface_destroy_listener);
667
668 shsurf->surface = NULL;
669 wl_resource_destroy(&shsurf->resource, time);
670}
671
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200672static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500673get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200674{
675 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
676 struct wl_listener *listener;
677
678 /* search the destroy listener list for our callback */
679 wl_list_for_each(listener, lst, link) {
680 if (listener->func == shell_handle_surface_destroy) {
681 return container_of(listener, struct shell_surface,
682 surface_destroy_listener);
683 }
684 }
685
686 return NULL;
687}
688
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200689static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200690shell_get_shell_surface(struct wl_client *client,
691 struct wl_resource *resource,
692 uint32_t id,
693 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200694{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500695 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200696 struct shell_surface *shsurf;
697
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200698 if (get_shell_surface(surface)) {
699 wl_resource_post_error(surface_resource,
700 WL_DISPLAY_ERROR_INVALID_OBJECT,
701 "wl_shell::get_shell_surface already requested");
702 return;
703 }
704
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200705 shsurf = calloc(1, sizeof *shsurf);
706 if (!shsurf) {
707 wl_resource_post_no_memory(resource);
708 return;
709 }
710
711 shsurf->resource.destroy = destroy_shell_surface;
712 shsurf->resource.object.id = id;
713 shsurf->resource.object.interface = &wl_shell_surface_interface;
714 shsurf->resource.object.implementation =
715 (void (**)(void)) &shell_surface_implementation;
716 shsurf->resource.data = shsurf;
717
718 shsurf->surface = surface;
719 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
720 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
721 &shsurf->surface_destroy_listener.link);
722
723 /* init link so its safe to always remove it in destroy_shell_surface */
724 wl_list_init(&shsurf->link);
725
Pekka Paalanen460099f2012-01-20 16:48:25 +0200726 /* empty when not in use */
727 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500728 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200729
Pekka Paalanen98262232011-12-01 10:42:22 +0200730 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200731
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200732 wl_client_add_resource(client, &shsurf->resource);
733}
734
735static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200736 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500737};
738
Kristian Høgsberg07937562011-04-12 17:25:42 -0400739static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500740handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200741{
742 proc->pid = 0;
743}
744
745static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200746launch_screensaver(struct wl_shell *shell)
747{
748 if (shell->screensaver.binding)
749 return;
750
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200751 if (!shell->screensaver.path)
752 return;
753
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500754 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200755 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200756 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200757 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200758}
759
760static void
761terminate_screensaver(struct wl_shell *shell)
762{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200763 if (shell->screensaver.process.pid == 0)
764 return;
765
766 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200767}
768
769static void
770show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
771{
772 struct wl_list *list;
773
774 if (shell->lock_surface)
775 list = &shell->lock_surface->surface->link;
776 else
777 list = &shell->compositor->surface_list;
778
779 wl_list_remove(&surface->surface->link);
780 wl_list_insert(list, &surface->surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200781 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200782 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200783}
784
785static void
786hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
787{
788 wl_list_remove(&surface->surface->link);
789 wl_list_init(&surface->surface->link);
790 surface->surface->output = NULL;
791}
792
793static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400794desktop_shell_set_background(struct wl_client *client,
795 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100796 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400797 struct wl_resource *surface_resource)
798{
799 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200800 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500801 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200802 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400803
Pekka Paalanen98262232011-12-01 10:42:22 +0200804 if (reset_shell_surface_type(shsurf))
805 return;
806
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100807 wl_list_for_each(priv, &shell->backgrounds, link) {
808 if (priv->output == output_resource->data) {
809 priv->surface->output = NULL;
810 wl_list_remove(&priv->surface->link);
811 wl_list_remove(&priv->link);
812 break;
813 }
814 }
815
Pekka Paalanen068ae942011-11-28 14:11:15 +0200816 shsurf->type = SHELL_SURFACE_BACKGROUND;
817 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100818
Pekka Paalanen068ae942011-11-28 14:11:15 +0200819 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100820
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200821 weston_surface_set_position(surface, shsurf->output->x,
822 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200823
Kristian Høgsberg75840622011-09-06 13:48:16 -0400824 wl_resource_post_event(resource,
825 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500826 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200827 shsurf->output->current->width,
828 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400829}
830
831static void
832desktop_shell_set_panel(struct wl_client *client,
833 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100834 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400835 struct wl_resource *surface_resource)
836{
837 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200838 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500839 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200840 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400841
Pekka Paalanen98262232011-12-01 10:42:22 +0200842 if (reset_shell_surface_type(shsurf))
843 return;
844
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100845 wl_list_for_each(priv, &shell->panels, link) {
846 if (priv->output == output_resource->data) {
847 priv->surface->output = NULL;
848 wl_list_remove(&priv->surface->link);
849 wl_list_remove(&priv->link);
850 break;
851 }
852 }
853
Pekka Paalanen068ae942011-11-28 14:11:15 +0200854 shsurf->type = SHELL_SURFACE_PANEL;
855 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100856
Pekka Paalanen068ae942011-11-28 14:11:15 +0200857 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100858
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200859 weston_surface_set_position(surface, shsurf->output->x,
860 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200861
Kristian Høgsberg75840622011-09-06 13:48:16 -0400862 wl_resource_post_event(resource,
863 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500864 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200865 shsurf->output->current->width,
866 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400867}
868
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200869static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500870handle_lock_surface_destroy(struct wl_listener *listener,
871 struct wl_resource *resource, uint32_t time)
872{
873 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200874 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500875
876 fprintf(stderr, "lock surface gone\n");
877 shell->lock_surface = NULL;
878}
879
880static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200881desktop_shell_set_lock_surface(struct wl_client *client,
882 struct wl_resource *resource,
883 struct wl_resource *surface_resource)
884{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200885 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200886 struct shell_surface *surface = surface_resource->data;
887
888 if (reset_shell_surface_type(surface))
889 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200890
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200891 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200892
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200893 if (!shell->locked)
894 return;
895
Pekka Paalanen98262232011-12-01 10:42:22 +0200896 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200897
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500898 shell->lock_surface_listener.func = handle_lock_surface_destroy;
899 wl_list_insert(&surface_resource->destroy_listener_list,
900 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200901
Pekka Paalanen068ae942011-11-28 14:11:15 +0200902 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200903}
904
905static void
906resume_desktop(struct wl_shell *shell)
907{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500908 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200909 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200910 struct shell_surface *tmp;
911
912 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
913 hide_screensaver(shell, tmp);
914
915 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200916
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200917 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200918 weston_surface_assign_output(surface);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200919
Pekka Paalanenfe340832011-11-25 16:07:52 +0200920 if (wl_list_empty(&shell->backgrounds)) {
921 list = &shell->compositor->surface_list;
922 } else {
923 struct shell_surface *background;
924 background = container_of(shell->backgrounds.prev,
925 struct shell_surface, link);
926 list = background->surface->link.prev;
927 }
928
929 if (!wl_list_empty(&shell->hidden_surface_list))
930 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500931 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200932
933 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200935 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500936 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200937 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200938}
939
940static void
941desktop_shell_unlock(struct wl_client *client,
942 struct wl_resource *resource)
943{
944 struct wl_shell *shell = resource->data;
945
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200946 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200947
948 if (shell->locked)
949 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200950}
951
Kristian Høgsberg75840622011-09-06 13:48:16 -0400952static const struct desktop_shell_interface desktop_shell_implementation = {
953 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200954 desktop_shell_set_panel,
955 desktop_shell_set_lock_surface,
956 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400957};
958
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200959static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500960get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200961{
962 struct shell_surface *shsurf;
963
964 shsurf = get_shell_surface(surface);
965 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200966 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200967 return shsurf->type;
968}
969
Kristian Høgsberg75840622011-09-06 13:48:16 -0400970static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400971move_binding(struct wl_input_device *device, uint32_t time,
972 uint32_t key, uint32_t button, uint32_t state, void *data)
973{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500974 struct weston_surface *surface =
975 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500976
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100977 if (surface == NULL)
978 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400979
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200980 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100981 case SHELL_SURFACE_PANEL:
982 case SHELL_SURFACE_BACKGROUND:
983 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200984 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100985 return;
986 default:
987 break;
988 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400989
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500990 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400991}
992
993static void
994resize_binding(struct wl_input_device *device, uint32_t time,
995 uint32_t key, uint32_t button, uint32_t state, void *data)
996{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500997 struct weston_surface *surface =
998 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400999 uint32_t edges = 0;
1000 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001001 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001002
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001003 if (surface == NULL)
1004 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001005
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001006 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001007 if (!shsurf)
1008 return;
1009
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001010 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001011 case SHELL_SURFACE_PANEL:
1012 case SHELL_SURFACE_BACKGROUND:
1013 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001014 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001015 return;
1016 default:
1017 break;
1018 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001019
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001020 weston_surface_from_global(surface,
1021 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001022
Pekka Paalanen60921e52012-01-25 15:55:43 +02001023 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001024 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001025 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001026 edges |= 0;
1027 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001028 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001029
Pekka Paalanen60921e52012-01-25 15:55:43 +02001030 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001031 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001032 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001033 edges |= 0;
1034 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001035 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001036
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001037 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001038 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001039}
1040
1041static void
Scott Moreauc6d7f602012-02-23 22:28:37 -07001042zoom_grab_key(struct wl_keyboard_grab *grab,
1043 uint32_t time, uint32_t key, int32_t state)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001044{
Scott Moreauc6d7f602012-02-23 22:28:37 -07001045 struct wl_input_device *device = grab->input_device;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001046 struct weston_input_device *wd = (struct weston_input_device *) device;
1047 struct weston_compositor *compositor = wd->compositor;
1048 struct weston_output *output;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001049 struct weston_zoom_grab *zoom;
1050
1051 if (!(wd->modifier_state & MODIFIER_SUPER)) {
1052 zoom = container_of(grab, struct weston_zoom_grab, grab);
1053 wl_input_device_end_keyboard_grab(device, time);
1054 free(zoom);
1055 return;
1056 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001057
1058 wl_list_for_each(output, &compositor->output_list, link) {
1059 if (pixman_region32_contains_point(&output->region,
1060 device->x, device->y, NULL)) {
Scott Moreauc6d7f602012-02-23 22:28:37 -07001061 if (state && key == KEY_UP) {
1062 output->zoom.active = 1;
1063 output->zoom.level -= output->zoom.increment;
1064 }
1065 if (state && key == KEY_DOWN)
1066 output->zoom.level += output->zoom.increment;
1067
1068 if (output->zoom.level >= 1.0) {
1069 output->zoom.active = 0;
1070 output->zoom.level = 1.0;
1071 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001072
1073 if (output->zoom.level < output->zoom.increment)
1074 output->zoom.level = output->zoom.increment;
1075
1076 weston_output_update_zoom(output, device->x, device->y);
1077 }
1078 }
1079}
1080
Scott Moreauc6d7f602012-02-23 22:28:37 -07001081static const struct wl_keyboard_grab_interface zoom_grab = {
1082 zoom_grab_key,
1083};
1084
Scott Moreauccbf29d2012-02-22 14:21:41 -07001085static void
Scott Moreauc6d7f602012-02-23 22:28:37 -07001086zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreauccbf29d2012-02-22 14:21:41 -07001087 uint32_t key, uint32_t button, uint32_t state, void *data)
1088{
1089 struct weston_input_device *wd = (struct weston_input_device *) device;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001090 struct weston_zoom_grab *zoom;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001091
Scott Moreauc6d7f602012-02-23 22:28:37 -07001092 zoom = malloc(sizeof *zoom);
1093 if (!zoom)
1094 return;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001095
Scott Moreauc6d7f602012-02-23 22:28:37 -07001096 zoom->grab.interface = &zoom_grab;
1097
1098 wl_input_device_start_keyboard_grab(&wd->input_device, &zoom->grab, time);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001099}
1100
1101static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001102terminate_binding(struct wl_input_device *device, uint32_t time,
1103 uint32_t key, uint32_t button, uint32_t state, void *data)
1104{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001105 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001106
1107 if (state)
1108 wl_display_terminate(compositor->wl_display);
1109}
1110
1111static void
Scott Moreau447013d2012-02-18 05:05:29 -07001112rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001113 uint32_t time, int32_t x, int32_t y)
1114{
1115 struct rotate_grab *rotate =
1116 container_of(grab, struct rotate_grab, grab);
1117 struct wl_input_device *device = grab->input_device;
1118 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001119 GLfloat cx = 0.5f * surface->surface->geometry.width;
1120 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001121 GLfloat dx, dy;
1122 GLfloat r;
1123
1124 dx = device->x - rotate->center.x;
1125 dy = device->y - rotate->center.y;
1126 r = sqrtf(dx * dx + dy * dy);
1127
1128 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001129 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001130
1131 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001132 struct weston_matrix *matrix =
1133 &surface->rotation.transform.matrix;
1134
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001135 weston_matrix_init(&rotate->rotation);
1136 rotate->rotation.d[0] = dx / r;
1137 rotate->rotation.d[4] = -dy / r;
1138 rotate->rotation.d[1] = -rotate->rotation.d[4];
1139 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001140
1141 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001142 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001143 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1144 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001145 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001146
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001147 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001148 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001149 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001150 } else {
1151 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001152 weston_matrix_init(&surface->rotation.rotation);
1153 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001154 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001155
1156 /* Repaint implies weston_surface_update_transform(), which
1157 * lazily applies the damage due to rotation update.
1158 */
1159 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001160}
1161
1162static void
Scott Moreau447013d2012-02-18 05:05:29 -07001163rotate_grab_button(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001164 uint32_t time, int32_t button, int32_t state)
1165{
1166 struct rotate_grab *rotate =
1167 container_of(grab, struct rotate_grab, grab);
1168 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001169 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001170
1171 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001172 weston_matrix_multiply(&surface->rotation.rotation,
1173 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001174 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001175 free(rotate);
1176 }
1177}
1178
Scott Moreau447013d2012-02-18 05:05:29 -07001179static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001180 noop_grab_focus,
1181 rotate_grab_motion,
1182 rotate_grab_button,
1183};
1184
1185static void
1186rotate_binding(struct wl_input_device *device, uint32_t time,
1187 uint32_t key, uint32_t button, uint32_t state, void *data)
1188{
1189 struct weston_surface *base_surface =
1190 (struct weston_surface *) device->pointer_focus;
1191 struct shell_surface *surface;
1192 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001193 GLfloat dx, dy;
1194 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001195
1196 if (base_surface == NULL)
1197 return;
1198
1199 surface = get_shell_surface(base_surface);
1200 if (!surface)
1201 return;
1202
1203 switch (surface->type) {
1204 case SHELL_SURFACE_PANEL:
1205 case SHELL_SURFACE_BACKGROUND:
1206 case SHELL_SURFACE_FULLSCREEN:
1207 case SHELL_SURFACE_SCREENSAVER:
1208 return;
1209 default:
1210 break;
1211 }
1212
Pekka Paalanen460099f2012-01-20 16:48:25 +02001213 rotate = malloc(sizeof *rotate);
1214 if (!rotate)
1215 return;
1216
1217 rotate->grab.interface = &rotate_grab_interface;
1218 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001219
1220 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001221 surface->surface->geometry.width / 2,
1222 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001223 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001224
Scott Moreau447013d2012-02-18 05:05:29 -07001225 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001226
1227 dx = device->x - rotate->center.x;
1228 dy = device->y - rotate->center.y;
1229 r = sqrtf(dx * dx + dy * dy);
1230 if (r > 20.0f) {
1231 struct weston_matrix inverse;
1232
1233 weston_matrix_init(&inverse);
1234 inverse.d[0] = dx / r;
1235 inverse.d[4] = dy / r;
1236 inverse.d[1] = -inverse.d[4];
1237 inverse.d[5] = inverse.d[0];
1238 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1239 } else {
1240 weston_matrix_init(&surface->rotation.rotation);
1241 weston_matrix_init(&rotate->rotation);
1242 }
1243
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001244 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001245}
1246
1247static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001248activate(struct weston_shell *base, struct weston_surface *es,
1249 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001250{
1251 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001252 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001253 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001254
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001255 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001256
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001257 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001258 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001259
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001260 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001261 case SHELL_SURFACE_BACKGROUND:
1262 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001263 wl_list_remove(&es->link);
1264 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001265 break;
1266 case SHELL_SURFACE_PANEL:
1267 /* already put on top */
1268 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001269 case SHELL_SURFACE_SCREENSAVER:
1270 /* always below lock surface */
1271 if (shell->lock_surface) {
1272 wl_list_remove(&es->link);
1273 wl_list_insert(&shell->lock_surface->surface->link,
1274 &es->link);
1275 }
1276 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001277 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001278 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001279 list = weston_compositor_top(compositor);
1280
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001281 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001282 struct shell_surface *panel;
1283 wl_list_for_each(panel, &shell->panels, link) {
1284 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001285 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001286 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001287 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001288 }
1289}
1290
1291static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001292click_to_activate_binding(struct wl_input_device *device,
1293 uint32_t time, uint32_t key,
1294 uint32_t button, uint32_t state, void *data)
1295{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001296 struct weston_input_device *wd = (struct weston_input_device *) device;
1297 struct weston_compositor *compositor = data;
1298 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001299
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001300 focus = (struct weston_surface *) device->pointer_focus;
Scott Moreau447013d2012-02-18 05:05:29 -07001301 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001302 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001303}
1304
1305static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001306lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001307{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001308 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001309 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001310 struct weston_surface *cur;
1311 struct weston_surface *tmp;
1312 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001313 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001314 uint32_t time;
1315
1316 if (shell->locked)
1317 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001318
1319 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001320
1321 /* Move all surfaces from compositor's list to our hidden list,
1322 * except the background. This way nothing else can show or
1323 * receive input events while we are locked. */
1324
1325 if (!wl_list_empty(&shell->hidden_surface_list)) {
1326 fprintf(stderr,
1327 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1328 __func__);
1329 }
1330
1331 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1332 /* skip input device sprites, cur->surface is uninitialised */
1333 if (cur->surface.resource.client == NULL)
1334 continue;
1335
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001336 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001337 continue;
1338
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001339 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001340 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001341 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001342 }
1343
Pekka Paalanen77346a62011-11-30 16:26:35 +02001344 launch_screensaver(shell);
1345
1346 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1347 show_screensaver(shell, shsurf);
1348
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001349 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001350 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001351 weston_compositor_wake(shell->compositor);
1352 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001353 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001354
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001355 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001356 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001357
1358 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001360 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1361 wl_input_device_set_keyboard_focus(&device->input_device,
1362 NULL, time);
1363 }
1364
1365 /* TODO: disable bindings that should not work while locked. */
1366
1367 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001368}
1369
1370static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001371unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001372{
1373 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1374
Pekka Paalanend81c2162011-11-16 13:47:34 +02001375 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001376 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001377 return;
1378 }
1379
1380 /* If desktop-shell client has gone away, unlock immediately. */
1381 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001382 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001383 return;
1384 }
1385
1386 if (shell->prepare_event_sent)
1387 return;
1388
1389 wl_resource_post_event(shell->child.desktop_shell,
1390 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1391 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001392}
1393
1394static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001395center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001396{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001397 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001398 GLfloat x = (mode->width - surface->geometry.width) / 2;
1399 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001400
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001401 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001402}
1403
1404static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001405map(struct weston_shell *base, struct weston_surface *surface,
1406 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001407{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001408 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001409 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001410 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001411 struct shell_surface *shsurf;
1412 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1413 int do_configure;
Juan Zhao96879df2012-02-07 08:45:41 +08001414 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001415
Pekka Paalanen77346a62011-11-30 16:26:35 +02001416 shsurf = get_shell_surface(surface);
1417 if (shsurf)
1418 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001419
Pekka Paalanen77346a62011-11-30 16:26:35 +02001420 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001421 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001422 do_configure = 0;
1423 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001424 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001425 do_configure = 1;
1426 }
1427
Pekka Paalanen60921e52012-01-25 15:55:43 +02001428 surface->geometry.width = width;
1429 surface->geometry.height = height;
1430 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001431
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001432 weston_compositor_update_drag_surfaces(compositor);
1433
Pekka Paalanen77346a62011-11-30 16:26:35 +02001434 /* initial positioning, see also configure() */
1435 switch (surface_type) {
1436 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001437 weston_surface_set_position(surface, 10 + random() % 400,
1438 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001439 break;
1440 case SHELL_SURFACE_SCREENSAVER:
1441 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001442 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001443 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001444 case SHELL_SURFACE_MAXIMIZED:
1445 /*use surface configure to set the geometry*/
1446 panel_height = get_output_panel_height(shell,surface->output);
1447 weston_surface_set_position(surface, surface->output->x,
1448 surface->output->y + panel_height);
1449 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001450 case SHELL_SURFACE_LOCK:
1451 center_on_output(surface, get_default_output(compositor));
1452 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001453 case SHELL_SURFACE_POPUP:
1454 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001455 case SHELL_SURFACE_NONE:
1456 weston_surface_set_position(surface,
1457 surface->geometry.x + sx,
1458 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001459 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001460 default:
1461 ;
1462 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001463
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001464 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001465 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001466 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001467 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001468 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001469 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001470 break;
1471 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001472 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001473 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001474 break;
1475 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001476 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001477 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001478
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001479 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001480 do_configure = 1;
1481 break;
1482 case SHELL_SURFACE_SCREENSAVER:
1483 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001484 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001485 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001486 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001487 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001488 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001489 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001490 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001491 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001492 break;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001493 case SHELL_SURFACE_NONE:
Ander Conselvan de Oliveiraba727bd2012-02-23 13:29:25 +02001494 do_configure = 0;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001495 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001496 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001497 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001498 if (!wl_list_empty(&shell->panels)) {
1499 struct shell_surface *panel =
1500 container_of(shell->panels.prev,
1501 struct shell_surface, link);
1502 wl_list_insert(&panel->surface->link, &surface->link);
1503 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001504 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001505 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001506 }
1507
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001508 if (do_configure) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001509 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001510 weston_compositor_repick(compositor);
Juan Zhao96879df2012-02-07 08:45:41 +08001511 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1512 surface->output = shsurf->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001513 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001514
Juan Zhao7bb92f02011-12-15 11:31:51 -05001515 switch (surface_type) {
1516 case SHELL_SURFACE_TOPLEVEL:
1517 case SHELL_SURFACE_TRANSIENT:
1518 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001519 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001520 if (!shell->locked)
1521 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001522 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001523 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001525 break;
1526 default:
1527 break;
1528 }
1529
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001530 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001531 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001532}
1533
1534static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001535configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001536 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001537{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001538 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001539 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1540 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001541
Pekka Paalanen77346a62011-11-30 16:26:35 +02001542 shsurf = get_shell_surface(surface);
1543 if (shsurf)
1544 surface_type = shsurf->type;
1545
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001546 surface->geometry.x = x;
1547 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001548 surface->geometry.width = width;
1549 surface->geometry.height = height;
1550 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001551
1552 switch (surface_type) {
1553 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001554 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001555 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001556 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001557 case SHELL_SURFACE_MAXIMIZED:
1558 /*setting x, y and using configure to change that geometry*/
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001559 surface->geometry.x = surface->output->x;
1560 surface->geometry.y = surface->output->y +
1561 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001562 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001563 default:
1564 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001565 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001566
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001567 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001568 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001569 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001570
1571 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1572 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001573 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1574 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001575 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001576}
1577
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001578static int launch_desktop_shell_process(struct wl_shell *shell);
1579
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001580static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001581desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001582{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001583 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001584 struct wl_shell *shell =
1585 container_of(process, struct wl_shell, child.process);
1586
1587 shell->child.process.pid = 0;
1588 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001589
1590 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1591 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001592 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001593 shell->child.deathstamp = time;
1594 shell->child.deathcount = 0;
1595 }
1596
1597 shell->child.deathcount++;
1598 if (shell->child.deathcount > 5) {
1599 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1600 return;
1601 }
1602
1603 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1604 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001605}
1606
1607static int
1608launch_desktop_shell_process(struct wl_shell *shell)
1609{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001610 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001611
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001612 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001613 &shell->child.process,
1614 shell_exe,
1615 desktop_shell_sigchld);
1616
1617 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001618 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001619 return 0;
1620}
1621
1622static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001623bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1624{
1625 struct wl_shell *shell = data;
1626
1627 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001628 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001629}
1630
Kristian Høgsberg75840622011-09-06 13:48:16 -04001631static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001632unbind_desktop_shell(struct wl_resource *resource)
1633{
1634 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001635
1636 if (shell->locked)
1637 resume_desktop(shell);
1638
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001639 shell->child.desktop_shell = NULL;
1640 shell->prepare_event_sent = false;
1641 free(resource);
1642}
1643
1644static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001645bind_desktop_shell(struct wl_client *client,
1646 void *data, uint32_t version, uint32_t id)
1647{
1648 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001649 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001650
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001651 resource = wl_client_add_object(client, &desktop_shell_interface,
1652 &desktop_shell_implementation,
1653 id, shell);
1654
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001655 if (client == shell->child.client) {
1656 resource->destroy = unbind_desktop_shell;
1657 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001658 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001659 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001660
1661 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1662 "permission to bind desktop_shell denied");
1663 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001664}
1665
Pekka Paalanen6e168112011-11-24 11:34:05 +02001666static void
1667screensaver_set_surface(struct wl_client *client,
1668 struct wl_resource *resource,
1669 struct wl_resource *shell_surface_resource,
1670 struct wl_resource *output_resource)
1671{
1672 struct wl_shell *shell = resource->data;
1673 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001674 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001675
Pekka Paalanen98262232011-12-01 10:42:22 +02001676 if (reset_shell_surface_type(surface))
1677 return;
1678
Pekka Paalanen77346a62011-11-30 16:26:35 +02001679 surface->type = SHELL_SURFACE_SCREENSAVER;
1680
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001681 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001682 surface->output = output;
1683 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001684}
1685
1686static const struct screensaver_interface screensaver_implementation = {
1687 screensaver_set_surface
1688};
1689
1690static void
1691unbind_screensaver(struct wl_resource *resource)
1692{
1693 struct wl_shell *shell = resource->data;
1694
Pekka Paalanen77346a62011-11-30 16:26:35 +02001695 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001696 free(resource);
1697}
1698
1699static void
1700bind_screensaver(struct wl_client *client,
1701 void *data, uint32_t version, uint32_t id)
1702{
1703 struct wl_shell *shell = data;
1704 struct wl_resource *resource;
1705
1706 resource = wl_client_add_object(client, &screensaver_interface,
1707 &screensaver_implementation,
1708 id, shell);
1709
Pekka Paalanen77346a62011-11-30 16:26:35 +02001710 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001711 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001712 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001713 return;
1714 }
1715
1716 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1717 "interface object already bound");
1718 wl_resource_destroy(resource, 0);
1719}
1720
Kristian Høgsberg07045392012-02-19 18:52:44 -05001721struct switcher {
1722 struct weston_compositor *compositor;
1723 struct weston_surface *current;
1724 struct wl_listener listener;
1725 struct wl_keyboard_grab grab;
1726};
1727
1728static void
1729switcher_next(struct switcher *switcher)
1730{
1731 struct weston_compositor *compositor = switcher->compositor;
1732 struct weston_surface *surface;
1733 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1734
1735 wl_list_for_each(surface, &compositor->surface_list, link) {
1736 /* Workaround for cursor surfaces. */
1737 if (surface->surface.resource.destroy_listener_list.next == NULL)
1738 continue;
1739
1740 switch (get_shell_surface_type(surface)) {
1741 case SHELL_SURFACE_TOPLEVEL:
1742 case SHELL_SURFACE_FULLSCREEN:
1743 case SHELL_SURFACE_MAXIMIZED:
1744 if (first == NULL)
1745 first = surface;
1746 if (prev == switcher->current)
1747 next = surface;
1748 prev = surface;
1749 surface->alpha = 64;
1750 weston_surface_damage(surface);
1751 break;
1752 default:
1753 break;
1754 }
1755 }
1756
1757 if (next == NULL)
1758 next = first;
1759
1760 wl_list_remove(&switcher->listener.link);
1761 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1762 &switcher->listener.link);
1763
1764 switcher->current = next;
1765 next->alpha = 255;
1766}
1767
1768static void
1769switcher_handle_surface_destroy(struct wl_listener *listener,
1770 struct wl_resource *resource, uint32_t time)
1771{
1772 struct switcher *switcher =
1773 container_of(listener, struct switcher, listener);
1774
1775 switcher_next(switcher);
1776}
1777
1778static void
1779switcher_destroy(struct switcher *switcher, uint32_t time)
1780{
1781 struct weston_compositor *compositor = switcher->compositor;
1782 struct weston_surface *surface;
1783 struct weston_input_device *device =
1784 (struct weston_input_device *) switcher->grab.input_device;
1785
1786 wl_list_for_each(surface, &compositor->surface_list, link) {
1787 surface->alpha = 255;
1788 weston_surface_damage(surface);
1789 }
1790
1791 activate(compositor->shell, switcher->current, device, time);
1792 wl_list_remove(&switcher->listener.link);
1793 wl_input_device_end_keyboard_grab(&device->input_device, time);
1794 free(switcher);
1795}
1796
1797static void
1798switcher_key(struct wl_keyboard_grab *grab,
1799 uint32_t time, uint32_t key, int32_t state)
1800{
1801 struct switcher *switcher = container_of(grab, struct switcher, grab);
1802 struct weston_input_device *device =
1803 (struct weston_input_device *) grab->input_device;
1804
1805 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1806 switcher_destroy(switcher, time);
1807 } else if (key == KEY_TAB && state) {
1808 switcher_next(switcher);
1809 }
1810};
1811
1812static const struct wl_keyboard_grab_interface switcher_grab = {
1813 switcher_key
1814};
1815
1816static void
1817switcher_binding(struct wl_input_device *device, uint32_t time,
1818 uint32_t key, uint32_t button,
1819 uint32_t state, void *data)
1820{
1821 struct weston_compositor *compositor = data;
1822 struct switcher *switcher;
1823
1824 switcher = malloc(sizeof *switcher);
1825 switcher->compositor = compositor;
1826 switcher->current = NULL;
1827 switcher->listener.func = switcher_handle_surface_destroy;
1828 wl_list_init(&switcher->listener.link);
1829
1830 switcher->grab.interface = &switcher_grab;
1831 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1832 switcher_next(switcher);
1833}
1834
Pekka Paalanen3c647232011-12-22 13:43:43 +02001835static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001836shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001837{
1838 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1839
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001840 if (shell->child.client)
1841 wl_client_destroy(shell->child.client);
1842
Pekka Paalanen3c647232011-12-22 13:43:43 +02001843 free(shell->screensaver.path);
1844 free(shell);
1845}
1846
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001847int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001848shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001849
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001850WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001851shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001852{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001853 struct wl_shell *shell;
1854
1855 shell = malloc(sizeof *shell);
1856 if (shell == NULL)
1857 return -1;
1858
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001859 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001860 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001861 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001862 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001863 shell->shell.map = map;
1864 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001865 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001866
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001867 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001868 wl_list_init(&shell->backgrounds);
1869 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001870 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001871
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001872 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001873
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001874 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1875 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001876 return -1;
1877
Kristian Høgsberg75840622011-09-06 13:48:16 -04001878 if (wl_display_add_global(ec->wl_display,
1879 &desktop_shell_interface,
1880 shell, bind_desktop_shell) == NULL)
1881 return -1;
1882
Pekka Paalanen6e168112011-11-24 11:34:05 +02001883 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1884 shell, bind_screensaver) == NULL)
1885 return -1;
1886
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001887 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001888 if (launch_desktop_shell_process(shell) != 0)
1889 return -1;
1890
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001891 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001892 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001893 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001894 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001895 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001896 MODIFIER_CTRL | MODIFIER_ALT,
1897 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001899 click_to_activate_binding, ec);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001900 weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07001901 zoom_binding, shell);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001902 weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07001903 zoom_binding, shell);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001904 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1905 MODIFIER_SUPER | MODIFIER_ALT,
1906 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001907
Kristian Høgsberg07045392012-02-19 18:52:44 -05001908 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
1909 switcher_binding, ec);
1910
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001911 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001912
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001913 return 0;
1914}