blob: ac7301a1c21413961127b8399730712c1c8413c5 [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;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200129};
130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500131struct weston_move_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700132 struct wl_pointer_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500134 int32_t dx, dy;
135};
136
Scott Moreauc6d7f602012-02-23 22:28:37 -0700137struct weston_zoom_grab {
138 struct wl_keyboard_grab grab;
139};
140
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141struct rotate_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700142 struct wl_pointer_grab grab;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200143 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500144 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200145 struct {
146 int32_t x;
147 int32_t y;
148 } center;
149};
150
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500151static void
Alex Wu4539b082012-03-01 12:57:46 +0800152center_on_output(struct weston_surface *surface,
153 struct weston_output *output);
154
155static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200156shell_configuration(struct wl_shell *shell)
157{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200158 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200159 char *path = NULL;
160 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200161
162 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200163 { "path", CONFIG_KEY_STRING, &path },
164 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200165 };
166
167 struct config_section cs[] = {
168 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
169 };
170
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500171 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500172 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200173 free(config_file);
174
Pekka Paalanen7296e792011-12-07 16:22:00 +0200175 shell->screensaver.path = path;
176 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200177}
178
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179static void
Scott Moreau447013d2012-02-18 05:05:29 -0700180noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500181 struct wl_surface *surface, int32_t x, int32_t y)
182{
183 grab->focus = NULL;
184}
185
186static void
Scott Moreau447013d2012-02-18 05:05:29 -0700187move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500188 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500191 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500192 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500194 weston_surface_configure(es,
195 device->x + move->dx,
196 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200197 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198}
199
200static void
Scott Moreau447013d2012-02-18 05:05:29 -0700201move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202 uint32_t time, int32_t button, int32_t state)
203{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500204 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500205
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500206 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700207 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500208 free(grab);
209 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210}
211
Scott Moreau447013d2012-02-18 05:05:29 -0700212static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500213 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214 move_grab_motion,
215 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216};
217
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400218static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500219weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500220 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500222 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500223
224 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400225 if (!move)
226 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227
228 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200229 move->dx = es->geometry.x - wd->input_device.grab_x;
230 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500231 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500232
Scott Moreau447013d2012-02-18 05:05:29 -0700233 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200235 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400236
237 return 0;
238}
239
240static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200241shell_surface_move(struct wl_client *client, struct wl_resource *resource,
242 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500244 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200245 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400246
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500247 if (wd->input_device.button_count == 0 ||
248 wd->input_device.grab_time != time ||
249 wd->input_device.pointer_focus != &shsurf->surface->surface)
250 return;
251
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500252 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400253 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254}
255
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500256struct weston_resize_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700257 struct wl_pointer_grab grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200259 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200260 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261};
262
263static void
Scott Moreau447013d2012-02-18 05:05:29 -0700264resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265 uint32_t time, int32_t x, int32_t y)
266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500269 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200270 int32_t from_x, from_y;
271 int32_t to_x, to_y;
272
273 weston_surface_from_global(resize->shsurf->surface,
274 device->grab_x, device->grab_y,
275 &from_x, &from_y);
276 weston_surface_from_global(resize->shsurf->surface,
277 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500278
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200279 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200280 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200281 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200282 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500283 } else {
284 width = resize->width;
285 }
286
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200287 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200288 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200289 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200290 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500291 } else {
292 height = resize->height;
293 }
294
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500295 wl_shell_surface_send_configure(&resize->shsurf->resource,
296 time, resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500297}
298
299static void
Scott Moreau447013d2012-02-18 05:05:29 -0700300resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500301 uint32_t time, int32_t button, int32_t state)
302{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500303 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500304
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500305 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700306 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500307 free(grab);
308 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500309}
310
Scott Moreau447013d2012-02-18 05:05:29 -0700311static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500312 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500313 resize_grab_motion,
314 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315};
316
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400317static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318weston_surface_resize(struct shell_surface *shsurf,
319 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200320 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500321{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500322 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500323
Alex Wu4539b082012-03-01 12:57:46 +0800324 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
325 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500326
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200327 if (edges == 0 || edges > 15 ||
328 (edges & 3) == 3 || (edges & 12) == 12)
329 return 0;
330
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500331 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400332 if (!resize)
333 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500334
335 resize->grab.interface = &resize_grab_interface;
336 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200337 resize->width = shsurf->surface->geometry.width;
338 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200339 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400340
Scott Moreau447013d2012-02-18 05:05:29 -0700341 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200343 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400344
345 return 0;
346}
347
348static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200349shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
350 struct wl_resource *input_resource, uint32_t time,
351 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400352{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500353 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200354 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400355
Alex Wu4539b082012-03-01 12:57:46 +0800356 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
357 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400358
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500359 if (wd->input_device.button_count == 0 ||
360 wd->input_device.grab_time != time ||
361 wd->input_device.pointer_focus != &shsurf->surface->surface)
362 return;
363
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500364 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400365 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500366}
367
Juan Zhao96879df2012-02-07 08:45:41 +0800368static struct weston_output *
369get_default_output(struct weston_compositor *compositor)
370{
371 return container_of(compositor->output_list.next,
372 struct weston_output, link);
373}
374
Alex Wu4539b082012-03-01 12:57:46 +0800375static void
376shell_unset_fullscreen(struct shell_surface *shsurf)
377{
378 /* undo all fullscreen things here */
379 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
380 shsurf->fullscreen.framerate = 0;
381 wl_list_remove(&shsurf->fullscreen.transform.link);
382 wl_list_init(&shsurf->fullscreen.transform.link);
383 weston_surface_destroy(shsurf->fullscreen.black_surface);
384 shsurf->fullscreen.black_surface = NULL;
385 shsurf->fullscreen_output = NULL;
386 shsurf->surface->force_configure = 1;
387 weston_surface_set_position(shsurf->surface,
388 shsurf->saved_x, shsurf->saved_y);
389}
390
Pekka Paalanen98262232011-12-01 10:42:22 +0200391static int
392reset_shell_surface_type(struct shell_surface *surface)
393{
394 switch (surface->type) {
395 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800396 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200397 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800398 case SHELL_SURFACE_MAXIMIZED:
399 surface->output = get_default_output(surface->surface->compositor);
400 weston_surface_set_position(surface->surface,
401 surface->saved_x,
402 surface->saved_y);
403 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200404 case SHELL_SURFACE_PANEL:
405 case SHELL_SURFACE_BACKGROUND:
406 wl_list_remove(&surface->link);
407 wl_list_init(&surface->link);
408 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200409 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200410 case SHELL_SURFACE_LOCK:
411 wl_resource_post_error(&surface->resource,
412 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200413 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200414 return -1;
415 case SHELL_SURFACE_NONE:
416 case SHELL_SURFACE_TOPLEVEL:
417 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500418 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200419 break;
420 }
421
422 surface->type = SHELL_SURFACE_NONE;
423 return 0;
424}
425
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500426static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200427shell_surface_set_toplevel(struct wl_client *client,
428 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400429
430{
Pekka Paalanen98262232011-12-01 10:42:22 +0200431 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400432
Pekka Paalanen98262232011-12-01 10:42:22 +0200433 if (reset_shell_surface_type(surface))
434 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400435
Pekka Paalanen98262232011-12-01 10:42:22 +0200436 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400437}
438
439static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200440shell_surface_set_transient(struct wl_client *client,
441 struct wl_resource *resource,
442 struct wl_resource *parent_resource,
443 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400444{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200445 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500446 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200447 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500448 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400449
Pekka Paalanen98262232011-12-01 10:42:22 +0200450 if (reset_shell_surface_type(shsurf))
451 return;
452
Alex Wu4539b082012-03-01 12:57:46 +0800453 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800454 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200455 weston_surface_set_position(es, pes->geometry.x + x,
456 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400457
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200458 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400459}
460
Juan Zhao96879df2012-02-07 08:45:41 +0800461static struct wl_shell *
462shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200463{
Juan Zhao96879df2012-02-07 08:45:41 +0800464 struct weston_surface *es = shsurf->surface;
465 struct weston_shell *shell = es->compositor->shell;
466
467 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
468}
469
470static int
471get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
472{
473 struct shell_surface *priv;
474 int panel_height = 0;
475
476 if (!output)
477 return 0;
478
479 wl_list_for_each(priv, &wlshell->panels, link) {
480 if (priv->output == output) {
481 panel_height = priv->surface->geometry.height;
482 break;
483 }
484 }
485 return panel_height;
486}
487
488static void
489shell_surface_set_maximized(struct wl_client *client,
490 struct wl_resource *resource,
491 struct wl_resource *output_resource )
492{
493 struct shell_surface *shsurf = resource->data;
494 struct weston_surface *es = shsurf->surface;
495 struct wl_shell *wlshell = NULL;
496 uint32_t edges = 0, panel_height = 0;
497
498 /* get the default output, if the client set it as NULL
499 check whether the ouput is available */
500 if (output_resource)
501 shsurf->output = output_resource->data;
502 else
503 shsurf->output = get_default_output(es->compositor);
504
505 if (reset_shell_surface_type(shsurf))
506 return;
507
508 shsurf->saved_x = es->geometry.x;
509 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800510 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800511
512 wlshell = shell_surface_get_shell(shsurf);
513 panel_height = get_output_panel_height(wlshell, es->output);
514 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500515
516 wl_shell_surface_send_configure(&shsurf->resource,
517 weston_compositor_get_time(), edges,
518 es->output->current->width,
519 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800520
521 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200522}
523
Alex Wu4539b082012-03-01 12:57:46 +0800524static struct weston_surface *
525create_black_surface(struct weston_compositor *ec,
526 GLfloat x, GLfloat y, int w, int h)
527{
528 struct weston_surface *surface = NULL;
529
530 surface = weston_surface_create(ec);
531 if (surface == NULL) {
532 fprintf(stderr, "no memory\n");
533 return NULL;
534 }
535
536 weston_surface_configure(surface, x, y, w, h);
537 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
538 return surface;
539}
540
541/* Create black surface and append it to the associated fullscreen surface.
542 * Handle size dismatch and positioning according to the method. */
543static void
544shell_configure_fullscreen(struct shell_surface *shsurf)
545{
546 struct weston_output *output = shsurf->fullscreen_output;
547 struct weston_surface *surface = shsurf->surface;
548 struct weston_matrix *matrix;
549 float scale;
550
551 center_on_output(surface, output);
552
553 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500554 shsurf->fullscreen.black_surface =
555 create_black_surface(surface->compositor,
556 output->x, output->y,
557 output->current->width,
558 output->current->height);
559
560 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
561 wl_list_insert(&surface->layer_link,
562 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800563 shsurf->fullscreen.black_surface->output = output;
564
565 switch (shsurf->fullscreen.type) {
566 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
567 break;
568 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
569 matrix = &shsurf->fullscreen.transform.matrix;
570 weston_matrix_init(matrix);
571 scale = (float)output->current->width/(float)surface->geometry.width;
572 weston_matrix_scale(matrix, scale, scale, 1);
573 wl_list_remove(&shsurf->fullscreen.transform.link);
574 wl_list_insert(surface->geometry.transformation_list.prev,
575 &shsurf->fullscreen.transform.link);
576 weston_surface_set_position(surface, output->x, output->y);
577 break;
578 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
579 break;
580 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
581 break;
582 default:
583 break;
584 }
585}
586
587/* make the fullscreen and black surface at the top */
588static void
589shell_stack_fullscreen(struct shell_surface *shsurf)
590{
591 struct weston_surface *surface = shsurf->surface;
592 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800593
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500594 wl_list_remove(&surface->layer_link);
595 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800596
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500597 wl_list_insert(&shell->fullscreen_layer.surface_list,
598 &surface->layer_link);
599 wl_list_insert(&surface->layer_link,
600 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800601
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500602 weston_surface_damage(surface);
603 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800604}
605
606static void
607shell_map_fullscreen(struct shell_surface *shsurf)
608{
609 shell_configure_fullscreen(shsurf);
610 shell_stack_fullscreen(shsurf);
611}
612
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400613static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200614shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500615 struct wl_resource *resource,
616 uint32_t method,
617 uint32_t framerate,
618 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400619{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200620 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500621 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800622
623 if (output_resource)
624 shsurf->output = output_resource->data;
625 else
626 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400627
Pekka Paalanen98262232011-12-01 10:42:22 +0200628 if (reset_shell_surface_type(shsurf))
629 return;
630
Alex Wu4539b082012-03-01 12:57:46 +0800631 shsurf->fullscreen_output = shsurf->output;
632 shsurf->fullscreen.type = method;
633 shsurf->fullscreen.framerate = framerate;
634 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400635
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200636 shsurf->saved_x = es->geometry.x;
637 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800638 shsurf->saved_position_valid = true;
639
640 if (es->output)
641 shsurf->surface->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500642
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500643 wl_shell_surface_send_configure(&shsurf->resource,
644 weston_compositor_get_time(), 0,
645 shsurf->output->current->width,
646 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400647}
648
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500649static void
Scott Moreau447013d2012-02-18 05:05:29 -0700650popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500651 struct wl_surface *surface, int32_t x, int32_t y)
652{
653 struct wl_input_device *device = grab->input_device;
654 struct shell_surface *priv =
655 container_of(grab, struct shell_surface, popup.grab);
656 struct wl_client *client = priv->surface->surface.resource.client;
657
Pekka Paalanencb108432012-01-19 16:25:40 +0200658 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200659 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500660 grab->focus = surface;
661 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200662 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500663 grab->focus = NULL;
664 }
665}
666
667static void
Scott Moreau447013d2012-02-18 05:05:29 -0700668popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200669 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500670{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500671 struct wl_resource *resource;
672
673 resource = grab->input_device->pointer_focus_resource;
674 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500675 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500676}
677
678static void
Scott Moreau447013d2012-02-18 05:05:29 -0700679popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500680 uint32_t time, int32_t button, int32_t state)
681{
682 struct wl_resource *resource;
683 struct shell_surface *shsurf =
684 container_of(grab, struct shell_surface, popup.grab);
685
686 resource = grab->input_device->pointer_focus_resource;
687 if (resource) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500688 wl_input_device_send_button(resource, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500689 } else if (state == 0 &&
690 (shsurf->popup.initial_up ||
691 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500692 wl_shell_surface_send_popup_done(&shsurf->resource);
Scott Moreau447013d2012-02-18 05:05:29 -0700693 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500694 shsurf->popup.grab.input_device = NULL;
695 }
696
697 if (state == 0)
698 shsurf->popup.initial_up = 1;
699}
700
Scott Moreau447013d2012-02-18 05:05:29 -0700701static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500702 popup_grab_focus,
703 popup_grab_motion,
704 popup_grab_button,
705};
706
707static void
708shell_map_popup(struct shell_surface *shsurf, uint32_t time)
709{
710 struct wl_input_device *device;
711 struct weston_surface *es = shsurf->surface;
712 struct weston_surface *parent = shsurf->parent->surface;
713
714 es->output = parent->output;
715
716 shsurf->popup.grab.interface = &popup_grab_interface;
717 device = es->compositor->input_device;
718
Pekka Paalanen938269a2012-02-07 14:19:01 +0200719 weston_surface_update_transform(parent);
720 if (parent->transform.enabled) {
721 shsurf->popup.parent_transform.matrix =
722 parent->transform.matrix;
723 } else {
724 /* construct x, y translation matrix */
725 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
726 shsurf->popup.parent_transform.matrix.d[12] =
727 parent->geometry.x;
728 shsurf->popup.parent_transform.matrix.d[13] =
729 parent->geometry.y;
730 }
731 wl_list_insert(es->geometry.transformation_list.prev,
732 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200733 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500734
735 shsurf->popup.grab.input_device = device;
736 shsurf->popup.time = device->grab_time;
737 shsurf->popup.initial_up = 0;
738
Scott Moreau447013d2012-02-18 05:05:29 -0700739 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500740 &shsurf->popup.grab, shsurf->popup.time);
741}
742
743static void
744shell_surface_set_popup(struct wl_client *client,
745 struct wl_resource *resource,
746 struct wl_resource *input_device_resource,
747 uint32_t time,
748 struct wl_resource *parent_resource,
749 int32_t x, int32_t y, uint32_t flags)
750{
751 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500752
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500753 shsurf->type = SHELL_SURFACE_POPUP;
754 shsurf->parent = parent_resource->data;
755 shsurf->popup.x = x;
756 shsurf->popup.y = y;
757}
758
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200759static const struct wl_shell_surface_interface shell_surface_implementation = {
760 shell_surface_move,
761 shell_surface_resize,
762 shell_surface_set_toplevel,
763 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500764 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800765 shell_surface_set_popup,
766 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200767};
768
769static void
770destroy_shell_surface(struct wl_resource *resource)
771{
772 struct shell_surface *shsurf = resource->data;
773
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500774 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700775 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500776
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200777 /* in case cleaning up a dead client destroys shell_surface first */
778 if (shsurf->surface)
779 wl_list_remove(&shsurf->surface_destroy_listener.link);
780
781 wl_list_remove(&shsurf->link);
782 free(shsurf);
783}
784
785static void
786shell_handle_surface_destroy(struct wl_listener *listener,
787 struct wl_resource *resource, uint32_t time)
788{
789 struct shell_surface *shsurf = container_of(listener,
790 struct shell_surface,
791 surface_destroy_listener);
792
793 shsurf->surface = NULL;
794 wl_resource_destroy(&shsurf->resource, time);
795}
796
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200797static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500798get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200799{
800 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
801 struct wl_listener *listener;
802
803 /* search the destroy listener list for our callback */
804 wl_list_for_each(listener, lst, link) {
805 if (listener->func == shell_handle_surface_destroy) {
806 return container_of(listener, struct shell_surface,
807 surface_destroy_listener);
808 }
809 }
810
811 return NULL;
812}
813
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200814static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200815shell_get_shell_surface(struct wl_client *client,
816 struct wl_resource *resource,
817 uint32_t id,
818 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200819{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500820 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200821 struct shell_surface *shsurf;
822
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200823 if (get_shell_surface(surface)) {
824 wl_resource_post_error(surface_resource,
825 WL_DISPLAY_ERROR_INVALID_OBJECT,
826 "wl_shell::get_shell_surface already requested");
827 return;
828 }
829
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200830 shsurf = calloc(1, sizeof *shsurf);
831 if (!shsurf) {
832 wl_resource_post_no_memory(resource);
833 return;
834 }
835
836 shsurf->resource.destroy = destroy_shell_surface;
837 shsurf->resource.object.id = id;
838 shsurf->resource.object.interface = &wl_shell_surface_interface;
839 shsurf->resource.object.implementation =
840 (void (**)(void)) &shell_surface_implementation;
841 shsurf->resource.data = shsurf;
842
Alex Wu4539b082012-03-01 12:57:46 +0800843 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200844 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800845 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
846 shsurf->fullscreen.framerate = 0;
847 shsurf->fullscreen.black_surface = NULL;
848 wl_list_init(&shsurf->fullscreen.transform.link);
849
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200850 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
851 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
852 &shsurf->surface_destroy_listener.link);
853
854 /* init link so its safe to always remove it in destroy_shell_surface */
855 wl_list_init(&shsurf->link);
856
Pekka Paalanen460099f2012-01-20 16:48:25 +0200857 /* empty when not in use */
858 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500859 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200860
Pekka Paalanen98262232011-12-01 10:42:22 +0200861 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200862
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200863 wl_client_add_resource(client, &shsurf->resource);
864}
865
866static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200867 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500868};
869
Kristian Høgsberg07937562011-04-12 17:25:42 -0400870static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500871handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200872{
873 proc->pid = 0;
874}
875
876static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200877launch_screensaver(struct wl_shell *shell)
878{
879 if (shell->screensaver.binding)
880 return;
881
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200882 if (!shell->screensaver.path)
883 return;
884
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500885 if (shell->screensaver.process.pid != 0) {
886 fprintf(stderr, "old screensaver still running\n");
887 return;
888 }
889
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500890 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200891 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200892 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200893 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200894}
895
896static void
897terminate_screensaver(struct wl_shell *shell)
898{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200899 if (shell->screensaver.process.pid == 0)
900 return;
901
902 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200903}
904
905static void
906show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
907{
908 struct wl_list *list;
909
910 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500911 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200912 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500913 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200914
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500915 wl_list_remove(&surface->surface->layer_link);
916 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200917 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200918 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200919}
920
921static void
922hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
923{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500924 wl_list_remove(&surface->surface->layer_link);
925 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200926 surface->surface->output = NULL;
927}
928
929static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400930desktop_shell_set_background(struct wl_client *client,
931 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100932 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400933 struct wl_resource *surface_resource)
934{
935 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200936 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500937 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200938 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400939
Pekka Paalanen98262232011-12-01 10:42:22 +0200940 if (reset_shell_surface_type(shsurf))
941 return;
942
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100943 wl_list_for_each(priv, &shell->backgrounds, link) {
944 if (priv->output == output_resource->data) {
945 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500946 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100947 wl_list_remove(&priv->link);
948 break;
949 }
950 }
951
Pekka Paalanen068ae942011-11-28 14:11:15 +0200952 shsurf->type = SHELL_SURFACE_BACKGROUND;
953 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100954
Pekka Paalanen068ae942011-11-28 14:11:15 +0200955 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100956
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200957 weston_surface_set_position(surface, shsurf->output->x,
958 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200959
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500960 desktop_shell_send_configure(resource,
961 weston_compositor_get_time(), 0,
962 surface_resource,
963 shsurf->output->current->width,
964 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400965}
966
967static void
968desktop_shell_set_panel(struct wl_client *client,
969 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100970 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400971 struct wl_resource *surface_resource)
972{
973 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200974 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500975 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200976 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400977
Pekka Paalanen98262232011-12-01 10:42:22 +0200978 if (reset_shell_surface_type(shsurf))
979 return;
980
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100981 wl_list_for_each(priv, &shell->panels, link) {
982 if (priv->output == output_resource->data) {
983 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500984 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100985 wl_list_remove(&priv->link);
986 break;
987 }
988 }
989
Pekka Paalanen068ae942011-11-28 14:11:15 +0200990 shsurf->type = SHELL_SURFACE_PANEL;
991 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100992
Pekka Paalanen068ae942011-11-28 14:11:15 +0200993 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100994
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200995 weston_surface_set_position(surface, shsurf->output->x,
996 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200997
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500998 desktop_shell_send_configure(resource,
999 weston_compositor_get_time(), 0,
1000 surface_resource,
1001 shsurf->output->current->width,
1002 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001003}
1004
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001005static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001006handle_lock_surface_destroy(struct wl_listener *listener,
1007 struct wl_resource *resource, uint32_t time)
1008{
1009 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001010 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001011
1012 fprintf(stderr, "lock surface gone\n");
1013 shell->lock_surface = NULL;
1014}
1015
1016static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001017desktop_shell_set_lock_surface(struct wl_client *client,
1018 struct wl_resource *resource,
1019 struct wl_resource *surface_resource)
1020{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001021 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001022 struct shell_surface *surface = surface_resource->data;
1023
1024 if (reset_shell_surface_type(surface))
1025 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001026
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001027 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001028
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001029 if (!shell->locked)
1030 return;
1031
Pekka Paalanen98262232011-12-01 10:42:22 +02001032 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001033
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001034 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1035 wl_list_insert(&surface_resource->destroy_listener_list,
1036 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001037
Pekka Paalanen068ae942011-11-28 14:11:15 +02001038 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001039}
1040
1041static void
1042resume_desktop(struct wl_shell *shell)
1043{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001044 struct shell_surface *tmp;
1045
1046 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1047 hide_screensaver(shell, tmp);
1048
1049 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001050
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001051 wl_list_remove(&shell->lock_layer.link);
1052 wl_list_insert(&shell->compositor->cursor_layer.link,
1053 &shell->fullscreen_layer.link);
1054 wl_list_insert(&shell->fullscreen_layer.link,
1055 &shell->panel_layer.link);
1056 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001057
1058 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001059 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001060 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001062 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001063}
1064
1065static void
1066desktop_shell_unlock(struct wl_client *client,
1067 struct wl_resource *resource)
1068{
1069 struct wl_shell *shell = resource->data;
1070
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001071 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001072
1073 if (shell->locked)
1074 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001075}
1076
Kristian Høgsberg75840622011-09-06 13:48:16 -04001077static const struct desktop_shell_interface desktop_shell_implementation = {
1078 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001079 desktop_shell_set_panel,
1080 desktop_shell_set_lock_surface,
1081 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001082};
1083
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001084static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001085get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001086{
1087 struct shell_surface *shsurf;
1088
1089 shsurf = get_shell_surface(surface);
1090 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001091 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001092 return shsurf->type;
1093}
1094
Kristian Høgsberg75840622011-09-06 13:48:16 -04001095static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001096move_binding(struct wl_input_device *device, uint32_t time,
1097 uint32_t key, uint32_t button, uint32_t state, void *data)
1098{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001099 struct weston_surface *surface =
1100 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001101
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001102 if (surface == NULL)
1103 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001104
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001105 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001106 case SHELL_SURFACE_PANEL:
1107 case SHELL_SURFACE_BACKGROUND:
1108 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001109 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001110 return;
1111 default:
1112 break;
1113 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001114
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001115 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001116}
1117
1118static void
1119resize_binding(struct wl_input_device *device, uint32_t time,
1120 uint32_t key, uint32_t button, uint32_t state, void *data)
1121{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001122 struct weston_surface *surface =
1123 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001124 uint32_t edges = 0;
1125 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001126 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001127
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001128 if (surface == NULL)
1129 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001130
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001131 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001132 if (!shsurf)
1133 return;
1134
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001135 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001136 case SHELL_SURFACE_PANEL:
1137 case SHELL_SURFACE_BACKGROUND:
1138 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001139 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001140 return;
1141 default:
1142 break;
1143 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001144
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001145 weston_surface_from_global(surface,
1146 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001147
Pekka Paalanen60921e52012-01-25 15:55:43 +02001148 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001149 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001150 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001151 edges |= 0;
1152 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001153 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001154
Pekka Paalanen60921e52012-01-25 15:55:43 +02001155 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001156 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001157 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001158 edges |= 0;
1159 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001161
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001162 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001164}
1165
1166static void
Scott Moreauc6d7f602012-02-23 22:28:37 -07001167zoom_grab_key(struct wl_keyboard_grab *grab,
1168 uint32_t time, uint32_t key, int32_t state)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001169{
Scott Moreauc6d7f602012-02-23 22:28:37 -07001170 struct wl_input_device *device = grab->input_device;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001171 struct weston_input_device *wd = (struct weston_input_device *) device;
1172 struct weston_compositor *compositor = wd->compositor;
1173 struct weston_output *output;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001174 struct weston_zoom_grab *zoom;
1175
1176 if (!(wd->modifier_state & MODIFIER_SUPER)) {
1177 zoom = container_of(grab, struct weston_zoom_grab, grab);
1178 wl_input_device_end_keyboard_grab(device, time);
1179 free(zoom);
1180 return;
1181 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001182
1183 wl_list_for_each(output, &compositor->output_list, link) {
1184 if (pixman_region32_contains_point(&output->region,
1185 device->x, device->y, NULL)) {
Scott Moreauc6d7f602012-02-23 22:28:37 -07001186 if (state && key == KEY_UP) {
1187 output->zoom.active = 1;
1188 output->zoom.level -= output->zoom.increment;
1189 }
1190 if (state && key == KEY_DOWN)
1191 output->zoom.level += output->zoom.increment;
1192
1193 if (output->zoom.level >= 1.0) {
1194 output->zoom.active = 0;
1195 output->zoom.level = 1.0;
1196 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001197
1198 if (output->zoom.level < output->zoom.increment)
1199 output->zoom.level = output->zoom.increment;
1200
1201 weston_output_update_zoom(output, device->x, device->y);
1202 }
1203 }
1204}
1205
Scott Moreauc6d7f602012-02-23 22:28:37 -07001206static const struct wl_keyboard_grab_interface zoom_grab = {
1207 zoom_grab_key,
1208};
1209
Scott Moreauccbf29d2012-02-22 14:21:41 -07001210static void
Scott Moreauc6d7f602012-02-23 22:28:37 -07001211zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreauccbf29d2012-02-22 14:21:41 -07001212 uint32_t key, uint32_t button, uint32_t state, void *data)
1213{
1214 struct weston_input_device *wd = (struct weston_input_device *) device;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001215 struct weston_zoom_grab *zoom;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001216
Scott Moreauc6d7f602012-02-23 22:28:37 -07001217 zoom = malloc(sizeof *zoom);
1218 if (!zoom)
1219 return;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001220
Scott Moreauc6d7f602012-02-23 22:28:37 -07001221 zoom->grab.interface = &zoom_grab;
1222
1223 wl_input_device_start_keyboard_grab(&wd->input_device, &zoom->grab, time);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001224}
1225
1226static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001227terminate_binding(struct wl_input_device *device, uint32_t time,
1228 uint32_t key, uint32_t button, uint32_t state, void *data)
1229{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001230 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001231
1232 if (state)
1233 wl_display_terminate(compositor->wl_display);
1234}
1235
1236static void
Scott Moreau447013d2012-02-18 05:05:29 -07001237rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001238 uint32_t time, int32_t x, int32_t y)
1239{
1240 struct rotate_grab *rotate =
1241 container_of(grab, struct rotate_grab, grab);
1242 struct wl_input_device *device = grab->input_device;
1243 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001244 GLfloat cx = 0.5f * surface->surface->geometry.width;
1245 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001246 GLfloat dx, dy;
1247 GLfloat r;
1248
1249 dx = device->x - rotate->center.x;
1250 dy = device->y - rotate->center.y;
1251 r = sqrtf(dx * dx + dy * dy);
1252
1253 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001254 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001255
1256 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001257 struct weston_matrix *matrix =
1258 &surface->rotation.transform.matrix;
1259
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001260 weston_matrix_init(&rotate->rotation);
1261 rotate->rotation.d[0] = dx / r;
1262 rotate->rotation.d[4] = -dy / r;
1263 rotate->rotation.d[1] = -rotate->rotation.d[4];
1264 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001265
1266 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001267 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001268 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1269 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001270 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001271
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001272 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001273 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001274 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001275 } else {
1276 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001277 weston_matrix_init(&surface->rotation.rotation);
1278 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001279 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001280
1281 /* Repaint implies weston_surface_update_transform(), which
1282 * lazily applies the damage due to rotation update.
1283 */
1284 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001285}
1286
1287static void
Scott Moreau447013d2012-02-18 05:05:29 -07001288rotate_grab_button(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001289 uint32_t time, int32_t button, int32_t state)
1290{
1291 struct rotate_grab *rotate =
1292 container_of(grab, struct rotate_grab, grab);
1293 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001294 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001295
1296 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001297 weston_matrix_multiply(&surface->rotation.rotation,
1298 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001299 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001300 free(rotate);
1301 }
1302}
1303
Scott Moreau447013d2012-02-18 05:05:29 -07001304static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001305 noop_grab_focus,
1306 rotate_grab_motion,
1307 rotate_grab_button,
1308};
1309
1310static void
1311rotate_binding(struct wl_input_device *device, uint32_t time,
1312 uint32_t key, uint32_t button, uint32_t state, void *data)
1313{
1314 struct weston_surface *base_surface =
1315 (struct weston_surface *) device->pointer_focus;
1316 struct shell_surface *surface;
1317 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001318 GLfloat dx, dy;
1319 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001320
1321 if (base_surface == NULL)
1322 return;
1323
1324 surface = get_shell_surface(base_surface);
1325 if (!surface)
1326 return;
1327
1328 switch (surface->type) {
1329 case SHELL_SURFACE_PANEL:
1330 case SHELL_SURFACE_BACKGROUND:
1331 case SHELL_SURFACE_FULLSCREEN:
1332 case SHELL_SURFACE_SCREENSAVER:
1333 return;
1334 default:
1335 break;
1336 }
1337
Pekka Paalanen460099f2012-01-20 16:48:25 +02001338 rotate = malloc(sizeof *rotate);
1339 if (!rotate)
1340 return;
1341
1342 rotate->grab.interface = &rotate_grab_interface;
1343 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001344
1345 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001346 surface->surface->geometry.width / 2,
1347 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001348 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001349
Scott Moreau447013d2012-02-18 05:05:29 -07001350 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001351
1352 dx = device->x - rotate->center.x;
1353 dy = device->y - rotate->center.y;
1354 r = sqrtf(dx * dx + dy * dy);
1355 if (r > 20.0f) {
1356 struct weston_matrix inverse;
1357
1358 weston_matrix_init(&inverse);
1359 inverse.d[0] = dx / r;
1360 inverse.d[4] = dy / r;
1361 inverse.d[1] = -inverse.d[4];
1362 inverse.d[5] = inverse.d[0];
1363 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1364 } else {
1365 weston_matrix_init(&surface->rotation.rotation);
1366 weston_matrix_init(&rotate->rotation);
1367 }
1368
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001369 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001370}
1371
1372static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001373activate(struct weston_shell *base, struct weston_surface *es,
1374 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001375{
1376 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001377 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001378
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001380
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001381 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001382 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001383
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001384 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001385 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001386 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001387 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001388 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001389
Pekka Paalanen77346a62011-11-30 16:26:35 +02001390 case SHELL_SURFACE_SCREENSAVER:
1391 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001392 if (shell->lock_surface)
1393 weston_surface_restack(es,
1394 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001395 break;
Alex Wu4539b082012-03-01 12:57:46 +08001396 case SHELL_SURFACE_FULLSCREEN:
1397 /* should on top of panels */
1398 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001399 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001400 weston_surface_restack(es,
1401 &shell->toplevel_layer.surface_list);
1402 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001403 }
1404}
1405
1406static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001407click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001408 uint32_t time, uint32_t key,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001409 uint32_t button, uint32_t state, void *data)
1410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001411 struct weston_input_device *wd = (struct weston_input_device *) device;
1412 struct weston_compositor *compositor = data;
1413 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001414 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001415
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu4539b082012-03-01 12:57:46 +08001417 upper = container_of(focus->link.prev, struct weston_surface, link);
1418 if (focus->link.prev != &compositor->surface_list &&
1419 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1420 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1421 shell_stack_fullscreen(get_shell_surface(upper));
1422 focus = upper;
1423 }
1424
Scott Moreau447013d2012-02-18 05:05:29 -07001425 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001426 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001427}
1428
1429static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001430lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001431{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001432 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001433 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001434 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001435 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001436 uint32_t time;
1437
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001438 if (shell->locked) {
1439 wl_list_for_each(output, &shell->compositor->output_list, link)
1440 /* TODO: find a way to jump to other DPMS levels */
1441 if (output->set_dpms)
1442 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001443 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001444 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001445
1446 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001447
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001448 /* Hide all surfaces by removing the fullscreen, panel and
1449 * toplevel layers. This way nothing else can show or receive
1450 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001451
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001452 wl_list_remove(&shell->panel_layer.link);
1453 wl_list_remove(&shell->toplevel_layer.link);
1454 wl_list_remove(&shell->fullscreen_layer.link);
1455 wl_list_insert(&shell->compositor->cursor_layer.link,
1456 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001457
Pekka Paalanen77346a62011-11-30 16:26:35 +02001458 launch_screensaver(shell);
1459
1460 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1461 show_screensaver(shell, shsurf);
1462
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001463 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001464 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001465 weston_compositor_wake(shell->compositor);
1466 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001467 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001468
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001469 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001471
1472 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001473 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001474 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1475 wl_input_device_set_keyboard_focus(&device->input_device,
1476 NULL, time);
1477 }
1478
1479 /* TODO: disable bindings that should not work while locked. */
1480
1481 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001482}
1483
1484static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001486{
1487 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1488
Pekka Paalanend81c2162011-11-16 13:47:34 +02001489 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001491 return;
1492 }
1493
1494 /* If desktop-shell client has gone away, unlock immediately. */
1495 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001496 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001497 return;
1498 }
1499
1500 if (shell->prepare_event_sent)
1501 return;
1502
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001503 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001504 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001505}
1506
1507static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001508center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001511 GLfloat x = (mode->width - surface->geometry.width) / 2;
1512 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001513
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001514 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001515}
1516
1517static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001518map(struct weston_shell *base, struct weston_surface *surface,
1519 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001520{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001521 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001522 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001523 struct shell_surface *shsurf;
1524 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Juan Zhao96879df2012-02-07 08:45:41 +08001525 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001526
Pekka Paalanen77346a62011-11-30 16:26:35 +02001527 shsurf = get_shell_surface(surface);
1528 if (shsurf)
1529 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001530
Pekka Paalanen60921e52012-01-25 15:55:43 +02001531 surface->geometry.width = width;
1532 surface->geometry.height = height;
1533 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001534
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001535 weston_compositor_update_drag_surfaces(compositor);
1536
Pekka Paalanen77346a62011-11-30 16:26:35 +02001537 /* initial positioning, see also configure() */
1538 switch (surface_type) {
1539 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001540 weston_surface_set_position(surface, 10 + random() % 400,
1541 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001542 break;
1543 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001544 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001545 break;
Alex Wu4539b082012-03-01 12:57:46 +08001546 case SHELL_SURFACE_FULLSCREEN:
1547 shell_map_fullscreen(shsurf);
1548 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001549 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001550 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001551 panel_height = get_output_panel_height(shell,surface->output);
1552 weston_surface_set_position(surface, surface->output->x,
1553 surface->output->y + panel_height);
1554 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001555 case SHELL_SURFACE_LOCK:
1556 center_on_output(surface, get_default_output(compositor));
1557 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001558 case SHELL_SURFACE_POPUP:
1559 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001560 case SHELL_SURFACE_NONE:
1561 weston_surface_set_position(surface,
1562 surface->geometry.x + sx,
1563 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001564 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001565 default:
1566 ;
1567 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001568
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001569 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001570 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001571 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001572 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001573 wl_list_insert(&shell->background_layer.surface_list,
1574 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001575 break;
1576 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001577 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001578 wl_list_insert(&shell->panel_layer.surface_list,
1579 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001580 break;
1581 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001582 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001583 wl_list_insert(&shell->lock_layer.surface_list,
1584 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001585 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001586 break;
1587 case SHELL_SURFACE_SCREENSAVER:
1588 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001589 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001590 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001591 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001592 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001593 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001594 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001595 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001596 break;
Alex Wu4539b082012-03-01 12:57:46 +08001597 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001598 case SHELL_SURFACE_NONE:
1599 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001600 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001601 wl_list_insert(&shell->toplevel_layer.surface_list,
1602 &surface->layer_link);
1603 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001604 }
1605
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001606 weston_surface_assign_output(surface);
1607 weston_compositor_repick(compositor);
1608 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1609 surface->output = shsurf->output;
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001610
Juan Zhao7bb92f02011-12-15 11:31:51 -05001611 switch (surface_type) {
1612 case SHELL_SURFACE_TOPLEVEL:
1613 case SHELL_SURFACE_TRANSIENT:
1614 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001615 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001616 if (!shell->locked)
1617 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001618 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001619 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001620 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001621 break;
1622 default:
1623 break;
1624 }
1625
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001626 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001627 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001628}
1629
1630static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001631configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001632 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001633{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001634 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001635 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001636 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001637 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001638
Pekka Paalanen77346a62011-11-30 16:26:35 +02001639 shsurf = get_shell_surface(surface);
1640 if (shsurf)
1641 surface_type = shsurf->type;
1642
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001643 surface->geometry.x = x;
1644 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001645 surface->geometry.width = width;
1646 surface->geometry.height = height;
1647 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001648
1649 switch (surface_type) {
1650 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001651 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001652 break;
Alex Wu4539b082012-03-01 12:57:46 +08001653 case SHELL_SURFACE_FULLSCREEN:
1654 shell_configure_fullscreen(shsurf);
1655 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1656 shell_stack_fullscreen(shsurf);
1657 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001658 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001659 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001660 surface->geometry.x = surface->output->x;
1661 surface->geometry.y = surface->output->y +
1662 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001663 break;
Alex Wu4539b082012-03-01 12:57:46 +08001664 case SHELL_SURFACE_TOPLEVEL:
1665 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001666 default:
1667 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001668 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001669
Alex Wu4539b082012-03-01 12:57:46 +08001670 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001671 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001672 weston_surface_assign_output(surface);
Alex Wu4539b082012-03-01 12:57:46 +08001673 weston_compositor_repick(surface->compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001674
1675 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1676 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001677 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1678 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001679 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001680}
1681
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001682static int launch_desktop_shell_process(struct wl_shell *shell);
1683
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001684static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001685desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001686{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001687 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001688 struct wl_shell *shell =
1689 container_of(process, struct wl_shell, child.process);
1690
1691 shell->child.process.pid = 0;
1692 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001693
1694 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1695 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001696 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001697 shell->child.deathstamp = time;
1698 shell->child.deathcount = 0;
1699 }
1700
1701 shell->child.deathcount++;
1702 if (shell->child.deathcount > 5) {
1703 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1704 return;
1705 }
1706
1707 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1708 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001709}
1710
1711static int
1712launch_desktop_shell_process(struct wl_shell *shell)
1713{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001714 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001715
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001717 &shell->child.process,
1718 shell_exe,
1719 desktop_shell_sigchld);
1720
1721 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001722 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001723 return 0;
1724}
1725
1726static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001727bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1728{
1729 struct wl_shell *shell = data;
1730
1731 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001732 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001733}
1734
Kristian Høgsberg75840622011-09-06 13:48:16 -04001735static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001736unbind_desktop_shell(struct wl_resource *resource)
1737{
1738 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001739
1740 if (shell->locked)
1741 resume_desktop(shell);
1742
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001743 shell->child.desktop_shell = NULL;
1744 shell->prepare_event_sent = false;
1745 free(resource);
1746}
1747
1748static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001749bind_desktop_shell(struct wl_client *client,
1750 void *data, uint32_t version, uint32_t id)
1751{
1752 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001753 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001754
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001755 resource = wl_client_add_object(client, &desktop_shell_interface,
1756 &desktop_shell_implementation,
1757 id, shell);
1758
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001759 if (client == shell->child.client) {
1760 resource->destroy = unbind_desktop_shell;
1761 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001762 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001763 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001764
1765 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1766 "permission to bind desktop_shell denied");
1767 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001768}
1769
Pekka Paalanen6e168112011-11-24 11:34:05 +02001770static void
1771screensaver_set_surface(struct wl_client *client,
1772 struct wl_resource *resource,
1773 struct wl_resource *shell_surface_resource,
1774 struct wl_resource *output_resource)
1775{
1776 struct wl_shell *shell = resource->data;
1777 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001778 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001779
Pekka Paalanen98262232011-12-01 10:42:22 +02001780 if (reset_shell_surface_type(surface))
1781 return;
1782
Pekka Paalanen77346a62011-11-30 16:26:35 +02001783 surface->type = SHELL_SURFACE_SCREENSAVER;
1784
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001785 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001786 surface->output = output;
1787 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001788}
1789
1790static const struct screensaver_interface screensaver_implementation = {
1791 screensaver_set_surface
1792};
1793
1794static void
1795unbind_screensaver(struct wl_resource *resource)
1796{
1797 struct wl_shell *shell = resource->data;
1798
Pekka Paalanen77346a62011-11-30 16:26:35 +02001799 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001800 free(resource);
1801}
1802
1803static void
1804bind_screensaver(struct wl_client *client,
1805 void *data, uint32_t version, uint32_t id)
1806{
1807 struct wl_shell *shell = data;
1808 struct wl_resource *resource;
1809
1810 resource = wl_client_add_object(client, &screensaver_interface,
1811 &screensaver_implementation,
1812 id, shell);
1813
Pekka Paalanen77346a62011-11-30 16:26:35 +02001814 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001815 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001816 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001817 return;
1818 }
1819
1820 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1821 "interface object already bound");
1822 wl_resource_destroy(resource, 0);
1823}
1824
Kristian Høgsberg07045392012-02-19 18:52:44 -05001825struct switcher {
1826 struct weston_compositor *compositor;
1827 struct weston_surface *current;
1828 struct wl_listener listener;
1829 struct wl_keyboard_grab grab;
1830};
1831
1832static void
1833switcher_next(struct switcher *switcher)
1834{
1835 struct weston_compositor *compositor = switcher->compositor;
1836 struct weston_surface *surface;
1837 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1838
1839 wl_list_for_each(surface, &compositor->surface_list, link) {
1840 /* Workaround for cursor surfaces. */
1841 if (surface->surface.resource.destroy_listener_list.next == NULL)
1842 continue;
1843
1844 switch (get_shell_surface_type(surface)) {
1845 case SHELL_SURFACE_TOPLEVEL:
1846 case SHELL_SURFACE_FULLSCREEN:
1847 case SHELL_SURFACE_MAXIMIZED:
1848 if (first == NULL)
1849 first = surface;
1850 if (prev == switcher->current)
1851 next = surface;
1852 prev = surface;
1853 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001854 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001855 weston_surface_damage(surface);
1856 break;
1857 default:
1858 break;
1859 }
1860 }
1861
1862 if (next == NULL)
1863 next = first;
1864
1865 wl_list_remove(&switcher->listener.link);
1866 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1867 &switcher->listener.link);
1868
1869 switcher->current = next;
1870 next->alpha = 255;
1871}
1872
1873static void
1874switcher_handle_surface_destroy(struct wl_listener *listener,
1875 struct wl_resource *resource, uint32_t time)
1876{
1877 struct switcher *switcher =
1878 container_of(listener, struct switcher, listener);
1879
1880 switcher_next(switcher);
1881}
1882
1883static void
1884switcher_destroy(struct switcher *switcher, uint32_t time)
1885{
1886 struct weston_compositor *compositor = switcher->compositor;
1887 struct weston_surface *surface;
1888 struct weston_input_device *device =
1889 (struct weston_input_device *) switcher->grab.input_device;
1890
1891 wl_list_for_each(surface, &compositor->surface_list, link) {
1892 surface->alpha = 255;
1893 weston_surface_damage(surface);
1894 }
1895
1896 activate(compositor->shell, switcher->current, device, time);
1897 wl_list_remove(&switcher->listener.link);
1898 wl_input_device_end_keyboard_grab(&device->input_device, time);
1899 free(switcher);
1900}
1901
1902static void
1903switcher_key(struct wl_keyboard_grab *grab,
1904 uint32_t time, uint32_t key, int32_t state)
1905{
1906 struct switcher *switcher = container_of(grab, struct switcher, grab);
1907 struct weston_input_device *device =
1908 (struct weston_input_device *) grab->input_device;
1909
1910 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1911 switcher_destroy(switcher, time);
1912 } else if (key == KEY_TAB && state) {
1913 switcher_next(switcher);
1914 }
1915};
1916
1917static const struct wl_keyboard_grab_interface switcher_grab = {
1918 switcher_key
1919};
1920
1921static void
1922switcher_binding(struct wl_input_device *device, uint32_t time,
1923 uint32_t key, uint32_t button,
1924 uint32_t state, void *data)
1925{
1926 struct weston_compositor *compositor = data;
1927 struct switcher *switcher;
1928
1929 switcher = malloc(sizeof *switcher);
1930 switcher->compositor = compositor;
1931 switcher->current = NULL;
1932 switcher->listener.func = switcher_handle_surface_destroy;
1933 wl_list_init(&switcher->listener.link);
1934
1935 switcher->grab.interface = &switcher_grab;
1936 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1937 switcher_next(switcher);
1938}
1939
Pekka Paalanen3c647232011-12-22 13:43:43 +02001940static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001941backlight_binding(struct wl_input_device *device, uint32_t time,
1942 uint32_t key, uint32_t button, uint32_t state, void *data)
1943{
1944 struct weston_compositor *compositor = data;
1945 struct weston_output *output;
1946
1947 /* TODO: we're limiting to simple use cases, where we assume just
1948 * control on the primary display. We'd have to extend later if we
1949 * ever get support for setting backlights on random desktop LCD
1950 * panels though */
1951 output = get_default_output(compositor);
1952 if (!output)
1953 return;
1954
1955 if (!output->set_backlight)
1956 return;
1957
1958 if ((key == KEY_F9 || key == KEY_BRIGHTNESSDOWN) &&
1959 output->backlight_current > 1)
1960 output->backlight_current--;
1961 else if ((key == KEY_F10 || key == KEY_BRIGHTNESSUP) &&
1962 output->backlight_current < 10)
1963 output->backlight_current++;
1964
1965 output->set_backlight(output, output->backlight_current);
1966}
1967
1968static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05001969debug_repaint_binding(struct wl_input_device *device, uint32_t time,
1970 uint32_t key, uint32_t button, uint32_t state, void *data)
1971{
1972 struct weston_compositor *compositor = data;
1973 struct wl_shell *shell =
1974 container_of(compositor->shell, struct wl_shell, shell);
1975 struct weston_surface *surface;
1976
1977 if (shell->debug_repaint_surface) {
1978 weston_surface_destroy(shell->debug_repaint_surface);
1979 shell->debug_repaint_surface = NULL;
1980 } else {
1981 surface = weston_surface_create(compositor);
1982 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
1983 weston_surface_configure(surface, 0, 0, 8192, 8192);
1984 wl_list_insert(&compositor->fade_layer.surface_list,
1985 &surface->layer_link);
1986 weston_surface_assign_output(surface);
1987 pixman_region32_init(&surface->input);
1988
1989 /* Here's the dirty little trick that makes the
1990 * repaint debugging work: we force an
1991 * update_transform first to update dependent state
1992 * and clear the geometry.dirty bit. Then we clear
1993 * the surface damage so it only gets repainted
1994 * piecewise as we repaint other things. */
1995
1996 weston_surface_update_transform(surface);
1997 pixman_region32_fini(&surface->damage);
1998 pixman_region32_init(&surface->damage);
1999 shell->debug_repaint_surface = surface;
2000 }
2001}
2002
2003static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002004shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002005{
2006 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2007
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002008 if (shell->child.client)
2009 wl_client_destroy(shell->child.client);
2010
Pekka Paalanen3c647232011-12-22 13:43:43 +02002011 free(shell->screensaver.path);
2012 free(shell);
2013}
2014
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002015int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002017
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002018WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002020{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002021 struct wl_shell *shell;
2022
2023 shell = malloc(sizeof *shell);
2024 if (shell == NULL)
2025 return -1;
2026
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002027 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002028 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002029 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002030 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002031 shell->shell.map = map;
2032 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002033 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002034
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002035 wl_list_init(&shell->backgrounds);
2036 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002037 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002038
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002039 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2040 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2041 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2042 weston_layer_init(&shell->background_layer,
2043 &shell->toplevel_layer.link);
2044 wl_list_init(&shell->lock_layer.surface_list);
2045
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002046 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002047
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002048 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2049 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002050 return -1;
2051
Kristian Høgsberg75840622011-09-06 13:48:16 -04002052 if (wl_display_add_global(ec->wl_display,
2053 &desktop_shell_interface,
2054 shell, bind_desktop_shell) == NULL)
2055 return -1;
2056
Pekka Paalanen6e168112011-11-24 11:34:05 +02002057 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2058 shell, bind_screensaver) == NULL)
2059 return -1;
2060
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002061 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002062 if (launch_desktop_shell_process(shell) != 0)
2063 return -1;
2064
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002065 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002066 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002067 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002068 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002069 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002070 MODIFIER_CTRL | MODIFIER_ALT,
2071 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002072 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002073 click_to_activate_binding, ec);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002074 weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07002075 zoom_binding, shell);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002076 weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07002077 zoom_binding, shell);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002078 weston_compositor_add_binding(ec, 0, BTN_LEFT,
2079 MODIFIER_SUPER | MODIFIER_ALT,
2080 rotate_binding, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002081 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
2082 switcher_binding, ec);
2083
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002084 /* brightness */
2085 weston_compositor_add_binding(ec, KEY_F9, 0, MODIFIER_CTRL,
2086 backlight_binding, ec);
2087 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0,
2088 backlight_binding, ec);
2089 weston_compositor_add_binding(ec, KEY_F10, 0, MODIFIER_CTRL,
2090 backlight_binding, ec);
2091 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0,
2092 backlight_binding, ec);
2093
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002094 weston_compositor_add_binding(ec, KEY_SPACE, 0, MODIFIER_SUPER,
2095 debug_repaint_binding, ec);
2096
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002097 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002098
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002099 return 0;
2100}