blob: 17ae1f49ff4b48f0a53beac0ffd9e13faea15ca2 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
43 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050045 struct weston_layer fullscreen_layer;
46 struct weston_layer panel_layer;
47 struct weston_layer toplevel_layer;
48 struct weston_layer background_layer;
49 struct weston_layer lock_layer;
50
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020051 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050052 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020053 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020054 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020055
56 unsigned deathcount;
57 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020058 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020059
60 bool locked;
61 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020062
Pekka Paalanen068ae942011-11-28 14:11:15 +020063 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050064 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010065
66 struct wl_list backgrounds;
67 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020068
Pekka Paalanen77346a62011-11-30 16:26:35 +020069 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020070 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020071 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct wl_resource *binding;
73 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050076
77 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040078};
79
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020081 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050082
Pekka Paalanen57da4a82011-11-23 16:42:16 +020083 SHELL_SURFACE_PANEL,
84 SHELL_SURFACE_BACKGROUND,
85 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020086 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050087
88 SHELL_SURFACE_TOPLEVEL,
89 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050090 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080091 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050092 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093};
94
Pekka Paalanen56cdea92011-11-23 16:14:12 +020095struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020096 struct wl_resource resource;
97
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050098 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020099 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200101
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500102 enum shell_surface_type type;
103 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800104 bool saved_position_valid;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100105
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500106 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200107 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500108 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200109 } rotation;
110
111 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700112 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500113 uint32_t time;
114 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200115 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 int32_t initial_up;
117 } popup;
118
Alex Wu4539b082012-03-01 12:57:46 +0800119 struct {
120 enum wl_shell_surface_fullscreen_method type;
121 struct weston_transform transform; /* matrix from x, y */
122 uint32_t framerate;
123 struct weston_surface *black_surface;
124 } fullscreen;
125
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500126 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300129
130 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200131};
132
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300133struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700134 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300135 struct shell_surface *shsurf;
136 struct wl_listener shsurf_destroy_listener;
137};
138
139struct weston_move_grab {
140 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500141 int32_t dx, dy;
142};
143
Pekka Paalanen460099f2012-01-20 16:48:25 +0200144struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300145 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500146 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200147 struct {
148 int32_t x;
149 int32_t y;
150 } center;
151};
152
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500153static void
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300154destroy_shell_grab_shsurf(struct wl_listener *listener,
155 struct wl_resource *resource, uint32_t time)
156{
157 struct shell_grab *grab;
158
159 grab = container_of(listener, struct shell_grab,
160 shsurf_destroy_listener);
161
162 grab->shsurf = NULL;
163}
164
165static void
166shell_grab_init(struct shell_grab *grab,
167 const struct wl_pointer_grab_interface *interface,
168 struct shell_surface *shsurf)
169{
170 grab->grab.interface = interface;
171 grab->shsurf = shsurf;
172 grab->shsurf_destroy_listener.func = destroy_shell_grab_shsurf;
173 wl_list_insert(shsurf->resource.destroy_listener_list.prev,
174 &grab->shsurf_destroy_listener.link);
175
176}
177
178static void
179shell_grab_finish(struct shell_grab *grab)
180{
181 wl_list_remove(&grab->shsurf_destroy_listener.link);
182}
183
184static void
Alex Wu4539b082012-03-01 12:57:46 +0800185center_on_output(struct weston_surface *surface,
186 struct weston_output *output);
187
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188static struct shell_surface *
189get_shell_surface(struct weston_surface *surface);
190
Alex Wu4539b082012-03-01 12:57:46 +0800191static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200192shell_configuration(struct wl_shell *shell)
193{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200194 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200195 char *path = NULL;
196 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200197
198 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200199 { "path", CONFIG_KEY_STRING, &path },
200 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200201 };
202
203 struct config_section cs[] = {
204 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
205 };
206
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200207 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500208 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200209 free(config_file);
210
Pekka Paalanen7296e792011-12-07 16:22:00 +0200211 shell->screensaver.path = path;
212 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200213}
214
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200215static void
Scott Moreau447013d2012-02-18 05:05:29 -0700216noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500217 struct wl_surface *surface, int32_t x, int32_t y)
218{
219 grab->focus = NULL;
220}
221
222static void
Scott Moreau447013d2012-02-18 05:05:29 -0700223move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500224 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500227 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228 struct shell_surface *shsurf = move->base.shsurf;
229 struct weston_surface *es;
230
231 if (!shsurf)
232 return;
233
234 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500235
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500236 weston_surface_configure(es,
237 device->x + move->dx,
238 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200239 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500240}
241
242static void
Scott Moreau447013d2012-02-18 05:05:29 -0700243move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400244 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300246 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
247 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500248 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500249
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500250 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300251 shell_grab_finish(shell_grab);
Scott Moreau447013d2012-02-18 05:05:29 -0700252 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500253 free(grab);
254 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500255}
256
Scott Moreau447013d2012-02-18 05:05:29 -0700257static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500258 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259 move_grab_motion,
260 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261};
262
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400263static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500264weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500265 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268 struct shell_surface *shsurf = get_shell_surface(es);
269
270 if (!shsurf)
271 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500272
273 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400274 if (!move)
275 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277 shell_grab_init(&move->base, &move_grab_interface, shsurf);
278
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200279 move->dx = es->geometry.x - wd->input_device.grab_x;
280 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500281
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300282 wl_input_device_start_pointer_grab(&wd->input_device,
283 &move->base.grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500284
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200285 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400286
287 return 0;
288}
289
290static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200291shell_surface_move(struct wl_client *client, struct wl_resource *resource,
292 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400293{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500294 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400296
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500297 if (wd->input_device.button_count == 0 ||
298 wd->input_device.grab_time != time ||
299 wd->input_device.pointer_focus != &shsurf->surface->surface)
300 return;
301
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500302 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400303 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500304}
305
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500306struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300307 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200309 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310};
311
312static void
Scott Moreau447013d2012-02-18 05:05:29 -0700313resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500314 uint32_t time, int32_t x, int32_t y)
315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200319 int32_t from_x, from_y;
320 int32_t to_x, to_y;
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322 if (!resize->base.shsurf)
323 return;
324
325 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200326 device->grab_x, device->grab_y,
327 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200329 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500330
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200331 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200332 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200333 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200334 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335 } else {
336 width = resize->width;
337 }
338
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200339 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200340 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200341 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200342 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500343 } else {
344 height = resize->height;
345 }
346
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300347 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500348 time, resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500349}
350
351static void
Scott Moreau447013d2012-02-18 05:05:29 -0700352resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400353 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500354{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300355 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500356 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500357
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500358 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300359 shell_grab_finish(&resize->base);
Scott Moreau447013d2012-02-18 05:05:29 -0700360 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500361 free(grab);
362 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500363}
364
Scott Moreau447013d2012-02-18 05:05:29 -0700365static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500366 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500367 resize_grab_motion,
368 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500369};
370
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400371static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500372weston_surface_resize(struct shell_surface *shsurf,
373 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200374 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500375{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500376 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500377
Alex Wu4539b082012-03-01 12:57:46 +0800378 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
379 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500380
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200381 if (edges == 0 || edges > 15 ||
382 (edges & 3) == 3 || (edges & 12) == 12)
383 return 0;
384
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500385 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400386 if (!resize)
387 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500388
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300389 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
390
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500391 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200392 resize->width = shsurf->surface->geometry.width;
393 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400394
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300395 wl_input_device_start_pointer_grab(&wd->input_device,
396 &resize->base.grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500397
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200398 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400399
400 return 0;
401}
402
403static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200404shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
405 struct wl_resource *input_resource, uint32_t time,
406 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400407{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500408 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200409 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400410
Alex Wu4539b082012-03-01 12:57:46 +0800411 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
412 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400413
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500414 if (wd->input_device.button_count == 0 ||
415 wd->input_device.grab_time != time ||
416 wd->input_device.pointer_focus != &shsurf->surface->surface)
417 return;
418
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500419 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400420 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500421}
422
Juan Zhao96879df2012-02-07 08:45:41 +0800423static struct weston_output *
424get_default_output(struct weston_compositor *compositor)
425{
426 return container_of(compositor->output_list.next,
427 struct weston_output, link);
428}
429
Alex Wu4539b082012-03-01 12:57:46 +0800430static void
431shell_unset_fullscreen(struct shell_surface *shsurf)
432{
433 /* undo all fullscreen things here */
434 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
435 shsurf->fullscreen.framerate = 0;
436 wl_list_remove(&shsurf->fullscreen.transform.link);
437 wl_list_init(&shsurf->fullscreen.transform.link);
438 weston_surface_destroy(shsurf->fullscreen.black_surface);
439 shsurf->fullscreen.black_surface = NULL;
440 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300441 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800442 weston_surface_set_position(shsurf->surface,
443 shsurf->saved_x, shsurf->saved_y);
444}
445
Pekka Paalanen98262232011-12-01 10:42:22 +0200446static int
447reset_shell_surface_type(struct shell_surface *surface)
448{
449 switch (surface->type) {
450 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800451 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200452 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800453 case SHELL_SURFACE_MAXIMIZED:
454 surface->output = get_default_output(surface->surface->compositor);
455 weston_surface_set_position(surface->surface,
456 surface->saved_x,
457 surface->saved_y);
458 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200459 case SHELL_SURFACE_PANEL:
460 case SHELL_SURFACE_BACKGROUND:
461 wl_list_remove(&surface->link);
462 wl_list_init(&surface->link);
463 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200464 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200465 case SHELL_SURFACE_LOCK:
466 wl_resource_post_error(&surface->resource,
467 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200468 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200469 return -1;
470 case SHELL_SURFACE_NONE:
471 case SHELL_SURFACE_TOPLEVEL:
472 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500473 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200474 break;
475 }
476
477 surface->type = SHELL_SURFACE_NONE;
478 return 0;
479}
480
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500481static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200482shell_surface_set_toplevel(struct wl_client *client,
483 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400484
485{
Pekka Paalanen98262232011-12-01 10:42:22 +0200486 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400487
Pekka Paalanen98262232011-12-01 10:42:22 +0200488 if (reset_shell_surface_type(surface))
489 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400490
Pekka Paalanen98262232011-12-01 10:42:22 +0200491 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400492}
493
494static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200495shell_surface_set_transient(struct wl_client *client,
496 struct wl_resource *resource,
497 struct wl_resource *parent_resource,
498 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400499{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200500 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200502 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500503 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400504
Pekka Paalanen98262232011-12-01 10:42:22 +0200505 if (reset_shell_surface_type(shsurf))
506 return;
507
Alex Wu4539b082012-03-01 12:57:46 +0800508 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800509 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200510 weston_surface_set_position(es, pes->geometry.x + x,
511 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400512
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200513 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400514}
515
Juan Zhao96879df2012-02-07 08:45:41 +0800516static struct wl_shell *
517shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200518{
Juan Zhao96879df2012-02-07 08:45:41 +0800519 struct weston_surface *es = shsurf->surface;
520 struct weston_shell *shell = es->compositor->shell;
521
522 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
523}
524
525static int
526get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
527{
528 struct shell_surface *priv;
529 int panel_height = 0;
530
531 if (!output)
532 return 0;
533
534 wl_list_for_each(priv, &wlshell->panels, link) {
535 if (priv->output == output) {
536 panel_height = priv->surface->geometry.height;
537 break;
538 }
539 }
540 return panel_height;
541}
542
543static void
544shell_surface_set_maximized(struct wl_client *client,
545 struct wl_resource *resource,
546 struct wl_resource *output_resource )
547{
548 struct shell_surface *shsurf = resource->data;
549 struct weston_surface *es = shsurf->surface;
550 struct wl_shell *wlshell = NULL;
551 uint32_t edges = 0, panel_height = 0;
552
553 /* get the default output, if the client set it as NULL
554 check whether the ouput is available */
555 if (output_resource)
556 shsurf->output = output_resource->data;
557 else
558 shsurf->output = get_default_output(es->compositor);
559
560 if (reset_shell_surface_type(shsurf))
561 return;
562
563 shsurf->saved_x = es->geometry.x;
564 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800565 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800566
567 wlshell = shell_surface_get_shell(shsurf);
568 panel_height = get_output_panel_height(wlshell, es->output);
569 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500570
571 wl_shell_surface_send_configure(&shsurf->resource,
572 weston_compositor_get_time(), edges,
573 es->output->current->width,
574 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800575
576 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200577}
578
Alex Wu21858432012-04-01 20:13:08 +0800579static void
580black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
581
Alex Wu4539b082012-03-01 12:57:46 +0800582static struct weston_surface *
583create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800584 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800585 GLfloat x, GLfloat y, int w, int h)
586{
587 struct weston_surface *surface = NULL;
588
589 surface = weston_surface_create(ec);
590 if (surface == NULL) {
591 fprintf(stderr, "no memory\n");
592 return NULL;
593 }
594
Alex Wu21858432012-04-01 20:13:08 +0800595 surface->configure = black_surface_configure;
596 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800597 weston_surface_configure(surface, x, y, w, h);
598 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
599 return surface;
600}
601
602/* Create black surface and append it to the associated fullscreen surface.
603 * Handle size dismatch and positioning according to the method. */
604static void
605shell_configure_fullscreen(struct shell_surface *shsurf)
606{
607 struct weston_output *output = shsurf->fullscreen_output;
608 struct weston_surface *surface = shsurf->surface;
609 struct weston_matrix *matrix;
610 float scale;
611
612 center_on_output(surface, output);
613
614 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500615 shsurf->fullscreen.black_surface =
616 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800617 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500618 output->x, output->y,
619 output->current->width,
620 output->current->height);
621
622 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
623 wl_list_insert(&surface->layer_link,
624 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800625 shsurf->fullscreen.black_surface->output = output;
626
627 switch (shsurf->fullscreen.type) {
628 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
629 break;
630 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
631 matrix = &shsurf->fullscreen.transform.matrix;
632 weston_matrix_init(matrix);
633 scale = (float)output->current->width/(float)surface->geometry.width;
634 weston_matrix_scale(matrix, scale, scale, 1);
635 wl_list_remove(&shsurf->fullscreen.transform.link);
636 wl_list_insert(surface->geometry.transformation_list.prev,
637 &shsurf->fullscreen.transform.link);
638 weston_surface_set_position(surface, output->x, output->y);
639 break;
640 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
641 break;
642 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
643 break;
644 default:
645 break;
646 }
647}
648
649/* make the fullscreen and black surface at the top */
650static void
651shell_stack_fullscreen(struct shell_surface *shsurf)
652{
653 struct weston_surface *surface = shsurf->surface;
654 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800655
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500656 wl_list_remove(&surface->layer_link);
657 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800658
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500659 wl_list_insert(&shell->fullscreen_layer.surface_list,
660 &surface->layer_link);
661 wl_list_insert(&surface->layer_link,
662 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800663
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500664 weston_surface_damage(surface);
665 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800666}
667
668static void
669shell_map_fullscreen(struct shell_surface *shsurf)
670{
671 shell_configure_fullscreen(shsurf);
672 shell_stack_fullscreen(shsurf);
673}
674
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400675static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200676shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500677 struct wl_resource *resource,
678 uint32_t method,
679 uint32_t framerate,
680 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400681{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200682 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500683 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800684
685 if (output_resource)
686 shsurf->output = output_resource->data;
687 else
688 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400689
Pekka Paalanen98262232011-12-01 10:42:22 +0200690 if (reset_shell_surface_type(shsurf))
691 return;
692
Alex Wu4539b082012-03-01 12:57:46 +0800693 shsurf->fullscreen_output = shsurf->output;
694 shsurf->fullscreen.type = method;
695 shsurf->fullscreen.framerate = framerate;
696 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400697
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200698 shsurf->saved_x = es->geometry.x;
699 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800700 shsurf->saved_position_valid = true;
701
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300702 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300703 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500704
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500705 wl_shell_surface_send_configure(&shsurf->resource,
706 weston_compositor_get_time(), 0,
707 shsurf->output->current->width,
708 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400709}
710
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500711static void
Scott Moreau447013d2012-02-18 05:05:29 -0700712popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500713 struct wl_surface *surface, int32_t x, int32_t y)
714{
715 struct wl_input_device *device = grab->input_device;
716 struct shell_surface *priv =
717 container_of(grab, struct shell_surface, popup.grab);
718 struct wl_client *client = priv->surface->surface.resource.client;
719
Pekka Paalanencb108432012-01-19 16:25:40 +0200720 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200721 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500722 grab->focus = surface;
723 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200724 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500725 grab->focus = NULL;
726 }
727}
728
729static void
Scott Moreau447013d2012-02-18 05:05:29 -0700730popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200731 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500732{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500733 struct wl_resource *resource;
734
735 resource = grab->input_device->pointer_focus_resource;
736 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500737 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500738}
739
740static void
Scott Moreau447013d2012-02-18 05:05:29 -0700741popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400742 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500743{
744 struct wl_resource *resource;
745 struct shell_surface *shsurf =
746 container_of(grab, struct shell_surface, popup.grab);
747
748 resource = grab->input_device->pointer_focus_resource;
749 if (resource) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500750 wl_input_device_send_button(resource, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500751 } else if (state == 0 &&
752 (shsurf->popup.initial_up ||
753 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500754 wl_shell_surface_send_popup_done(&shsurf->resource);
Scott Moreau447013d2012-02-18 05:05:29 -0700755 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500756 shsurf->popup.grab.input_device = NULL;
757 }
758
759 if (state == 0)
760 shsurf->popup.initial_up = 1;
761}
762
Scott Moreau447013d2012-02-18 05:05:29 -0700763static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500764 popup_grab_focus,
765 popup_grab_motion,
766 popup_grab_button,
767};
768
769static void
770shell_map_popup(struct shell_surface *shsurf, uint32_t time)
771{
772 struct wl_input_device *device;
773 struct weston_surface *es = shsurf->surface;
774 struct weston_surface *parent = shsurf->parent->surface;
775
776 es->output = parent->output;
777
778 shsurf->popup.grab.interface = &popup_grab_interface;
779 device = es->compositor->input_device;
780
Pekka Paalanen938269a2012-02-07 14:19:01 +0200781 weston_surface_update_transform(parent);
782 if (parent->transform.enabled) {
783 shsurf->popup.parent_transform.matrix =
784 parent->transform.matrix;
785 } else {
786 /* construct x, y translation matrix */
787 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
788 shsurf->popup.parent_transform.matrix.d[12] =
789 parent->geometry.x;
790 shsurf->popup.parent_transform.matrix.d[13] =
791 parent->geometry.y;
792 }
793 wl_list_insert(es->geometry.transformation_list.prev,
794 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200795 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500796
797 shsurf->popup.grab.input_device = device;
798 shsurf->popup.time = device->grab_time;
799 shsurf->popup.initial_up = 0;
800
Scott Moreau447013d2012-02-18 05:05:29 -0700801 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500802 &shsurf->popup.grab, shsurf->popup.time);
803}
804
805static void
806shell_surface_set_popup(struct wl_client *client,
807 struct wl_resource *resource,
808 struct wl_resource *input_device_resource,
809 uint32_t time,
810 struct wl_resource *parent_resource,
811 int32_t x, int32_t y, uint32_t flags)
812{
813 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500814
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500815 shsurf->type = SHELL_SURFACE_POPUP;
816 shsurf->parent = parent_resource->data;
817 shsurf->popup.x = x;
818 shsurf->popup.y = y;
819}
820
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200821static const struct wl_shell_surface_interface shell_surface_implementation = {
822 shell_surface_move,
823 shell_surface_resize,
824 shell_surface_set_toplevel,
825 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500826 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800827 shell_surface_set_popup,
828 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200829};
830
831static void
832destroy_shell_surface(struct wl_resource *resource)
833{
834 struct shell_surface *shsurf = resource->data;
835
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500836 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700837 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500838
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200839 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300840 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200841 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300842 shsurf->surface->configure = NULL;
843 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200844
Alex Wuaa08e2d2012-03-05 11:01:40 +0800845 if (shsurf->fullscreen.black_surface)
846 weston_surface_destroy(shsurf->fullscreen.black_surface);
847
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200848 wl_list_remove(&shsurf->link);
849 free(shsurf);
850}
851
852static void
853shell_handle_surface_destroy(struct wl_listener *listener,
854 struct wl_resource *resource, uint32_t time)
855{
856 struct shell_surface *shsurf = container_of(listener,
857 struct shell_surface,
858 surface_destroy_listener);
859
860 shsurf->surface = NULL;
861 wl_resource_destroy(&shsurf->resource, time);
862}
863
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200864static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500865get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200866{
867 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
868 struct wl_listener *listener;
869
870 /* search the destroy listener list for our callback */
871 wl_list_for_each(listener, lst, link) {
872 if (listener->func == shell_handle_surface_destroy) {
873 return container_of(listener, struct shell_surface,
874 surface_destroy_listener);
875 }
876 }
877
878 return NULL;
879}
880
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200881static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300882shell_surface_configure(struct weston_surface *, int32_t, int32_t);
883
884static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200885shell_get_shell_surface(struct wl_client *client,
886 struct wl_resource *resource,
887 uint32_t id,
888 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200889{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500890 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200891 struct shell_surface *shsurf;
892
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200893 if (get_shell_surface(surface)) {
894 wl_resource_post_error(surface_resource,
895 WL_DISPLAY_ERROR_INVALID_OBJECT,
896 "wl_shell::get_shell_surface already requested");
897 return;
898 }
899
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300900 if (surface->configure) {
901 wl_resource_post_error(surface_resource,
902 WL_DISPLAY_ERROR_INVALID_OBJECT,
903 "surface->configure already set");
904 return;
905 }
906
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200907 shsurf = calloc(1, sizeof *shsurf);
908 if (!shsurf) {
909 wl_resource_post_no_memory(resource);
910 return;
911 }
912
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300913 surface->configure = shell_surface_configure;
914
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200915 shsurf->resource.destroy = destroy_shell_surface;
916 shsurf->resource.object.id = id;
917 shsurf->resource.object.interface = &wl_shell_surface_interface;
918 shsurf->resource.object.implementation =
919 (void (**)(void)) &shell_surface_implementation;
920 shsurf->resource.data = shsurf;
921
Alex Wu4539b082012-03-01 12:57:46 +0800922 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200923 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800924 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
925 shsurf->fullscreen.framerate = 0;
926 shsurf->fullscreen.black_surface = NULL;
927 wl_list_init(&shsurf->fullscreen.transform.link);
928
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200929 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
930 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
931 &shsurf->surface_destroy_listener.link);
932
933 /* init link so its safe to always remove it in destroy_shell_surface */
934 wl_list_init(&shsurf->link);
935
Pekka Paalanen460099f2012-01-20 16:48:25 +0200936 /* empty when not in use */
937 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500938 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200939
Pekka Paalanen98262232011-12-01 10:42:22 +0200940 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200941
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200942 wl_client_add_resource(client, &shsurf->resource);
943}
944
945static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200946 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500947};
948
Kristian Høgsberg07937562011-04-12 17:25:42 -0400949static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200951{
952 proc->pid = 0;
953}
954
955static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200956launch_screensaver(struct wl_shell *shell)
957{
958 if (shell->screensaver.binding)
959 return;
960
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200961 if (!shell->screensaver.path)
962 return;
963
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500964 if (shell->screensaver.process.pid != 0) {
965 fprintf(stderr, "old screensaver still running\n");
966 return;
967 }
968
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200970 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200971 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200972 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200973}
974
975static void
976terminate_screensaver(struct wl_shell *shell)
977{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200978 if (shell->screensaver.process.pid == 0)
979 return;
980
981 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200982}
983
984static void
985show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
986{
987 struct wl_list *list;
988
989 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500990 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200991 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500992 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200993
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500994 wl_list_remove(&surface->surface->layer_link);
995 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200996 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200997 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200998}
999
1000static void
1001hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
1002{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001003 wl_list_remove(&surface->surface->layer_link);
1004 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001005 surface->surface->output = NULL;
1006}
1007
1008static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001009desktop_shell_set_background(struct wl_client *client,
1010 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001011 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001012 struct wl_resource *surface_resource)
1013{
1014 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001015 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001016 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001017 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001018
Pekka Paalanen98262232011-12-01 10:42:22 +02001019 if (reset_shell_surface_type(shsurf))
1020 return;
1021
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001022 wl_list_for_each(priv, &shell->backgrounds, link) {
1023 if (priv->output == output_resource->data) {
1024 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001025 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001026 wl_list_remove(&priv->link);
1027 break;
1028 }
1029 }
1030
Pekka Paalanen068ae942011-11-28 14:11:15 +02001031 shsurf->type = SHELL_SURFACE_BACKGROUND;
1032 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001033
Pekka Paalanen068ae942011-11-28 14:11:15 +02001034 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001035
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001036 weston_surface_set_position(surface, shsurf->output->x,
1037 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001038
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001039 desktop_shell_send_configure(resource,
1040 weston_compositor_get_time(), 0,
1041 surface_resource,
1042 shsurf->output->current->width,
1043 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001044}
1045
1046static void
1047desktop_shell_set_panel(struct wl_client *client,
1048 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001049 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001050 struct wl_resource *surface_resource)
1051{
1052 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001053 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001055 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001056
Pekka Paalanen98262232011-12-01 10:42:22 +02001057 if (reset_shell_surface_type(shsurf))
1058 return;
1059
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001060 wl_list_for_each(priv, &shell->panels, link) {
1061 if (priv->output == output_resource->data) {
1062 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001063 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001064 wl_list_remove(&priv->link);
1065 break;
1066 }
1067 }
1068
Pekka Paalanen068ae942011-11-28 14:11:15 +02001069 shsurf->type = SHELL_SURFACE_PANEL;
1070 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001071
Pekka Paalanen068ae942011-11-28 14:11:15 +02001072 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001073
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001074 weston_surface_set_position(surface, shsurf->output->x,
1075 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001076
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001077 desktop_shell_send_configure(resource,
1078 weston_compositor_get_time(), 0,
1079 surface_resource,
1080 shsurf->output->current->width,
1081 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001082}
1083
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001084static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001085handle_lock_surface_destroy(struct wl_listener *listener,
1086 struct wl_resource *resource, uint32_t time)
1087{
1088 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001089 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001090
1091 fprintf(stderr, "lock surface gone\n");
1092 shell->lock_surface = NULL;
1093}
1094
1095static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001096desktop_shell_set_lock_surface(struct wl_client *client,
1097 struct wl_resource *resource,
1098 struct wl_resource *surface_resource)
1099{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001100 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001101 struct shell_surface *surface = surface_resource->data;
1102
1103 if (reset_shell_surface_type(surface))
1104 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001105
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001106 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001107
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001108 if (!shell->locked)
1109 return;
1110
Pekka Paalanen98262232011-12-01 10:42:22 +02001111 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001112
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001113 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1114 wl_list_insert(&surface_resource->destroy_listener_list,
1115 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001116
Pekka Paalanen068ae942011-11-28 14:11:15 +02001117 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001118}
1119
1120static void
1121resume_desktop(struct wl_shell *shell)
1122{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001123 struct shell_surface *tmp;
1124
1125 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1126 hide_screensaver(shell, tmp);
1127
1128 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001129
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001130 wl_list_remove(&shell->lock_layer.link);
1131 wl_list_insert(&shell->compositor->cursor_layer.link,
1132 &shell->fullscreen_layer.link);
1133 wl_list_insert(&shell->fullscreen_layer.link,
1134 &shell->panel_layer.link);
1135 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001136
1137 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001138 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001139 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001140 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001141}
1142
1143static void
1144desktop_shell_unlock(struct wl_client *client,
1145 struct wl_resource *resource)
1146{
1147 struct wl_shell *shell = resource->data;
1148
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001149 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001150
1151 if (shell->locked)
1152 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001153}
1154
Kristian Høgsberg75840622011-09-06 13:48:16 -04001155static const struct desktop_shell_interface desktop_shell_implementation = {
1156 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001157 desktop_shell_set_panel,
1158 desktop_shell_set_lock_surface,
1159 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001160};
1161
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001162static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001163get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001164{
1165 struct shell_surface *shsurf;
1166
1167 shsurf = get_shell_surface(surface);
1168 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001169 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001170 return shsurf->type;
1171}
1172
Kristian Høgsberg75840622011-09-06 13:48:16 -04001173static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001174move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001175 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001176{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001177 struct weston_surface *surface =
1178 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001179
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001180 if (surface == NULL)
1181 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001182
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001183 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001184 case SHELL_SURFACE_PANEL:
1185 case SHELL_SURFACE_BACKGROUND:
1186 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001187 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001188 return;
1189 default:
1190 break;
1191 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001192
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001193 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001194}
1195
1196static void
1197resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001198 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001199{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001200 struct weston_surface *surface =
1201 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001202 uint32_t edges = 0;
1203 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001204 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001205
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001206 if (surface == NULL)
1207 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001208
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001209 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001210 if (!shsurf)
1211 return;
1212
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001213 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001214 case SHELL_SURFACE_PANEL:
1215 case SHELL_SURFACE_BACKGROUND:
1216 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001217 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001218 return;
1219 default:
1220 break;
1221 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001222
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001223 weston_surface_from_global(surface,
1224 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001225
Pekka Paalanen60921e52012-01-25 15:55:43 +02001226 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001227 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001228 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001229 edges |= 0;
1230 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001231 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001232
Pekka Paalanen60921e52012-01-25 15:55:43 +02001233 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001234 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001235 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001236 edges |= 0;
1237 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001238 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001239
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001240 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001241 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001242}
1243
1244static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001245surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1246 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1247{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001248 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001249 struct shell_surface *shsurf;
1250 struct weston_surface *surface =
1251 (struct weston_surface *) device->pointer_focus;
1252
1253 if (surface == NULL)
1254 return;
1255
1256 shsurf = get_shell_surface(surface);
1257 if (!shsurf)
1258 return;
1259
1260 switch (shsurf->type) {
1261 case SHELL_SURFACE_BACKGROUND:
1262 case SHELL_SURFACE_SCREENSAVER:
1263 return;
1264 default:
1265 break;
1266 }
1267
1268 surface->alpha += value * step;
1269
1270 if (surface->alpha > 255)
1271 surface->alpha = 255;
1272 if (surface->alpha < step)
1273 surface->alpha = step;
1274
1275 surface->geometry.dirty = 1;
1276 weston_surface_damage(surface);
1277}
1278
1279static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001280zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001281 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001282{
1283 struct weston_input_device *wd = (struct weston_input_device *) device;
1284 struct weston_compositor *compositor = wd->compositor;
1285 struct weston_output *output;
1286
1287 wl_list_for_each(output, &compositor->output_list, link) {
1288 if (pixman_region32_contains_point(&output->region,
1289 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001290 output->zoom.active = 1;
1291 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001292
1293 if (output->zoom.level >= 1.0) {
1294 output->zoom.active = 0;
1295 output->zoom.level = 1.0;
1296 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001297
1298 if (output->zoom.level < output->zoom.increment)
1299 output->zoom.level = output->zoom.increment;
1300
1301 weston_output_update_zoom(output, device->x, device->y);
1302 }
1303 }
1304}
1305
Scott Moreauccbf29d2012-02-22 14:21:41 -07001306static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001307terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001308 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001309{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001310 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001311
1312 if (state)
1313 wl_display_terminate(compositor->wl_display);
1314}
1315
1316static void
Scott Moreau447013d2012-02-18 05:05:29 -07001317rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001318 uint32_t time, int32_t x, int32_t y)
1319{
1320 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001321 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001322 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001323 struct shell_surface *shsurf = rotate->base.shsurf;
1324 struct weston_surface *surface;
1325 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1326
1327 if (!shsurf)
1328 return;
1329
1330 surface = shsurf->surface;
1331
1332 cx = 0.5f * surface->geometry.width;
1333 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001334
1335 dx = device->x - rotate->center.x;
1336 dy = device->y - rotate->center.y;
1337 r = sqrtf(dx * dx + dy * dy);
1338
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001339 wl_list_remove(&shsurf->rotation.transform.link);
1340 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001341
1342 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001343 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001344 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001345
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001346 weston_matrix_init(&rotate->rotation);
1347 rotate->rotation.d[0] = dx / r;
1348 rotate->rotation.d[4] = -dy / r;
1349 rotate->rotation.d[1] = -rotate->rotation.d[4];
1350 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001351
1352 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001353 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001354 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001355 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001356 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001357
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001358 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001359 &shsurf->surface->geometry.transformation_list,
1360 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001361 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001362 wl_list_init(&shsurf->rotation.transform.link);
1363 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001364 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001365 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001366
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001367 /* We need to adjust the position of the surface
1368 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001369 cposx = surface->geometry.x + cx;
1370 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001371 dposx = rotate->center.x - cposx;
1372 dposy = rotate->center.y - cposy;
1373 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001374 weston_surface_set_position(surface,
1375 surface->geometry.x + dposx,
1376 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001377 }
1378
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001379 /* Repaint implies weston_surface_update_transform(), which
1380 * lazily applies the damage due to rotation update.
1381 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001382 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001383}
1384
1385static void
Scott Moreau447013d2012-02-18 05:05:29 -07001386rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001387 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001388{
1389 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001390 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001391 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001392 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001393
1394 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001395 if (shsurf)
1396 weston_matrix_multiply(&shsurf->rotation.rotation,
1397 &rotate->rotation);
1398 shell_grab_finish(&rotate->base);
Scott Moreau447013d2012-02-18 05:05:29 -07001399 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001400 free(rotate);
1401 }
1402}
1403
Scott Moreau447013d2012-02-18 05:05:29 -07001404static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001405 noop_grab_focus,
1406 rotate_grab_motion,
1407 rotate_grab_button,
1408};
1409
1410static void
1411rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001412 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001413{
1414 struct weston_surface *base_surface =
1415 (struct weston_surface *) device->pointer_focus;
1416 struct shell_surface *surface;
1417 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001418 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001419 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001420
1421 if (base_surface == NULL)
1422 return;
1423
1424 surface = get_shell_surface(base_surface);
1425 if (!surface)
1426 return;
1427
1428 switch (surface->type) {
1429 case SHELL_SURFACE_PANEL:
1430 case SHELL_SURFACE_BACKGROUND:
1431 case SHELL_SURFACE_FULLSCREEN:
1432 case SHELL_SURFACE_SCREENSAVER:
1433 return;
1434 default:
1435 break;
1436 }
1437
Pekka Paalanen460099f2012-01-20 16:48:25 +02001438 rotate = malloc(sizeof *rotate);
1439 if (!rotate)
1440 return;
1441
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001442 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001443
1444 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001445 surface->surface->geometry.width / 2,
1446 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001447 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001448
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001449 wl_input_device_start_pointer_grab(device, &rotate->base.grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001450
1451 dx = device->x - rotate->center.x;
1452 dy = device->y - rotate->center.y;
1453 r = sqrtf(dx * dx + dy * dy);
1454 if (r > 20.0f) {
1455 struct weston_matrix inverse;
1456
1457 weston_matrix_init(&inverse);
1458 inverse.d[0] = dx / r;
1459 inverse.d[4] = dy / r;
1460 inverse.d[1] = -inverse.d[4];
1461 inverse.d[5] = inverse.d[0];
1462 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001463
1464 weston_matrix_init(&rotate->rotation);
1465 rotate->rotation.d[0] = dx / r;
1466 rotate->rotation.d[4] = -dy / r;
1467 rotate->rotation.d[1] = -rotate->rotation.d[4];
1468 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001469 } else {
1470 weston_matrix_init(&surface->rotation.rotation);
1471 weston_matrix_init(&rotate->rotation);
1472 }
1473
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001474 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001475}
1476
1477static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001478activate(struct weston_shell *base, struct weston_surface *es,
1479 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001480{
1481 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482 struct weston_compositor *compositor = shell->compositor;
Alex Wu21858432012-04-01 20:13:08 +08001483 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001484
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001486
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001487 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001488 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001489
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001490 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001491 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001492 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001493 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001494 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001495
Pekka Paalanen77346a62011-11-30 16:26:35 +02001496 case SHELL_SURFACE_SCREENSAVER:
1497 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001498 if (shell->lock_surface)
1499 weston_surface_restack(es,
1500 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001501 break;
Alex Wu4539b082012-03-01 12:57:46 +08001502 case SHELL_SURFACE_FULLSCREEN:
1503 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001504 shell_stack_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001505 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001506 default:
Alex Wu21858432012-04-01 20:13:08 +08001507 /* move the fullscreen surfaces down into the toplevel layer */
1508 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1509 wl_list_for_each_reverse_safe(surf,
1510 prev,
1511 &shell->fullscreen_layer.surface_list,
1512 layer_link)
1513 weston_surface_restack(surf,
1514 &shell->toplevel_layer.surface_list);
1515 }
1516
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001517 weston_surface_restack(es,
1518 &shell->toplevel_layer.surface_list);
1519 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001520 }
1521}
1522
Alex Wu21858432012-04-01 20:13:08 +08001523/* no-op func for checking black surface */
1524static void
1525black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1526{
1527}
1528
1529static bool
1530is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1531{
1532 if (es->configure == black_surface_configure) {
1533 if (fs_surface)
1534 *fs_surface = (struct weston_surface *)es->private;
1535 return true;
1536 }
1537 return false;
1538}
1539
Kristian Høgsberg75840622011-09-06 13:48:16 -04001540static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001541click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001542 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001543 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545 struct weston_input_device *wd = (struct weston_input_device *) device;
1546 struct weston_compositor *compositor = data;
1547 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001548 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001549
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001550 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001551 if (!focus)
1552 return;
1553
Alex Wu21858432012-04-01 20:13:08 +08001554 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001555 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001556
Alex Wu9c35e6b2012-03-05 14:13:13 +08001557 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001558 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001559}
1560
1561static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001562lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001563{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001564 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001565 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001566 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001567 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001568 uint32_t time;
1569
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001570 if (shell->locked) {
1571 wl_list_for_each(output, &shell->compositor->output_list, link)
1572 /* TODO: find a way to jump to other DPMS levels */
1573 if (output->set_dpms)
1574 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001575 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001576 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001577
1578 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001579
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001580 /* Hide all surfaces by removing the fullscreen, panel and
1581 * toplevel layers. This way nothing else can show or receive
1582 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001583
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001584 wl_list_remove(&shell->panel_layer.link);
1585 wl_list_remove(&shell->toplevel_layer.link);
1586 wl_list_remove(&shell->fullscreen_layer.link);
1587 wl_list_insert(&shell->compositor->cursor_layer.link,
1588 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001589
Pekka Paalanen77346a62011-11-30 16:26:35 +02001590 launch_screensaver(shell);
1591
1592 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1593 show_screensaver(shell, shsurf);
1594
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001595 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001596 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 weston_compositor_wake(shell->compositor);
1598 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001599 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001600
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001601 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001602 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001603
1604 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001605 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001606 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1607 wl_input_device_set_keyboard_focus(&device->input_device,
1608 NULL, time);
1609 }
1610
1611 /* TODO: disable bindings that should not work while locked. */
1612
1613 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001614}
1615
1616static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001617unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001618{
1619 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1620
Pekka Paalanend81c2162011-11-16 13:47:34 +02001621 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001622 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001623 return;
1624 }
1625
1626 /* If desktop-shell client has gone away, unlock immediately. */
1627 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001628 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001629 return;
1630 }
1631
1632 if (shell->prepare_event_sent)
1633 return;
1634
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001635 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001636 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001637}
1638
1639static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001640center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001641{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001642 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001643 GLfloat x = (mode->width - surface->geometry.width) / 2;
1644 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001645
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001646 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001647}
1648
1649static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001650map(struct weston_shell *base, struct weston_surface *surface,
1651 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001652{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001653 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001654 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001655 struct shell_surface *shsurf;
1656 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001657 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001658 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001659
Pekka Paalanen77346a62011-11-30 16:26:35 +02001660 shsurf = get_shell_surface(surface);
1661 if (shsurf)
1662 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001663
Pekka Paalanen60921e52012-01-25 15:55:43 +02001664 surface->geometry.width = width;
1665 surface->geometry.height = height;
1666 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001667
1668 /* initial positioning, see also configure() */
1669 switch (surface_type) {
1670 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001671 weston_surface_set_position(surface, 10 + random() % 400,
1672 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001673 break;
1674 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001675 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001676 break;
Alex Wu4539b082012-03-01 12:57:46 +08001677 case SHELL_SURFACE_FULLSCREEN:
1678 shell_map_fullscreen(shsurf);
1679 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001680 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001681 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001682 panel_height = get_output_panel_height(shell,surface->output);
1683 weston_surface_set_position(surface, surface->output->x,
1684 surface->output->y + panel_height);
1685 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001686 case SHELL_SURFACE_LOCK:
1687 center_on_output(surface, get_default_output(compositor));
1688 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001689 case SHELL_SURFACE_POPUP:
1690 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001691 case SHELL_SURFACE_NONE:
1692 weston_surface_set_position(surface,
1693 surface->geometry.x + sx,
1694 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001695 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001696 default:
1697 ;
1698 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001699
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001700 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001701 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001702 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001703 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 wl_list_insert(&shell->background_layer.surface_list,
1705 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001706 break;
1707 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001708 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001709 wl_list_insert(&shell->panel_layer.surface_list,
1710 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001711 break;
1712 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001713 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001714 wl_list_insert(&shell->lock_layer.surface_list,
1715 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001717 break;
1718 case SHELL_SURFACE_SCREENSAVER:
1719 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001720 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001721 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001722 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001723 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001724 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001725 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001726 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001727 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001728 case SHELL_SURFACE_POPUP:
1729 case SHELL_SURFACE_TRANSIENT:
1730 parent = shsurf->parent->surface;
1731 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1732 break;
Alex Wu4539b082012-03-01 12:57:46 +08001733 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001734 case SHELL_SURFACE_NONE:
1735 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001736 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001737 wl_list_insert(&shell->toplevel_layer.surface_list,
1738 &surface->layer_link);
1739 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001740 }
1741
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001742 if (surface_type != SHELL_SURFACE_NONE) {
1743 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001744 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1745 surface->output = shsurf->output;
1746 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001747
Juan Zhao7bb92f02011-12-15 11:31:51 -05001748 switch (surface_type) {
1749 case SHELL_SURFACE_TOPLEVEL:
1750 case SHELL_SURFACE_TRANSIENT:
1751 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001752 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001753 if (!shell->locked)
1754 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001755 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001756 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001757 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001758 break;
1759 default:
1760 break;
1761 }
1762
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001763 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001765}
1766
1767static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001768configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001769 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001770{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001771 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001772 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001773 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001774 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001775
Pekka Paalanen77346a62011-11-30 16:26:35 +02001776 shsurf = get_shell_surface(surface);
1777 if (shsurf)
1778 surface_type = shsurf->type;
1779
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001780 surface->geometry.x = x;
1781 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001782 surface->geometry.width = width;
1783 surface->geometry.height = height;
1784 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001785
1786 switch (surface_type) {
1787 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001788 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001789 break;
Alex Wu4539b082012-03-01 12:57:46 +08001790 case SHELL_SURFACE_FULLSCREEN:
1791 shell_configure_fullscreen(shsurf);
1792 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1793 shell_stack_fullscreen(shsurf);
1794 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001795 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001796 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001797 surface->geometry.x = surface->output->x;
1798 surface->geometry.y = surface->output->y +
1799 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001800 break;
Alex Wu4539b082012-03-01 12:57:46 +08001801 case SHELL_SURFACE_TOPLEVEL:
1802 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001803 default:
1804 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001805 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001806
Alex Wu4539b082012-03-01 12:57:46 +08001807 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001808 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001809 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001810
1811 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1812 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001813 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1814 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001815 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001816}
1817
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001818static void
1819shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1820{
1821 struct weston_shell *shell = es->compositor->shell;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001822 struct shell_surface *shsurf = get_shell_surface(es);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001823
1824 if (!weston_surface_is_mapped(es)) {
1825 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001826 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001827 es->geometry.width != es->buffer->width ||
1828 es->geometry.height != es->buffer->height) {
1829 GLfloat from_x, from_y;
1830 GLfloat to_x, to_y;
1831
1832 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1833 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1834 configure(shell, es,
1835 es->geometry.x + to_x - from_x,
1836 es->geometry.y + to_y - from_y,
1837 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001838 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001839 }
1840}
1841
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001842static int launch_desktop_shell_process(struct wl_shell *shell);
1843
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001844static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001845desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001846{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001847 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001848 struct wl_shell *shell =
1849 container_of(process, struct wl_shell, child.process);
1850
1851 shell->child.process.pid = 0;
1852 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001853
1854 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1855 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001856 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001857 shell->child.deathstamp = time;
1858 shell->child.deathcount = 0;
1859 }
1860
1861 shell->child.deathcount++;
1862 if (shell->child.deathcount > 5) {
1863 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1864 return;
1865 }
1866
1867 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1868 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001869}
1870
1871static int
1872launch_desktop_shell_process(struct wl_shell *shell)
1873{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001874 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001875
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001876 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001877 &shell->child.process,
1878 shell_exe,
1879 desktop_shell_sigchld);
1880
1881 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001882 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001883 return 0;
1884}
1885
1886static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001887bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1888{
1889 struct wl_shell *shell = data;
1890
1891 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001892 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001893}
1894
Kristian Høgsberg75840622011-09-06 13:48:16 -04001895static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001896unbind_desktop_shell(struct wl_resource *resource)
1897{
1898 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001899
1900 if (shell->locked)
1901 resume_desktop(shell);
1902
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001903 shell->child.desktop_shell = NULL;
1904 shell->prepare_event_sent = false;
1905 free(resource);
1906}
1907
1908static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001909bind_desktop_shell(struct wl_client *client,
1910 void *data, uint32_t version, uint32_t id)
1911{
1912 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001913 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001914
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001915 resource = wl_client_add_object(client, &desktop_shell_interface,
1916 &desktop_shell_implementation,
1917 id, shell);
1918
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001919 if (client == shell->child.client) {
1920 resource->destroy = unbind_desktop_shell;
1921 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001922 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001923 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001924
1925 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1926 "permission to bind desktop_shell denied");
1927 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001928}
1929
Pekka Paalanen6e168112011-11-24 11:34:05 +02001930static void
1931screensaver_set_surface(struct wl_client *client,
1932 struct wl_resource *resource,
1933 struct wl_resource *shell_surface_resource,
1934 struct wl_resource *output_resource)
1935{
1936 struct wl_shell *shell = resource->data;
1937 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001938 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001939
Pekka Paalanen98262232011-12-01 10:42:22 +02001940 if (reset_shell_surface_type(surface))
1941 return;
1942
Pekka Paalanen77346a62011-11-30 16:26:35 +02001943 surface->type = SHELL_SURFACE_SCREENSAVER;
1944
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001945 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001946 surface->output = output;
1947 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001948}
1949
1950static const struct screensaver_interface screensaver_implementation = {
1951 screensaver_set_surface
1952};
1953
1954static void
1955unbind_screensaver(struct wl_resource *resource)
1956{
1957 struct wl_shell *shell = resource->data;
1958
Pekka Paalanen77346a62011-11-30 16:26:35 +02001959 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001960 free(resource);
1961}
1962
1963static void
1964bind_screensaver(struct wl_client *client,
1965 void *data, uint32_t version, uint32_t id)
1966{
1967 struct wl_shell *shell = data;
1968 struct wl_resource *resource;
1969
1970 resource = wl_client_add_object(client, &screensaver_interface,
1971 &screensaver_implementation,
1972 id, shell);
1973
Pekka Paalanen77346a62011-11-30 16:26:35 +02001974 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001975 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001976 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001977 return;
1978 }
1979
1980 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1981 "interface object already bound");
1982 wl_resource_destroy(resource, 0);
1983}
1984
Kristian Høgsberg07045392012-02-19 18:52:44 -05001985struct switcher {
1986 struct weston_compositor *compositor;
1987 struct weston_surface *current;
1988 struct wl_listener listener;
1989 struct wl_keyboard_grab grab;
1990};
1991
1992static void
1993switcher_next(struct switcher *switcher)
1994{
1995 struct weston_compositor *compositor = switcher->compositor;
1996 struct weston_surface *surface;
1997 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04001998 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001999
2000 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002001 switch (get_shell_surface_type(surface)) {
2002 case SHELL_SURFACE_TOPLEVEL:
2003 case SHELL_SURFACE_FULLSCREEN:
2004 case SHELL_SURFACE_MAXIMIZED:
2005 if (first == NULL)
2006 first = surface;
2007 if (prev == switcher->current)
2008 next = surface;
2009 prev = surface;
2010 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002011 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002012 weston_surface_damage(surface);
2013 break;
2014 default:
2015 break;
2016 }
Alex Wu1659daa2012-04-01 20:13:09 +08002017
2018 if (is_black_surface(surface, NULL)) {
2019 surface->alpha = 64;
2020 surface->geometry.dirty = 1;
2021 weston_surface_damage(surface);
2022 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002023 }
2024
2025 if (next == NULL)
2026 next = first;
2027
Alex Wu07b26062012-03-12 16:06:01 +08002028 if (next == NULL)
2029 return;
2030
Kristian Høgsberg07045392012-02-19 18:52:44 -05002031 wl_list_remove(&switcher->listener.link);
2032 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
2033 &switcher->listener.link);
2034
2035 switcher->current = next;
2036 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002037
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002038 shsurf = get_shell_surface(switcher->current);
2039 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2040 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002041}
2042
2043static void
2044switcher_handle_surface_destroy(struct wl_listener *listener,
2045 struct wl_resource *resource, uint32_t time)
2046{
2047 struct switcher *switcher =
2048 container_of(listener, struct switcher, listener);
2049
2050 switcher_next(switcher);
2051}
2052
2053static void
2054switcher_destroy(struct switcher *switcher, uint32_t time)
2055{
2056 struct weston_compositor *compositor = switcher->compositor;
2057 struct weston_surface *surface;
2058 struct weston_input_device *device =
2059 (struct weston_input_device *) switcher->grab.input_device;
2060
2061 wl_list_for_each(surface, &compositor->surface_list, link) {
2062 surface->alpha = 255;
2063 weston_surface_damage(surface);
2064 }
2065
Alex Wu07b26062012-03-12 16:06:01 +08002066 if (switcher->current)
2067 activate(compositor->shell, switcher->current, device, time);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002068 wl_list_remove(&switcher->listener.link);
2069 wl_input_device_end_keyboard_grab(&device->input_device, time);
2070 free(switcher);
2071}
2072
2073static void
2074switcher_key(struct wl_keyboard_grab *grab,
2075 uint32_t time, uint32_t key, int32_t state)
2076{
2077 struct switcher *switcher = container_of(grab, struct switcher, grab);
2078 struct weston_input_device *device =
2079 (struct weston_input_device *) grab->input_device;
2080
2081 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2082 switcher_destroy(switcher, time);
2083 } else if (key == KEY_TAB && state) {
2084 switcher_next(switcher);
2085 }
2086};
2087
2088static const struct wl_keyboard_grab_interface switcher_grab = {
2089 switcher_key
2090};
2091
2092static void
2093switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002094 uint32_t key, uint32_t button, uint32_t axis,
2095 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002096{
2097 struct weston_compositor *compositor = data;
2098 struct switcher *switcher;
2099
2100 switcher = malloc(sizeof *switcher);
2101 switcher->compositor = compositor;
2102 switcher->current = NULL;
2103 switcher->listener.func = switcher_handle_surface_destroy;
2104 wl_list_init(&switcher->listener.link);
2105
2106 switcher->grab.interface = &switcher_grab;
2107 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002108 wl_input_device_set_keyboard_focus(device, NULL,
2109 weston_compositor_get_time());
Kristian Høgsberg07045392012-02-19 18:52:44 -05002110 switcher_next(switcher);
2111}
2112
Pekka Paalanen3c647232011-12-22 13:43:43 +02002113static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002114backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002115 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002116{
2117 struct weston_compositor *compositor = data;
2118 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002119 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002120
2121 /* TODO: we're limiting to simple use cases, where we assume just
2122 * control on the primary display. We'd have to extend later if we
2123 * ever get support for setting backlights on random desktop LCD
2124 * panels though */
2125 output = get_default_output(compositor);
2126 if (!output)
2127 return;
2128
2129 if (!output->set_backlight)
2130 return;
2131
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002132 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2133 backlight_new = output->backlight_current - 25;
2134 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2135 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002136
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002137 if (backlight_new < 5)
2138 backlight_new = 5;
2139 if (backlight_new > 255)
2140 backlight_new = 255;
2141
2142 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002143 output->set_backlight(output, output->backlight_current);
2144}
2145
2146static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002147debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002148 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002149{
2150 struct weston_compositor *compositor = data;
2151 struct wl_shell *shell =
2152 container_of(compositor->shell, struct wl_shell, shell);
2153 struct weston_surface *surface;
2154
2155 if (shell->debug_repaint_surface) {
2156 weston_surface_destroy(shell->debug_repaint_surface);
2157 shell->debug_repaint_surface = NULL;
2158 } else {
2159 surface = weston_surface_create(compositor);
2160 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2161 weston_surface_configure(surface, 0, 0, 8192, 8192);
2162 wl_list_insert(&compositor->fade_layer.surface_list,
2163 &surface->layer_link);
2164 weston_surface_assign_output(surface);
2165 pixman_region32_init(&surface->input);
2166
2167 /* Here's the dirty little trick that makes the
2168 * repaint debugging work: we force an
2169 * update_transform first to update dependent state
2170 * and clear the geometry.dirty bit. Then we clear
2171 * the surface damage so it only gets repainted
2172 * piecewise as we repaint other things. */
2173
2174 weston_surface_update_transform(surface);
2175 pixman_region32_fini(&surface->damage);
2176 pixman_region32_init(&surface->damage);
2177 shell->debug_repaint_surface = surface;
2178 }
2179}
2180
2181static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002182shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002183{
2184 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2185
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002186 if (shell->child.client)
2187 wl_client_destroy(shell->child.client);
2188
Pekka Paalanen3c647232011-12-22 13:43:43 +02002189 free(shell->screensaver.path);
2190 free(shell);
2191}
2192
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002193int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002194shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002195
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002196WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002197shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002198{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002199 struct wl_shell *shell;
2200
2201 shell = malloc(sizeof *shell);
2202 if (shell == NULL)
2203 return -1;
2204
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002205 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002206 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002207 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002208 shell->shell.unlock = unlock;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002209 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002210
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002211 wl_list_init(&shell->backgrounds);
2212 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002213 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002214
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002215 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2216 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2217 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2218 weston_layer_init(&shell->background_layer,
2219 &shell->toplevel_layer.link);
2220 wl_list_init(&shell->lock_layer.surface_list);
2221
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002222 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002223
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002224 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2225 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002226 return -1;
2227
Kristian Høgsberg75840622011-09-06 13:48:16 -04002228 if (wl_display_add_global(ec->wl_display,
2229 &desktop_shell_interface,
2230 shell, bind_desktop_shell) == NULL)
2231 return -1;
2232
Pekka Paalanen6e168112011-11-24 11:34:05 +02002233 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2234 shell, bind_screensaver) == NULL)
2235 return -1;
2236
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002237 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002238 if (launch_desktop_shell_process(shell) != 0)
2239 return -1;
2240
Scott Moreau6a3633d2012-03-20 08:47:59 -06002241 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2242 move_binding, shell);
2243 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2244 resize_binding, shell);
2245 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2246 MODIFIER_CTRL | MODIFIER_ALT,
2247 terminate_binding, ec);
2248 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2249 click_to_activate_binding, ec);
Scott Moreau1b45a792012-03-22 10:58:23 -06002250 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002251 MODIFIER_SUPER | MODIFIER_ALT,
2252 surface_opacity_binding, NULL);
2253 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002254 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002255 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002256 MODIFIER_SUPER | MODIFIER_ALT,
2257 rotate_binding, NULL);
2258 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2259 switcher_binding, ec);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002260
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002261 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002262 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2263 backlight_binding, ec);
2264 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2265 backlight_binding, ec);
2266 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2267 backlight_binding, ec);
2268 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2269 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002270
Scott Moreau6a3633d2012-03-20 08:47:59 -06002271 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2272 debug_repaint_binding, ec);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002273
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002274 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002275
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002276 return 0;
2277}