blob: 6572ae3d7ff236b37091cda9b831980cd072e574 [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;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040043
44 struct wl_listener lock_listener;
45 struct wl_listener unlock_listener;
46 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050048 struct weston_layer fullscreen_layer;
49 struct weston_layer panel_layer;
50 struct weston_layer toplevel_layer;
51 struct weston_layer background_layer;
52 struct weston_layer lock_layer;
53
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020056 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020057 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020058
59 unsigned deathcount;
60 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062
63 bool locked;
64 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020065
Pekka Paalanen068ae942011-11-28 14:11:15 +020066 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050067 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010068
69 struct wl_list backgrounds;
70 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020071
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020073 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020074 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 struct wl_resource *binding;
76 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050079
80 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040081};
82
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050083enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020084 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050085
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086 SHELL_SURFACE_PANEL,
87 SHELL_SURFACE_BACKGROUND,
88 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020089 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090
91 SHELL_SURFACE_TOPLEVEL,
92 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080094 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050095 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020096};
97
Pekka Paalanen56cdea92011-11-23 16:14:12 +020098struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020099 struct wl_resource resource;
100
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500101 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200102 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500103 struct shell_surface *parent;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400104 struct wl_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200105
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500106 enum shell_surface_type type;
107 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800108 bool saved_position_valid;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100109
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500110 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200111 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500112 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200113 } rotation;
114
115 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700116 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 uint32_t time;
118 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200119 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500120 int32_t initial_up;
121 } popup;
122
Alex Wu4539b082012-03-01 12:57:46 +0800123 struct {
124 enum wl_shell_surface_fullscreen_method type;
125 struct weston_transform transform; /* matrix from x, y */
126 uint32_t framerate;
127 struct weston_surface *black_surface;
128 } fullscreen;
129
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500130 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500131 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100132 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300133
134 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200135};
136
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300137struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700138 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300139 struct shell_surface *shsurf;
140 struct wl_listener shsurf_destroy_listener;
141};
142
143struct weston_move_grab {
144 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500145 int32_t dx, dy;
146};
147
Pekka Paalanen460099f2012-01-20 16:48:25 +0200148struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300149 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500150 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200151 struct {
152 int32_t x;
153 int32_t y;
154 } center;
155};
156
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500157static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400158destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300159{
160 struct shell_grab *grab;
161
162 grab = container_of(listener, struct shell_grab,
163 shsurf_destroy_listener);
164
165 grab->shsurf = NULL;
166}
167
168static void
169shell_grab_init(struct shell_grab *grab,
170 const struct wl_pointer_grab_interface *interface,
171 struct shell_surface *shsurf)
172{
173 grab->grab.interface = interface;
174 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400175 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
176 wl_signal_add(&shsurf->resource.destroy_signal,
177 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300178
179}
180
181static void
182shell_grab_finish(struct shell_grab *grab)
183{
184 wl_list_remove(&grab->shsurf_destroy_listener.link);
185}
186
187static void
Alex Wu4539b082012-03-01 12:57:46 +0800188center_on_output(struct weston_surface *surface,
189 struct weston_output *output);
190
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300191static struct shell_surface *
192get_shell_surface(struct weston_surface *surface);
193
Alex Wu4539b082012-03-01 12:57:46 +0800194static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200195shell_configuration(struct wl_shell *shell)
196{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200197 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200198 char *path = NULL;
199 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200200
201 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200202 { "path", CONFIG_KEY_STRING, &path },
203 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200204 };
205
206 struct config_section cs[] = {
207 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
208 };
209
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200210 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500211 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200212 free(config_file);
213
Pekka Paalanen7296e792011-12-07 16:22:00 +0200214 shell->screensaver.path = path;
215 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200216}
217
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200218static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400219noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500220 struct wl_surface *surface, int32_t x, int32_t y)
221{
222 grab->focus = NULL;
223}
224
225static void
Scott Moreau447013d2012-02-18 05:05:29 -0700226move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500227 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500230 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300231 struct shell_surface *shsurf = move->base.shsurf;
232 struct weston_surface *es;
233
234 if (!shsurf)
235 return;
236
237 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500238
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500239 weston_surface_configure(es,
240 device->x + move->dx,
241 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200242 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500243}
244
245static void
Scott Moreau447013d2012-02-18 05:05:29 -0700246move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400247 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300249 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
250 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500251 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500252
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500253 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300254 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400255 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500256 free(grab);
257 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258}
259
Scott Moreau447013d2012-02-18 05:05:29 -0700260static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500261 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500262 move_grab_motion,
263 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500264};
265
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400266static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400268 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500269{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500270 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271 struct shell_surface *shsurf = get_shell_surface(es);
272
273 if (!shsurf)
274 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275
276 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400277 if (!move)
278 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280 shell_grab_init(&move->base, &move_grab_interface, shsurf);
281
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200282 move->dx = es->geometry.x - wd->input_device.grab_x;
283 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500284
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300285 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400286 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400288 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400289
290 return 0;
291}
292
293static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200294shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400295 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400296{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500297 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200298 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400299
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500300 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400301 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500302 wd->input_device.pointer_focus != &shsurf->surface->surface)
303 return;
304
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400305 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400306 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500307}
308
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500309struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300310 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200312 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500313};
314
315static void
Scott Moreau447013d2012-02-18 05:05:29 -0700316resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317 uint32_t time, int32_t x, int32_t y)
318{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500319 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500320 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500321 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200322 int32_t from_x, from_y;
323 int32_t to_x, to_y;
324
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300325 if (!resize->base.shsurf)
326 return;
327
328 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200329 device->grab_x, device->grab_y,
330 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200332 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500333
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200334 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200335 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200336 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200337 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500338 } else {
339 width = resize->width;
340 }
341
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200342 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200343 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200344 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200345 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500346 } else {
347 height = resize->height;
348 }
349
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400351 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500352}
353
354static void
Scott Moreau447013d2012-02-18 05:05:29 -0700355resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400356 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500357{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300358 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500359 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500360
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500361 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300362 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400363 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500364 free(grab);
365 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500366}
367
Scott Moreau447013d2012-02-18 05:05:29 -0700368static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500369 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500370 resize_grab_motion,
371 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500372};
373
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400374static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500375weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400376 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500377{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500378 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500379
Alex Wu4539b082012-03-01 12:57:46 +0800380 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
381 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500382
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200383 if (edges == 0 || edges > 15 ||
384 (edges & 3) == 3 || (edges & 12) == 12)
385 return 0;
386
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500387 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400388 if (!resize)
389 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500390
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
392
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500393 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200394 resize->width = shsurf->surface->geometry.width;
395 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400396
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300397 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400398 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500399
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400400 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400401
402 return 0;
403}
404
405static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200406shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400407 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200408 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400409{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500410 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200411 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400412
Alex Wu4539b082012-03-01 12:57:46 +0800413 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
414 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400415
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500416 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400417 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500418 wd->input_device.pointer_focus != &shsurf->surface->surface)
419 return;
420
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400421 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400422 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500423}
424
Juan Zhao96879df2012-02-07 08:45:41 +0800425static struct weston_output *
426get_default_output(struct weston_compositor *compositor)
427{
428 return container_of(compositor->output_list.next,
429 struct weston_output, link);
430}
431
Alex Wu4539b082012-03-01 12:57:46 +0800432static void
433shell_unset_fullscreen(struct shell_surface *shsurf)
434{
435 /* undo all fullscreen things here */
436 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
437 shsurf->fullscreen.framerate = 0;
438 wl_list_remove(&shsurf->fullscreen.transform.link);
439 wl_list_init(&shsurf->fullscreen.transform.link);
440 weston_surface_destroy(shsurf->fullscreen.black_surface);
441 shsurf->fullscreen.black_surface = NULL;
442 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300443 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800444 weston_surface_set_position(shsurf->surface,
445 shsurf->saved_x, shsurf->saved_y);
446}
447
Pekka Paalanen98262232011-12-01 10:42:22 +0200448static int
449reset_shell_surface_type(struct shell_surface *surface)
450{
451 switch (surface->type) {
452 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800453 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200454 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800455 case SHELL_SURFACE_MAXIMIZED:
456 surface->output = get_default_output(surface->surface->compositor);
457 weston_surface_set_position(surface->surface,
458 surface->saved_x,
459 surface->saved_y);
460 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200461 case SHELL_SURFACE_PANEL:
462 case SHELL_SURFACE_BACKGROUND:
463 wl_list_remove(&surface->link);
464 wl_list_init(&surface->link);
465 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200466 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200467 case SHELL_SURFACE_LOCK:
468 wl_resource_post_error(&surface->resource,
469 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200470 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200471 return -1;
472 case SHELL_SURFACE_NONE:
473 case SHELL_SURFACE_TOPLEVEL:
474 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500475 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200476 break;
477 }
478
479 surface->type = SHELL_SURFACE_NONE;
480 return 0;
481}
482
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500483static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200484shell_surface_set_toplevel(struct wl_client *client,
485 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400486
487{
Pekka Paalanen98262232011-12-01 10:42:22 +0200488 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400489
Pekka Paalanen98262232011-12-01 10:42:22 +0200490 if (reset_shell_surface_type(surface))
491 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400492
Pekka Paalanen98262232011-12-01 10:42:22 +0200493 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400494}
495
496static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200497shell_surface_set_transient(struct wl_client *client,
498 struct wl_resource *resource,
499 struct wl_resource *parent_resource,
500 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400501{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200502 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500503 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200504 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500505 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400506
Pekka Paalanen98262232011-12-01 10:42:22 +0200507 if (reset_shell_surface_type(shsurf))
508 return;
509
Alex Wu4539b082012-03-01 12:57:46 +0800510 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800511 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200512 weston_surface_set_position(es, pes->geometry.x + x,
513 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400514
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200515 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400516}
517
Juan Zhao96879df2012-02-07 08:45:41 +0800518static struct wl_shell *
519shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200520{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400521 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800522}
523
524static int
525get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
526{
527 struct shell_surface *priv;
528 int panel_height = 0;
529
530 if (!output)
531 return 0;
532
533 wl_list_for_each(priv, &wlshell->panels, link) {
534 if (priv->output == output) {
535 panel_height = priv->surface->geometry.height;
536 break;
537 }
538 }
539 return panel_height;
540}
541
542static void
543shell_surface_set_maximized(struct wl_client *client,
544 struct wl_resource *resource,
545 struct wl_resource *output_resource )
546{
547 struct shell_surface *shsurf = resource->data;
548 struct weston_surface *es = shsurf->surface;
549 struct wl_shell *wlshell = NULL;
550 uint32_t edges = 0, panel_height = 0;
551
552 /* get the default output, if the client set it as NULL
553 check whether the ouput is available */
554 if (output_resource)
555 shsurf->output = output_resource->data;
556 else
557 shsurf->output = get_default_output(es->compositor);
558
559 if (reset_shell_surface_type(shsurf))
560 return;
561
562 shsurf->saved_x = es->geometry.x;
563 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800564 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800565
566 wlshell = shell_surface_get_shell(shsurf);
567 panel_height = get_output_panel_height(wlshell, es->output);
568 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500569
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400570 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500571 es->output->current->width,
572 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800573
574 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200575}
576
Alex Wu21858432012-04-01 20:13:08 +0800577static void
578black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
579
Alex Wu4539b082012-03-01 12:57:46 +0800580static struct weston_surface *
581create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800582 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800583 GLfloat x, GLfloat y, int w, int h)
584{
585 struct weston_surface *surface = NULL;
586
587 surface = weston_surface_create(ec);
588 if (surface == NULL) {
589 fprintf(stderr, "no memory\n");
590 return NULL;
591 }
592
Alex Wu21858432012-04-01 20:13:08 +0800593 surface->configure = black_surface_configure;
594 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800595 weston_surface_configure(surface, x, y, w, h);
596 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
597 return surface;
598}
599
600/* Create black surface and append it to the associated fullscreen surface.
601 * Handle size dismatch and positioning according to the method. */
602static void
603shell_configure_fullscreen(struct shell_surface *shsurf)
604{
605 struct weston_output *output = shsurf->fullscreen_output;
606 struct weston_surface *surface = shsurf->surface;
607 struct weston_matrix *matrix;
608 float scale;
609
610 center_on_output(surface, output);
611
612 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500613 shsurf->fullscreen.black_surface =
614 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800615 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500616 output->x, output->y,
617 output->current->width,
618 output->current->height);
619
620 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
621 wl_list_insert(&surface->layer_link,
622 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800623 shsurf->fullscreen.black_surface->output = output;
624
625 switch (shsurf->fullscreen.type) {
626 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
627 break;
628 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
629 matrix = &shsurf->fullscreen.transform.matrix;
630 weston_matrix_init(matrix);
631 scale = (float)output->current->width/(float)surface->geometry.width;
632 weston_matrix_scale(matrix, scale, scale, 1);
633 wl_list_remove(&shsurf->fullscreen.transform.link);
634 wl_list_insert(surface->geometry.transformation_list.prev,
635 &shsurf->fullscreen.transform.link);
636 weston_surface_set_position(surface, output->x, output->y);
637 break;
638 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
639 break;
640 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
641 break;
642 default:
643 break;
644 }
645}
646
647/* make the fullscreen and black surface at the top */
648static void
649shell_stack_fullscreen(struct shell_surface *shsurf)
650{
651 struct weston_surface *surface = shsurf->surface;
652 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800653
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500654 wl_list_remove(&surface->layer_link);
655 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800656
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500657 wl_list_insert(&shell->fullscreen_layer.surface_list,
658 &surface->layer_link);
659 wl_list_insert(&surface->layer_link,
660 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800661
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500662 weston_surface_damage(surface);
663 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800664}
665
666static void
667shell_map_fullscreen(struct shell_surface *shsurf)
668{
669 shell_configure_fullscreen(shsurf);
670 shell_stack_fullscreen(shsurf);
671}
672
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400673static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200674shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500675 struct wl_resource *resource,
676 uint32_t method,
677 uint32_t framerate,
678 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400679{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200680 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500681 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800682
683 if (output_resource)
684 shsurf->output = output_resource->data;
685 else
686 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400687
Pekka Paalanen98262232011-12-01 10:42:22 +0200688 if (reset_shell_surface_type(shsurf))
689 return;
690
Alex Wu4539b082012-03-01 12:57:46 +0800691 shsurf->fullscreen_output = shsurf->output;
692 shsurf->fullscreen.type = method;
693 shsurf->fullscreen.framerate = framerate;
694 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400695
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200696 shsurf->saved_x = es->geometry.x;
697 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800698 shsurf->saved_position_valid = true;
699
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300700 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300701 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500702
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400703 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500704 shsurf->output->current->width,
705 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400706}
707
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500708static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400709popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500710 struct wl_surface *surface, int32_t x, int32_t y)
711{
712 struct wl_input_device *device = grab->input_device;
713 struct shell_surface *priv =
714 container_of(grab, struct shell_surface, popup.grab);
715 struct wl_client *client = priv->surface->surface.resource.client;
716
Pekka Paalanencb108432012-01-19 16:25:40 +0200717 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400718 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500719 grab->focus = surface;
720 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400721 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500722 grab->focus = NULL;
723 }
724}
725
726static void
Scott Moreau447013d2012-02-18 05:05:29 -0700727popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200728 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500729{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500730 struct wl_resource *resource;
731
732 resource = grab->input_device->pointer_focus_resource;
733 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500734 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500735}
736
737static void
Scott Moreau447013d2012-02-18 05:05:29 -0700738popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400739 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500740{
741 struct wl_resource *resource;
742 struct shell_surface *shsurf =
743 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400744 struct wl_display *display;
745 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500746
747 resource = grab->input_device->pointer_focus_resource;
748 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400749 display = wl_client_get_display(resource->client);
750 serial = wl_display_get_serial(display);
751 wl_input_device_send_button(resource, serial,
752 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500753 } else if (state == 0 &&
754 (shsurf->popup.initial_up ||
755 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500756 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400757 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500758 shsurf->popup.grab.input_device = NULL;
759 }
760
761 if (state == 0)
762 shsurf->popup.initial_up = 1;
763}
764
Scott Moreau447013d2012-02-18 05:05:29 -0700765static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500766 popup_grab_focus,
767 popup_grab_motion,
768 popup_grab_button,
769};
770
771static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400772shell_map_popup(struct shell_surface *shsurf, uint32_t serial)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500773{
774 struct wl_input_device *device;
775 struct weston_surface *es = shsurf->surface;
776 struct weston_surface *parent = shsurf->parent->surface;
777
778 es->output = parent->output;
779
780 shsurf->popup.grab.interface = &popup_grab_interface;
781 device = es->compositor->input_device;
782
Pekka Paalanen938269a2012-02-07 14:19:01 +0200783 weston_surface_update_transform(parent);
784 if (parent->transform.enabled) {
785 shsurf->popup.parent_transform.matrix =
786 parent->transform.matrix;
787 } else {
788 /* construct x, y translation matrix */
789 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
790 shsurf->popup.parent_transform.matrix.d[12] =
791 parent->geometry.x;
792 shsurf->popup.parent_transform.matrix.d[13] =
793 parent->geometry.y;
794 }
795 wl_list_insert(es->geometry.transformation_list.prev,
796 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200797 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500798
799 shsurf->popup.grab.input_device = device;
800 shsurf->popup.time = device->grab_time;
801 shsurf->popup.initial_up = 0;
802
Scott Moreau447013d2012-02-18 05:05:29 -0700803 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400804 &shsurf->popup.grab);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500805}
806
807static void
808shell_surface_set_popup(struct wl_client *client,
809 struct wl_resource *resource,
810 struct wl_resource *input_device_resource,
811 uint32_t time,
812 struct wl_resource *parent_resource,
813 int32_t x, int32_t y, uint32_t flags)
814{
815 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500816
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500817 shsurf->type = SHELL_SURFACE_POPUP;
818 shsurf->parent = parent_resource->data;
819 shsurf->popup.x = x;
820 shsurf->popup.y = y;
821}
822
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200823static const struct wl_shell_surface_interface shell_surface_implementation = {
824 shell_surface_move,
825 shell_surface_resize,
826 shell_surface_set_toplevel,
827 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500828 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800829 shell_surface_set_popup,
830 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200831};
832
833static void
834destroy_shell_surface(struct wl_resource *resource)
835{
836 struct shell_surface *shsurf = resource->data;
837
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500838 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400839 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500840
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200841 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300842 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200843 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300844 shsurf->surface->configure = NULL;
845 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200846
Alex Wuaa08e2d2012-03-05 11:01:40 +0800847 if (shsurf->fullscreen.black_surface)
848 weston_surface_destroy(shsurf->fullscreen.black_surface);
849
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200850 wl_list_remove(&shsurf->link);
851 free(shsurf);
852}
853
854static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400855shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200856{
857 struct shell_surface *shsurf = container_of(listener,
858 struct shell_surface,
859 surface_destroy_listener);
860
861 shsurf->surface = NULL;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400862 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200863}
864
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200865static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500866get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200867{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200868 struct wl_listener *listener;
869
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400870 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
871 shell_handle_surface_destroy);
872 if (listener)
873 return container_of(listener, struct shell_surface,
874 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200875
876 return NULL;
877}
878
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200879static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300880shell_surface_configure(struct weston_surface *, int32_t, int32_t);
881
882static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200883shell_get_shell_surface(struct wl_client *client,
884 struct wl_resource *resource,
885 uint32_t id,
886 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200887{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500888 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200889 struct shell_surface *shsurf;
890
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200891 if (get_shell_surface(surface)) {
892 wl_resource_post_error(surface_resource,
893 WL_DISPLAY_ERROR_INVALID_OBJECT,
894 "wl_shell::get_shell_surface already requested");
895 return;
896 }
897
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300898 if (surface->configure) {
899 wl_resource_post_error(surface_resource,
900 WL_DISPLAY_ERROR_INVALID_OBJECT,
901 "surface->configure already set");
902 return;
903 }
904
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200905 shsurf = calloc(1, sizeof *shsurf);
906 if (!shsurf) {
907 wl_resource_post_no_memory(resource);
908 return;
909 }
910
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300911 surface->configure = shell_surface_configure;
912
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200913 shsurf->resource.destroy = destroy_shell_surface;
914 shsurf->resource.object.id = id;
915 shsurf->resource.object.interface = &wl_shell_surface_interface;
916 shsurf->resource.object.implementation =
917 (void (**)(void)) &shell_surface_implementation;
918 shsurf->resource.data = shsurf;
919
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400920 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +0800921 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200922 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800923 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
924 shsurf->fullscreen.framerate = 0;
925 shsurf->fullscreen.black_surface = NULL;
926 wl_list_init(&shsurf->fullscreen.transform.link);
927
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400928 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
929 wl_signal_add(&surface->surface.resource.destroy_signal,
930 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200931
932 /* init link so its safe to always remove it in destroy_shell_surface */
933 wl_list_init(&shsurf->link);
934
Pekka Paalanen460099f2012-01-20 16:48:25 +0200935 /* empty when not in use */
936 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500937 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200938
Pekka Paalanen98262232011-12-01 10:42:22 +0200939 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200940
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200941 wl_client_add_resource(client, &shsurf->resource);
942}
943
944static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200945 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500946};
947
Kristian Høgsberg07937562011-04-12 17:25:42 -0400948static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500949handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200950{
951 proc->pid = 0;
952}
953
954static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200955launch_screensaver(struct wl_shell *shell)
956{
957 if (shell->screensaver.binding)
958 return;
959
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200960 if (!shell->screensaver.path)
961 return;
962
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500963 if (shell->screensaver.process.pid != 0) {
964 fprintf(stderr, "old screensaver still running\n");
965 return;
966 }
967
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500968 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200969 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200970 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200971 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200972}
973
974static void
975terminate_screensaver(struct wl_shell *shell)
976{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200977 if (shell->screensaver.process.pid == 0)
978 return;
979
980 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200981}
982
983static void
984show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
985{
986 struct wl_list *list;
987
988 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200990 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500991 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200992
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500993 wl_list_remove(&surface->surface->layer_link);
994 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200995 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200996 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200997}
998
999static void
1000hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
1001{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001002 wl_list_remove(&surface->surface->layer_link);
1003 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001004 surface->surface->output = NULL;
1005}
1006
1007static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001008desktop_shell_set_background(struct wl_client *client,
1009 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001010 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001011 struct wl_resource *surface_resource)
1012{
1013 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001014 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001015 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001016 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001017
Pekka Paalanen98262232011-12-01 10:42:22 +02001018 if (reset_shell_surface_type(shsurf))
1019 return;
1020
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001021 wl_list_for_each(priv, &shell->backgrounds, link) {
1022 if (priv->output == output_resource->data) {
1023 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001024 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001025 wl_list_remove(&priv->link);
1026 break;
1027 }
1028 }
1029
Pekka Paalanen068ae942011-11-28 14:11:15 +02001030 shsurf->type = SHELL_SURFACE_BACKGROUND;
1031 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001032
Pekka Paalanen068ae942011-11-28 14:11:15 +02001033 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001034
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001035 weston_surface_set_position(surface, shsurf->output->x,
1036 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001037
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001038 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001039 surface_resource,
1040 shsurf->output->current->width,
1041 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001042}
1043
1044static void
1045desktop_shell_set_panel(struct wl_client *client,
1046 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001047 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001048 struct wl_resource *surface_resource)
1049{
1050 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001051 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001052 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001053 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001054
Pekka Paalanen98262232011-12-01 10:42:22 +02001055 if (reset_shell_surface_type(shsurf))
1056 return;
1057
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001058 wl_list_for_each(priv, &shell->panels, link) {
1059 if (priv->output == output_resource->data) {
1060 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001061 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001062 wl_list_remove(&priv->link);
1063 break;
1064 }
1065 }
1066
Pekka Paalanen068ae942011-11-28 14:11:15 +02001067 shsurf->type = SHELL_SURFACE_PANEL;
1068 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001069
Pekka Paalanen068ae942011-11-28 14:11:15 +02001070 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001071
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001072 weston_surface_set_position(surface, shsurf->output->x,
1073 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001074
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001075 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001076 surface_resource,
1077 shsurf->output->current->width,
1078 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001079}
1080
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001081static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001082handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001083{
1084 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001085 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001086
1087 fprintf(stderr, "lock surface gone\n");
1088 shell->lock_surface = NULL;
1089}
1090
1091static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001092desktop_shell_set_lock_surface(struct wl_client *client,
1093 struct wl_resource *resource,
1094 struct wl_resource *surface_resource)
1095{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001096 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001097 struct shell_surface *surface = surface_resource->data;
1098
1099 if (reset_shell_surface_type(surface))
1100 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001101
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001102 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001103
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001104 if (!shell->locked)
1105 return;
1106
Pekka Paalanen98262232011-12-01 10:42:22 +02001107 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001108
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001109 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1110 wl_signal_add(&surface_resource->destroy_signal,
1111 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001112
Pekka Paalanen068ae942011-11-28 14:11:15 +02001113 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001114}
1115
1116static void
1117resume_desktop(struct wl_shell *shell)
1118{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001119 struct shell_surface *tmp;
1120
1121 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1122 hide_screensaver(shell, tmp);
1123
1124 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001125
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001126 wl_list_remove(&shell->lock_layer.link);
1127 wl_list_insert(&shell->compositor->cursor_layer.link,
1128 &shell->fullscreen_layer.link);
1129 wl_list_insert(&shell->fullscreen_layer.link,
1130 &shell->panel_layer.link);
1131 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001132
1133 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001134 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001135 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001136 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001137}
1138
1139static void
1140desktop_shell_unlock(struct wl_client *client,
1141 struct wl_resource *resource)
1142{
1143 struct wl_shell *shell = resource->data;
1144
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001145 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001146
1147 if (shell->locked)
1148 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001149}
1150
Kristian Høgsberg75840622011-09-06 13:48:16 -04001151static const struct desktop_shell_interface desktop_shell_implementation = {
1152 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001153 desktop_shell_set_panel,
1154 desktop_shell_set_lock_surface,
1155 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001156};
1157
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001158static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001159get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001160{
1161 struct shell_surface *shsurf;
1162
1163 shsurf = get_shell_surface(surface);
1164 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001165 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001166 return shsurf->type;
1167}
1168
Kristian Høgsberg75840622011-09-06 13:48:16 -04001169static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001170move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001171 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001172{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001173 struct weston_surface *surface =
1174 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001175
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001176 if (surface == NULL)
1177 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001178
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001179 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001180 case SHELL_SURFACE_PANEL:
1181 case SHELL_SURFACE_BACKGROUND:
1182 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001183 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001184 return;
1185 default:
1186 break;
1187 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001188
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001189 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001190}
1191
1192static void
1193resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001194 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001195{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001196 struct weston_surface *surface =
1197 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001198 uint32_t edges = 0;
1199 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001200 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001201
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001202 if (surface == NULL)
1203 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001204
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001205 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001206 if (!shsurf)
1207 return;
1208
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001209 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001210 case SHELL_SURFACE_PANEL:
1211 case SHELL_SURFACE_BACKGROUND:
1212 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001213 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001214 return;
1215 default:
1216 break;
1217 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001218
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001219 weston_surface_from_global(surface,
1220 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001221
Pekka Paalanen60921e52012-01-25 15:55:43 +02001222 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001223 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001224 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001225 edges |= 0;
1226 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001227 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001228
Pekka Paalanen60921e52012-01-25 15:55:43 +02001229 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001230 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001231 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001232 edges |= 0;
1233 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001234 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001235
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001236 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001237 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001238}
1239
1240static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001241surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1242 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1243{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001244 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001245 struct shell_surface *shsurf;
1246 struct weston_surface *surface =
1247 (struct weston_surface *) device->pointer_focus;
1248
1249 if (surface == NULL)
1250 return;
1251
1252 shsurf = get_shell_surface(surface);
1253 if (!shsurf)
1254 return;
1255
1256 switch (shsurf->type) {
1257 case SHELL_SURFACE_BACKGROUND:
1258 case SHELL_SURFACE_SCREENSAVER:
1259 return;
1260 default:
1261 break;
1262 }
1263
1264 surface->alpha += value * step;
1265
1266 if (surface->alpha > 255)
1267 surface->alpha = 255;
1268 if (surface->alpha < step)
1269 surface->alpha = step;
1270
1271 surface->geometry.dirty = 1;
1272 weston_surface_damage(surface);
1273}
1274
1275static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001276zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001277 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001278{
1279 struct weston_input_device *wd = (struct weston_input_device *) device;
1280 struct weston_compositor *compositor = wd->compositor;
1281 struct weston_output *output;
1282
1283 wl_list_for_each(output, &compositor->output_list, link) {
1284 if (pixman_region32_contains_point(&output->region,
1285 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001286 output->zoom.active = 1;
1287 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001288
1289 if (output->zoom.level >= 1.0) {
1290 output->zoom.active = 0;
1291 output->zoom.level = 1.0;
1292 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001293
1294 if (output->zoom.level < output->zoom.increment)
1295 output->zoom.level = output->zoom.increment;
1296
1297 weston_output_update_zoom(output, device->x, device->y);
1298 }
1299 }
1300}
1301
Scott Moreauccbf29d2012-02-22 14:21:41 -07001302static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001303terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001304 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001305{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001306 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001307
1308 if (state)
1309 wl_display_terminate(compositor->wl_display);
1310}
1311
1312static void
Scott Moreau447013d2012-02-18 05:05:29 -07001313rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001314 uint32_t time, int32_t x, int32_t y)
1315{
1316 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001317 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001318 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001319 struct shell_surface *shsurf = rotate->base.shsurf;
1320 struct weston_surface *surface;
1321 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1322
1323 if (!shsurf)
1324 return;
1325
1326 surface = shsurf->surface;
1327
1328 cx = 0.5f * surface->geometry.width;
1329 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001330
1331 dx = device->x - rotate->center.x;
1332 dy = device->y - rotate->center.y;
1333 r = sqrtf(dx * dx + dy * dy);
1334
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001335 wl_list_remove(&shsurf->rotation.transform.link);
1336 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001337
1338 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001339 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001340 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001341
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001342 weston_matrix_init(&rotate->rotation);
1343 rotate->rotation.d[0] = dx / r;
1344 rotate->rotation.d[4] = -dy / r;
1345 rotate->rotation.d[1] = -rotate->rotation.d[4];
1346 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001347
1348 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001349 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001350 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001351 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001352 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001353
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001354 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001355 &shsurf->surface->geometry.transformation_list,
1356 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001357 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001358 wl_list_init(&shsurf->rotation.transform.link);
1359 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001360 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001361 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001362
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001363 /* We need to adjust the position of the surface
1364 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001365 cposx = surface->geometry.x + cx;
1366 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001367 dposx = rotate->center.x - cposx;
1368 dposy = rotate->center.y - cposy;
1369 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001370 weston_surface_set_position(surface,
1371 surface->geometry.x + dposx,
1372 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001373 }
1374
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001375 /* Repaint implies weston_surface_update_transform(), which
1376 * lazily applies the damage due to rotation update.
1377 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001378 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001379}
1380
1381static void
Scott Moreau447013d2012-02-18 05:05:29 -07001382rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001383 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001384{
1385 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001386 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001387 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001388 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001389
1390 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001391 if (shsurf)
1392 weston_matrix_multiply(&shsurf->rotation.rotation,
1393 &rotate->rotation);
1394 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001395 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001396 free(rotate);
1397 }
1398}
1399
Scott Moreau447013d2012-02-18 05:05:29 -07001400static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001401 noop_grab_focus,
1402 rotate_grab_motion,
1403 rotate_grab_button,
1404};
1405
1406static void
1407rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001408 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001409{
1410 struct weston_surface *base_surface =
1411 (struct weston_surface *) device->pointer_focus;
1412 struct shell_surface *surface;
1413 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001414 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001415 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001416
1417 if (base_surface == NULL)
1418 return;
1419
1420 surface = get_shell_surface(base_surface);
1421 if (!surface)
1422 return;
1423
1424 switch (surface->type) {
1425 case SHELL_SURFACE_PANEL:
1426 case SHELL_SURFACE_BACKGROUND:
1427 case SHELL_SURFACE_FULLSCREEN:
1428 case SHELL_SURFACE_SCREENSAVER:
1429 return;
1430 default:
1431 break;
1432 }
1433
Pekka Paalanen460099f2012-01-20 16:48:25 +02001434 rotate = malloc(sizeof *rotate);
1435 if (!rotate)
1436 return;
1437
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001438 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001439
1440 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001441 surface->surface->geometry.width / 2,
1442 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001443 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001444
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001445 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001446
1447 dx = device->x - rotate->center.x;
1448 dy = device->y - rotate->center.y;
1449 r = sqrtf(dx * dx + dy * dy);
1450 if (r > 20.0f) {
1451 struct weston_matrix inverse;
1452
1453 weston_matrix_init(&inverse);
1454 inverse.d[0] = dx / r;
1455 inverse.d[4] = dy / r;
1456 inverse.d[1] = -inverse.d[4];
1457 inverse.d[5] = inverse.d[0];
1458 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001459
1460 weston_matrix_init(&rotate->rotation);
1461 rotate->rotation.d[0] = dx / r;
1462 rotate->rotation.d[4] = -dy / r;
1463 rotate->rotation.d[1] = -rotate->rotation.d[4];
1464 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001465 } else {
1466 weston_matrix_init(&surface->rotation.rotation);
1467 weston_matrix_init(&rotate->rotation);
1468 }
1469
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001470 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001471}
1472
1473static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001474activate(struct wl_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001475 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001476{
Alex Wu21858432012-04-01 20:13:08 +08001477 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001478
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001479 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001480
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001481 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001482 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001483 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001484 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001485 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001486
Pekka Paalanen77346a62011-11-30 16:26:35 +02001487 case SHELL_SURFACE_SCREENSAVER:
1488 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001489 if (shell->lock_surface)
1490 weston_surface_restack(es,
1491 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001492 break;
Alex Wu4539b082012-03-01 12:57:46 +08001493 case SHELL_SURFACE_FULLSCREEN:
1494 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001495 shell_stack_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001496 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001497 default:
Alex Wu21858432012-04-01 20:13:08 +08001498 /* move the fullscreen surfaces down into the toplevel layer */
1499 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1500 wl_list_for_each_reverse_safe(surf,
1501 prev,
1502 &shell->fullscreen_layer.surface_list,
1503 layer_link)
1504 weston_surface_restack(surf,
1505 &shell->toplevel_layer.surface_list);
1506 }
1507
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001508 weston_surface_restack(es,
1509 &shell->toplevel_layer.surface_list);
1510 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001511 }
1512}
1513
Alex Wu21858432012-04-01 20:13:08 +08001514/* no-op func for checking black surface */
1515static void
1516black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1517{
1518}
1519
1520static bool
1521is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1522{
1523 if (es->configure == black_surface_configure) {
1524 if (fs_surface)
1525 *fs_surface = (struct weston_surface *)es->private;
1526 return true;
1527 }
1528 return false;
1529}
1530
Kristian Høgsberg75840622011-09-06 13:48:16 -04001531static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001532click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001533 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001534 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001535{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 struct weston_input_device *wd = (struct weston_input_device *) device;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001537 struct wl_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001539 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001540
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001541 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001542 if (!focus)
1543 return;
1544
Alex Wu21858432012-04-01 20:13:08 +08001545 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001546 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001547
Alex Wu9c35e6b2012-03-05 14:13:13 +08001548 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001549 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001550}
1551
1552static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001553lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001554{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001555 struct wl_shell *shell =
1556 container_of(listener, struct wl_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001557 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001558 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001559 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001560
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001561 if (shell->locked) {
1562 wl_list_for_each(output, &shell->compositor->output_list, link)
1563 /* TODO: find a way to jump to other DPMS levels */
1564 if (output->set_dpms)
1565 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001566 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001567 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001568
1569 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001570
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001571 /* Hide all surfaces by removing the fullscreen, panel and
1572 * toplevel layers. This way nothing else can show or receive
1573 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001574
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001575 wl_list_remove(&shell->panel_layer.link);
1576 wl_list_remove(&shell->toplevel_layer.link);
1577 wl_list_remove(&shell->fullscreen_layer.link);
1578 wl_list_insert(&shell->compositor->cursor_layer.link,
1579 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001580
Pekka Paalanen77346a62011-11-30 16:26:35 +02001581 launch_screensaver(shell);
1582
1583 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1584 show_screensaver(shell, shsurf);
1585
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001586 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001587 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001588 weston_compositor_wake(shell->compositor);
1589 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001590 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001591
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001592 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001593 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001594
1595 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001596 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1597 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001598 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001599 }
1600
1601 /* TODO: disable bindings that should not work while locked. */
1602
1603 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001604}
1605
1606static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001607unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001608{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001609 struct wl_shell *shell =
1610 container_of(listener, struct wl_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001611
Pekka Paalanend81c2162011-11-16 13:47:34 +02001612 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001613 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001614 return;
1615 }
1616
1617 /* If desktop-shell client has gone away, unlock immediately. */
1618 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001619 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001620 return;
1621 }
1622
1623 if (shell->prepare_event_sent)
1624 return;
1625
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001626 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001627 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001628}
1629
1630static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001631center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001632{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001633 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001634 GLfloat x = (mode->width - surface->geometry.width) / 2;
1635 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001636
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001637 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001638}
1639
1640static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001641map(struct wl_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001642 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001643{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001644 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001645 struct shell_surface *shsurf;
1646 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001647 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001648 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001649
Pekka Paalanen77346a62011-11-30 16:26:35 +02001650 shsurf = get_shell_surface(surface);
1651 if (shsurf)
1652 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001653
Pekka Paalanen60921e52012-01-25 15:55:43 +02001654 surface->geometry.width = width;
1655 surface->geometry.height = height;
1656 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001657
1658 /* initial positioning, see also configure() */
1659 switch (surface_type) {
1660 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001661 weston_surface_set_position(surface, 10 + random() % 400,
1662 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001663 break;
1664 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001665 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001666 break;
Alex Wu4539b082012-03-01 12:57:46 +08001667 case SHELL_SURFACE_FULLSCREEN:
1668 shell_map_fullscreen(shsurf);
1669 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001670 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001671 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001672 panel_height = get_output_panel_height(shell,surface->output);
1673 weston_surface_set_position(surface, surface->output->x,
1674 surface->output->y + panel_height);
1675 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001676 case SHELL_SURFACE_LOCK:
1677 center_on_output(surface, get_default_output(compositor));
1678 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001679 case SHELL_SURFACE_POPUP:
1680 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001681 case SHELL_SURFACE_NONE:
1682 weston_surface_set_position(surface,
1683 surface->geometry.x + sx,
1684 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001685 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001686 default:
1687 ;
1688 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001689
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001690 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001691 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001692 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001693 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001694 wl_list_insert(&shell->background_layer.surface_list,
1695 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001696 break;
1697 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001698 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001699 wl_list_insert(&shell->panel_layer.surface_list,
1700 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001701 break;
1702 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001703 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 wl_list_insert(&shell->lock_layer.surface_list,
1705 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001706 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001707 break;
1708 case SHELL_SURFACE_SCREENSAVER:
1709 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001710 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001711 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001712 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001713 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001714 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001715 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001716 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001717 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001718 case SHELL_SURFACE_POPUP:
1719 case SHELL_SURFACE_TRANSIENT:
1720 parent = shsurf->parent->surface;
1721 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1722 break;
Alex Wu4539b082012-03-01 12:57:46 +08001723 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001724 case SHELL_SURFACE_NONE:
1725 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001726 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001727 wl_list_insert(&shell->toplevel_layer.surface_list,
1728 &surface->layer_link);
1729 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001730 }
1731
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001732 if (surface_type != SHELL_SURFACE_NONE) {
1733 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001734 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1735 surface->output = shsurf->output;
1736 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001737
Juan Zhao7bb92f02011-12-15 11:31:51 -05001738 switch (surface_type) {
1739 case SHELL_SURFACE_TOPLEVEL:
1740 case SHELL_SURFACE_TRANSIENT:
1741 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001742 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001743 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001744 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001745 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001746 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001747 break;
1748 default:
1749 break;
1750 }
1751
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001752 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001753 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001754}
1755
1756static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001757configure(struct wl_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001758 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001759{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001760 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001761 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001762 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001763
Pekka Paalanen77346a62011-11-30 16:26:35 +02001764 shsurf = get_shell_surface(surface);
1765 if (shsurf)
1766 surface_type = shsurf->type;
1767
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001768 surface->geometry.x = x;
1769 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001770 surface->geometry.width = width;
1771 surface->geometry.height = height;
1772 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001773
1774 switch (surface_type) {
1775 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001776 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001777 break;
Alex Wu4539b082012-03-01 12:57:46 +08001778 case SHELL_SURFACE_FULLSCREEN:
1779 shell_configure_fullscreen(shsurf);
1780 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1781 shell_stack_fullscreen(shsurf);
1782 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001783 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001784 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001785 surface->geometry.x = surface->output->x;
1786 surface->geometry.y = surface->output->y +
1787 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001788 break;
Alex Wu4539b082012-03-01 12:57:46 +08001789 case SHELL_SURFACE_TOPLEVEL:
1790 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001791 default:
1792 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001793 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001794
Alex Wu4539b082012-03-01 12:57:46 +08001795 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001796 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001797 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001798
1799 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1800 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001801 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1802 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001803 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001804}
1805
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001806static void
1807shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1808{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001809 struct shell_surface *shsurf = get_shell_surface(es);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001810 struct wl_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001811
1812 if (!weston_surface_is_mapped(es)) {
1813 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001814 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001815 es->geometry.width != es->buffer->width ||
1816 es->geometry.height != es->buffer->height) {
1817 GLfloat from_x, from_y;
1818 GLfloat to_x, to_y;
1819
1820 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1821 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1822 configure(shell, es,
1823 es->geometry.x + to_x - from_x,
1824 es->geometry.y + to_y - from_y,
1825 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001826 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001827 }
1828}
1829
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001830static int launch_desktop_shell_process(struct wl_shell *shell);
1831
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001832static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001834{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001835 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001836 struct wl_shell *shell =
1837 container_of(process, struct wl_shell, child.process);
1838
1839 shell->child.process.pid = 0;
1840 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001841
1842 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1843 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001844 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001845 shell->child.deathstamp = time;
1846 shell->child.deathcount = 0;
1847 }
1848
1849 shell->child.deathcount++;
1850 if (shell->child.deathcount > 5) {
1851 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1852 return;
1853 }
1854
1855 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1856 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001857}
1858
1859static int
1860launch_desktop_shell_process(struct wl_shell *shell)
1861{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001862 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001863
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001864 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001865 &shell->child.process,
1866 shell_exe,
1867 desktop_shell_sigchld);
1868
1869 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001870 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001871 return 0;
1872}
1873
1874static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001875bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1876{
1877 struct wl_shell *shell = data;
1878
1879 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001880 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001881}
1882
Kristian Høgsberg75840622011-09-06 13:48:16 -04001883static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001884unbind_desktop_shell(struct wl_resource *resource)
1885{
1886 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001887
1888 if (shell->locked)
1889 resume_desktop(shell);
1890
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001891 shell->child.desktop_shell = NULL;
1892 shell->prepare_event_sent = false;
1893 free(resource);
1894}
1895
1896static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001897bind_desktop_shell(struct wl_client *client,
1898 void *data, uint32_t version, uint32_t id)
1899{
1900 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001901 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001902
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001903 resource = wl_client_add_object(client, &desktop_shell_interface,
1904 &desktop_shell_implementation,
1905 id, shell);
1906
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001907 if (client == shell->child.client) {
1908 resource->destroy = unbind_desktop_shell;
1909 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001910 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001911 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001912
1913 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1914 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001915 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001916}
1917
Pekka Paalanen6e168112011-11-24 11:34:05 +02001918static void
1919screensaver_set_surface(struct wl_client *client,
1920 struct wl_resource *resource,
1921 struct wl_resource *shell_surface_resource,
1922 struct wl_resource *output_resource)
1923{
1924 struct wl_shell *shell = resource->data;
1925 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001926 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001927
Pekka Paalanen98262232011-12-01 10:42:22 +02001928 if (reset_shell_surface_type(surface))
1929 return;
1930
Pekka Paalanen77346a62011-11-30 16:26:35 +02001931 surface->type = SHELL_SURFACE_SCREENSAVER;
1932
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001933 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001934 surface->output = output;
1935 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001936}
1937
1938static const struct screensaver_interface screensaver_implementation = {
1939 screensaver_set_surface
1940};
1941
1942static void
1943unbind_screensaver(struct wl_resource *resource)
1944{
1945 struct wl_shell *shell = resource->data;
1946
Pekka Paalanen77346a62011-11-30 16:26:35 +02001947 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001948 free(resource);
1949}
1950
1951static void
1952bind_screensaver(struct wl_client *client,
1953 void *data, uint32_t version, uint32_t id)
1954{
1955 struct wl_shell *shell = data;
1956 struct wl_resource *resource;
1957
1958 resource = wl_client_add_object(client, &screensaver_interface,
1959 &screensaver_implementation,
1960 id, shell);
1961
Pekka Paalanen77346a62011-11-30 16:26:35 +02001962 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001963 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001964 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001965 return;
1966 }
1967
1968 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1969 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001970 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001971}
1972
Kristian Høgsberg07045392012-02-19 18:52:44 -05001973struct switcher {
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001974 struct wl_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001975 struct weston_surface *current;
1976 struct wl_listener listener;
1977 struct wl_keyboard_grab grab;
1978};
1979
1980static void
1981switcher_next(struct switcher *switcher)
1982{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001983 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001984 struct weston_surface *surface;
1985 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04001986 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001987
1988 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05001989 switch (get_shell_surface_type(surface)) {
1990 case SHELL_SURFACE_TOPLEVEL:
1991 case SHELL_SURFACE_FULLSCREEN:
1992 case SHELL_SURFACE_MAXIMIZED:
1993 if (first == NULL)
1994 first = surface;
1995 if (prev == switcher->current)
1996 next = surface;
1997 prev = surface;
1998 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001999 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002000 weston_surface_damage(surface);
2001 break;
2002 default:
2003 break;
2004 }
Alex Wu1659daa2012-04-01 20:13:09 +08002005
2006 if (is_black_surface(surface, NULL)) {
2007 surface->alpha = 64;
2008 surface->geometry.dirty = 1;
2009 weston_surface_damage(surface);
2010 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002011 }
2012
2013 if (next == NULL)
2014 next = first;
2015
Alex Wu07b26062012-03-12 16:06:01 +08002016 if (next == NULL)
2017 return;
2018
Kristian Høgsberg07045392012-02-19 18:52:44 -05002019 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002020 wl_signal_add(&next->surface.resource.destroy_signal,
2021 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002022
2023 switcher->current = next;
2024 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002025
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002026 shsurf = get_shell_surface(switcher->current);
2027 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2028 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002029}
2030
2031static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002032switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002033{
2034 struct switcher *switcher =
2035 container_of(listener, struct switcher, listener);
2036
2037 switcher_next(switcher);
2038}
2039
2040static void
2041switcher_destroy(struct switcher *switcher, uint32_t time)
2042{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002043 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002044 struct weston_surface *surface;
2045 struct weston_input_device *device =
2046 (struct weston_input_device *) switcher->grab.input_device;
2047
2048 wl_list_for_each(surface, &compositor->surface_list, link) {
2049 surface->alpha = 255;
2050 weston_surface_damage(surface);
2051 }
2052
Alex Wu07b26062012-03-12 16:06:01 +08002053 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002054 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002055 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002056 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002057 free(switcher);
2058}
2059
2060static void
2061switcher_key(struct wl_keyboard_grab *grab,
2062 uint32_t time, uint32_t key, int32_t state)
2063{
2064 struct switcher *switcher = container_of(grab, struct switcher, grab);
2065 struct weston_input_device *device =
2066 (struct weston_input_device *) grab->input_device;
2067
2068 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2069 switcher_destroy(switcher, time);
2070 } else if (key == KEY_TAB && state) {
2071 switcher_next(switcher);
2072 }
2073};
2074
2075static const struct wl_keyboard_grab_interface switcher_grab = {
2076 switcher_key
2077};
2078
2079static void
2080switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002081 uint32_t key, uint32_t button, uint32_t axis,
2082 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002083{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002084 struct wl_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002085 struct switcher *switcher;
2086
2087 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002088 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002089 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002090 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002091 wl_list_init(&switcher->listener.link);
2092
2093 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002094 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2095 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002096 switcher_next(switcher);
2097}
2098
Pekka Paalanen3c647232011-12-22 13:43:43 +02002099static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002100backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002101 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002102{
2103 struct weston_compositor *compositor = data;
2104 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002105 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002106
2107 /* TODO: we're limiting to simple use cases, where we assume just
2108 * control on the primary display. We'd have to extend later if we
2109 * ever get support for setting backlights on random desktop LCD
2110 * panels though */
2111 output = get_default_output(compositor);
2112 if (!output)
2113 return;
2114
2115 if (!output->set_backlight)
2116 return;
2117
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002118 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2119 backlight_new = output->backlight_current - 25;
2120 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2121 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002122
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002123 if (backlight_new < 5)
2124 backlight_new = 5;
2125 if (backlight_new > 255)
2126 backlight_new = 255;
2127
2128 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002129 output->set_backlight(output, output->backlight_current);
2130}
2131
2132static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002133debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002134 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002135{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002136 struct wl_shell *shell = data;
2137 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002138 struct weston_surface *surface;
2139
2140 if (shell->debug_repaint_surface) {
2141 weston_surface_destroy(shell->debug_repaint_surface);
2142 shell->debug_repaint_surface = NULL;
2143 } else {
2144 surface = weston_surface_create(compositor);
2145 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2146 weston_surface_configure(surface, 0, 0, 8192, 8192);
2147 wl_list_insert(&compositor->fade_layer.surface_list,
2148 &surface->layer_link);
2149 weston_surface_assign_output(surface);
2150 pixman_region32_init(&surface->input);
2151
2152 /* Here's the dirty little trick that makes the
2153 * repaint debugging work: we force an
2154 * update_transform first to update dependent state
2155 * and clear the geometry.dirty bit. Then we clear
2156 * the surface damage so it only gets repainted
2157 * piecewise as we repaint other things. */
2158
2159 weston_surface_update_transform(surface);
2160 pixman_region32_fini(&surface->damage);
2161 pixman_region32_init(&surface->damage);
2162 shell->debug_repaint_surface = surface;
2163 }
2164}
2165
2166static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002167shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002168{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002169 struct wl_shell *shell =
2170 container_of(listener, struct wl_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002171
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002172 if (shell->child.client)
2173 wl_client_destroy(shell->child.client);
2174
Pekka Paalanen3c647232011-12-22 13:43:43 +02002175 free(shell->screensaver.path);
2176 free(shell);
2177}
2178
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002179int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002180shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002181
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002182WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002183shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002184{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002185 struct wl_shell *shell;
2186
2187 shell = malloc(sizeof *shell);
2188 if (shell == NULL)
2189 return -1;
2190
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002191 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002192 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002193
2194 shell->destroy_listener.notify = shell_destroy;
2195 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2196 shell->lock_listener.notify = lock;
2197 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2198 shell->unlock_listener.notify = unlock;
2199 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002200
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002201 wl_list_init(&shell->backgrounds);
2202 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002203 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002204
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002205 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2206 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2207 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2208 weston_layer_init(&shell->background_layer,
2209 &shell->toplevel_layer.link);
2210 wl_list_init(&shell->lock_layer.surface_list);
2211
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002212 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002213
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002214 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2215 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002216 return -1;
2217
Kristian Høgsberg75840622011-09-06 13:48:16 -04002218 if (wl_display_add_global(ec->wl_display,
2219 &desktop_shell_interface,
2220 shell, bind_desktop_shell) == NULL)
2221 return -1;
2222
Pekka Paalanen6e168112011-11-24 11:34:05 +02002223 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2224 shell, bind_screensaver) == NULL)
2225 return -1;
2226
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002227 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002228 if (launch_desktop_shell_process(shell) != 0)
2229 return -1;
2230
Scott Moreau6a3633d2012-03-20 08:47:59 -06002231 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2232 move_binding, shell);
2233 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2234 resize_binding, shell);
2235 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2236 MODIFIER_CTRL | MODIFIER_ALT,
2237 terminate_binding, ec);
2238 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002239 click_to_activate_binding, shell);
Scott Moreau1b45a792012-03-22 10:58:23 -06002240 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002241 MODIFIER_SUPER | MODIFIER_ALT,
2242 surface_opacity_binding, NULL);
2243 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002244 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002245 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002246 MODIFIER_SUPER | MODIFIER_ALT,
2247 rotate_binding, NULL);
2248 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002249 switcher_binding, shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002250
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002251 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002252 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2253 backlight_binding, ec);
2254 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2255 backlight_binding, ec);
2256 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2257 backlight_binding, ec);
2258 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2259 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002260
Scott Moreau6a3633d2012-03-20 08:47:59 -06002261 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002262 debug_repaint_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002263
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002264 return 0;
2265}