blob: 4725335ad2ffca57fda9be4b149fb880b8b8f279 [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
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295 wl_resource_post_event(&resize->shsurf->resource,
296 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
297 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500298}
299
300static void
Scott Moreau447013d2012-02-18 05:05:29 -0700301resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500302 uint32_t time, int32_t button, int32_t state)
303{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500304 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500305
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500306 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700307 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500308 free(grab);
309 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310}
311
Scott Moreau447013d2012-02-18 05:05:29 -0700312static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500313 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500314 resize_grab_motion,
315 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500316};
317
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400318static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500319weston_surface_resize(struct shell_surface *shsurf,
320 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200321 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500323 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500324
Alex Wu4539b082012-03-01 12:57:46 +0800325 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
326 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500327
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200328 if (edges == 0 || edges > 15 ||
329 (edges & 3) == 3 || (edges & 12) == 12)
330 return 0;
331
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500332 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400333 if (!resize)
334 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335
336 resize->grab.interface = &resize_grab_interface;
337 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200338 resize->width = shsurf->surface->geometry.width;
339 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200340 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400341
Scott Moreau447013d2012-02-18 05:05:29 -0700342 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500343
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200344 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400345
346 return 0;
347}
348
349static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200350shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
351 struct wl_resource *input_resource, uint32_t time,
352 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400353{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500354 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200355 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400356
Alex Wu4539b082012-03-01 12:57:46 +0800357 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
358 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400359
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500360 if (wd->input_device.button_count == 0 ||
361 wd->input_device.grab_time != time ||
362 wd->input_device.pointer_focus != &shsurf->surface->surface)
363 return;
364
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500365 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400366 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500367}
368
Juan Zhao96879df2012-02-07 08:45:41 +0800369static struct weston_output *
370get_default_output(struct weston_compositor *compositor)
371{
372 return container_of(compositor->output_list.next,
373 struct weston_output, link);
374}
375
Alex Wu4539b082012-03-01 12:57:46 +0800376static void
377shell_unset_fullscreen(struct shell_surface *shsurf)
378{
379 /* undo all fullscreen things here */
380 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
381 shsurf->fullscreen.framerate = 0;
382 wl_list_remove(&shsurf->fullscreen.transform.link);
383 wl_list_init(&shsurf->fullscreen.transform.link);
384 weston_surface_destroy(shsurf->fullscreen.black_surface);
385 shsurf->fullscreen.black_surface = NULL;
386 shsurf->fullscreen_output = NULL;
387 shsurf->surface->force_configure = 1;
388 weston_surface_set_position(shsurf->surface,
389 shsurf->saved_x, shsurf->saved_y);
390}
391
Pekka Paalanen98262232011-12-01 10:42:22 +0200392static int
393reset_shell_surface_type(struct shell_surface *surface)
394{
395 switch (surface->type) {
396 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800397 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200398 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800399 case SHELL_SURFACE_MAXIMIZED:
400 surface->output = get_default_output(surface->surface->compositor);
401 weston_surface_set_position(surface->surface,
402 surface->saved_x,
403 surface->saved_y);
404 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200405 case SHELL_SURFACE_PANEL:
406 case SHELL_SURFACE_BACKGROUND:
407 wl_list_remove(&surface->link);
408 wl_list_init(&surface->link);
409 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200410 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200411 case SHELL_SURFACE_LOCK:
412 wl_resource_post_error(&surface->resource,
413 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200414 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200415 return -1;
416 case SHELL_SURFACE_NONE:
417 case SHELL_SURFACE_TOPLEVEL:
418 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500419 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200420 break;
421 }
422
423 surface->type = SHELL_SURFACE_NONE;
424 return 0;
425}
426
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500427static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200428shell_surface_set_toplevel(struct wl_client *client,
429 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400430
431{
Pekka Paalanen98262232011-12-01 10:42:22 +0200432 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400433
Pekka Paalanen98262232011-12-01 10:42:22 +0200434 if (reset_shell_surface_type(surface))
435 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400436
Pekka Paalanen98262232011-12-01 10:42:22 +0200437 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400438}
439
440static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200441shell_surface_set_transient(struct wl_client *client,
442 struct wl_resource *resource,
443 struct wl_resource *parent_resource,
444 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400445{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200446 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500447 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200448 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500449 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400450
Pekka Paalanen98262232011-12-01 10:42:22 +0200451 if (reset_shell_surface_type(shsurf))
452 return;
453
Alex Wu4539b082012-03-01 12:57:46 +0800454 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800455 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200456 weston_surface_set_position(es, pes->geometry.x + x,
457 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400458
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200459 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400460}
461
Juan Zhao96879df2012-02-07 08:45:41 +0800462static struct wl_shell *
463shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200464{
Juan Zhao96879df2012-02-07 08:45:41 +0800465 struct weston_surface *es = shsurf->surface;
466 struct weston_shell *shell = es->compositor->shell;
467
468 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
469}
470
471static int
472get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
473{
474 struct shell_surface *priv;
475 int panel_height = 0;
476
477 if (!output)
478 return 0;
479
480 wl_list_for_each(priv, &wlshell->panels, link) {
481 if (priv->output == output) {
482 panel_height = priv->surface->geometry.height;
483 break;
484 }
485 }
486 return panel_height;
487}
488
489static void
490shell_surface_set_maximized(struct wl_client *client,
491 struct wl_resource *resource,
492 struct wl_resource *output_resource )
493{
494 struct shell_surface *shsurf = resource->data;
495 struct weston_surface *es = shsurf->surface;
496 struct wl_shell *wlshell = NULL;
497 uint32_t edges = 0, panel_height = 0;
498
499 /* get the default output, if the client set it as NULL
500 check whether the ouput is available */
501 if (output_resource)
502 shsurf->output = output_resource->data;
503 else
504 shsurf->output = get_default_output(es->compositor);
505
506 if (reset_shell_surface_type(shsurf))
507 return;
508
509 shsurf->saved_x = es->geometry.x;
510 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800511 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800512
513 wlshell = shell_surface_get_shell(shsurf);
514 panel_height = get_output_panel_height(wlshell, es->output);
515 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
516 wl_resource_post_event(&shsurf->resource,
517 WL_SHELL_SURFACE_CONFIGURE,
518 weston_compositor_get_time(), edges,
519 es->output->current->width,
520 es->output->current->height - panel_height);
521
522 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200523}
524
Alex Wu4539b082012-03-01 12:57:46 +0800525static struct weston_surface *
526create_black_surface(struct weston_compositor *ec,
527 GLfloat x, GLfloat y, int w, int h)
528{
529 struct weston_surface *surface = NULL;
530
531 surface = weston_surface_create(ec);
532 if (surface == NULL) {
533 fprintf(stderr, "no memory\n");
534 return NULL;
535 }
536
537 weston_surface_configure(surface, x, y, w, h);
538 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
539 return surface;
540}
541
542/* Create black surface and append it to the associated fullscreen surface.
543 * Handle size dismatch and positioning according to the method. */
544static void
545shell_configure_fullscreen(struct shell_surface *shsurf)
546{
547 struct weston_output *output = shsurf->fullscreen_output;
548 struct weston_surface *surface = shsurf->surface;
549 struct weston_matrix *matrix;
550 float scale;
551
552 center_on_output(surface, output);
553
554 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500555 shsurf->fullscreen.black_surface =
556 create_black_surface(surface->compositor,
557 output->x, output->y,
558 output->current->width,
559 output->current->height);
560
561 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
562 wl_list_insert(&surface->layer_link,
563 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800564 shsurf->fullscreen.black_surface->output = output;
565
566 switch (shsurf->fullscreen.type) {
567 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
568 break;
569 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
570 matrix = &shsurf->fullscreen.transform.matrix;
571 weston_matrix_init(matrix);
572 scale = (float)output->current->width/(float)surface->geometry.width;
573 weston_matrix_scale(matrix, scale, scale, 1);
574 wl_list_remove(&shsurf->fullscreen.transform.link);
575 wl_list_insert(surface->geometry.transformation_list.prev,
576 &shsurf->fullscreen.transform.link);
577 weston_surface_set_position(surface, output->x, output->y);
578 break;
579 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
580 break;
581 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
582 break;
583 default:
584 break;
585 }
586}
587
588/* make the fullscreen and black surface at the top */
589static void
590shell_stack_fullscreen(struct shell_surface *shsurf)
591{
592 struct weston_surface *surface = shsurf->surface;
593 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800594
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500595 wl_list_remove(&surface->layer_link);
596 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800597
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500598 wl_list_insert(&shell->fullscreen_layer.surface_list,
599 &surface->layer_link);
600 wl_list_insert(&surface->layer_link,
601 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800602
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500603 weston_surface_damage(surface);
604 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800605}
606
607static void
608shell_map_fullscreen(struct shell_surface *shsurf)
609{
610 shell_configure_fullscreen(shsurf);
611 shell_stack_fullscreen(shsurf);
612}
613
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400614static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200615shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500616 struct wl_resource *resource,
617 uint32_t method,
618 uint32_t framerate,
619 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400620{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200621 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500622 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800623
624 if (output_resource)
625 shsurf->output = output_resource->data;
626 else
627 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400628
Pekka Paalanen98262232011-12-01 10:42:22 +0200629 if (reset_shell_surface_type(shsurf))
630 return;
631
Alex Wu4539b082012-03-01 12:57:46 +0800632 shsurf->fullscreen_output = shsurf->output;
633 shsurf->fullscreen.type = method;
634 shsurf->fullscreen.framerate = framerate;
635 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400636
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200637 shsurf->saved_x = es->geometry.x;
638 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800639 shsurf->saved_position_valid = true;
640
641 if (es->output)
642 shsurf->surface->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500643
644 wl_resource_post_event(resource,
645 WL_SHELL_SURFACE_CONFIGURE,
646 weston_compositor_get_time(), 0,
Alex Wu88277d12012-02-22 14:50:46 +0800647 shsurf->output->current->width,
648 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400649}
650
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500651static void
Scott Moreau447013d2012-02-18 05:05:29 -0700652popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500653 struct wl_surface *surface, int32_t x, int32_t y)
654{
655 struct wl_input_device *device = grab->input_device;
656 struct shell_surface *priv =
657 container_of(grab, struct shell_surface, popup.grab);
658 struct wl_client *client = priv->surface->surface.resource.client;
659
Pekka Paalanencb108432012-01-19 16:25:40 +0200660 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200661 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500662 grab->focus = surface;
663 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200664 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500665 grab->focus = NULL;
666 }
667}
668
669static void
Scott Moreau447013d2012-02-18 05:05:29 -0700670popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200671 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500672{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500673 struct wl_resource *resource;
674
675 resource = grab->input_device->pointer_focus_resource;
676 if (resource)
677 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200678 time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500679}
680
681static void
Scott Moreau447013d2012-02-18 05:05:29 -0700682popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500683 uint32_t time, int32_t button, int32_t state)
684{
685 struct wl_resource *resource;
686 struct shell_surface *shsurf =
687 container_of(grab, struct shell_surface, popup.grab);
688
689 resource = grab->input_device->pointer_focus_resource;
690 if (resource) {
691 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
692 time, button, state);
693 } else if (state == 0 &&
694 (shsurf->popup.initial_up ||
695 time - shsurf->popup.time > 500)) {
696 wl_resource_post_event(&shsurf->resource,
697 WL_SHELL_SURFACE_POPUP_DONE);
Scott Moreau447013d2012-02-18 05:05:29 -0700698 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500699 shsurf->popup.grab.input_device = NULL;
700 }
701
702 if (state == 0)
703 shsurf->popup.initial_up = 1;
704}
705
Scott Moreau447013d2012-02-18 05:05:29 -0700706static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500707 popup_grab_focus,
708 popup_grab_motion,
709 popup_grab_button,
710};
711
712static void
713shell_map_popup(struct shell_surface *shsurf, uint32_t time)
714{
715 struct wl_input_device *device;
716 struct weston_surface *es = shsurf->surface;
717 struct weston_surface *parent = shsurf->parent->surface;
718
719 es->output = parent->output;
720
721 shsurf->popup.grab.interface = &popup_grab_interface;
722 device = es->compositor->input_device;
723
Pekka Paalanen938269a2012-02-07 14:19:01 +0200724 weston_surface_update_transform(parent);
725 if (parent->transform.enabled) {
726 shsurf->popup.parent_transform.matrix =
727 parent->transform.matrix;
728 } else {
729 /* construct x, y translation matrix */
730 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
731 shsurf->popup.parent_transform.matrix.d[12] =
732 parent->geometry.x;
733 shsurf->popup.parent_transform.matrix.d[13] =
734 parent->geometry.y;
735 }
736 wl_list_insert(es->geometry.transformation_list.prev,
737 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200738 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500739
740 shsurf->popup.grab.input_device = device;
741 shsurf->popup.time = device->grab_time;
742 shsurf->popup.initial_up = 0;
743
Scott Moreau447013d2012-02-18 05:05:29 -0700744 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500745 &shsurf->popup.grab, shsurf->popup.time);
746}
747
748static void
749shell_surface_set_popup(struct wl_client *client,
750 struct wl_resource *resource,
751 struct wl_resource *input_device_resource,
752 uint32_t time,
753 struct wl_resource *parent_resource,
754 int32_t x, int32_t y, uint32_t flags)
755{
756 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500757
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500758 shsurf->type = SHELL_SURFACE_POPUP;
759 shsurf->parent = parent_resource->data;
760 shsurf->popup.x = x;
761 shsurf->popup.y = y;
762}
763
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200764static const struct wl_shell_surface_interface shell_surface_implementation = {
765 shell_surface_move,
766 shell_surface_resize,
767 shell_surface_set_toplevel,
768 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500769 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800770 shell_surface_set_popup,
771 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200772};
773
774static void
775destroy_shell_surface(struct wl_resource *resource)
776{
777 struct shell_surface *shsurf = resource->data;
778
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500779 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700780 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500781
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200782 /* in case cleaning up a dead client destroys shell_surface first */
783 if (shsurf->surface)
784 wl_list_remove(&shsurf->surface_destroy_listener.link);
785
786 wl_list_remove(&shsurf->link);
787 free(shsurf);
788}
789
790static void
791shell_handle_surface_destroy(struct wl_listener *listener,
792 struct wl_resource *resource, uint32_t time)
793{
794 struct shell_surface *shsurf = container_of(listener,
795 struct shell_surface,
796 surface_destroy_listener);
797
798 shsurf->surface = NULL;
799 wl_resource_destroy(&shsurf->resource, time);
800}
801
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200802static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500803get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200804{
805 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
806 struct wl_listener *listener;
807
808 /* search the destroy listener list for our callback */
809 wl_list_for_each(listener, lst, link) {
810 if (listener->func == shell_handle_surface_destroy) {
811 return container_of(listener, struct shell_surface,
812 surface_destroy_listener);
813 }
814 }
815
816 return NULL;
817}
818
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200819static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200820shell_get_shell_surface(struct wl_client *client,
821 struct wl_resource *resource,
822 uint32_t id,
823 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200824{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500825 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200826 struct shell_surface *shsurf;
827
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200828 if (get_shell_surface(surface)) {
829 wl_resource_post_error(surface_resource,
830 WL_DISPLAY_ERROR_INVALID_OBJECT,
831 "wl_shell::get_shell_surface already requested");
832 return;
833 }
834
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200835 shsurf = calloc(1, sizeof *shsurf);
836 if (!shsurf) {
837 wl_resource_post_no_memory(resource);
838 return;
839 }
840
841 shsurf->resource.destroy = destroy_shell_surface;
842 shsurf->resource.object.id = id;
843 shsurf->resource.object.interface = &wl_shell_surface_interface;
844 shsurf->resource.object.implementation =
845 (void (**)(void)) &shell_surface_implementation;
846 shsurf->resource.data = shsurf;
847
Alex Wu4539b082012-03-01 12:57:46 +0800848 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200849 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800850 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
851 shsurf->fullscreen.framerate = 0;
852 shsurf->fullscreen.black_surface = NULL;
853 wl_list_init(&shsurf->fullscreen.transform.link);
854
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200855 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
856 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
857 &shsurf->surface_destroy_listener.link);
858
859 /* init link so its safe to always remove it in destroy_shell_surface */
860 wl_list_init(&shsurf->link);
861
Pekka Paalanen460099f2012-01-20 16:48:25 +0200862 /* empty when not in use */
863 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500864 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200865
Pekka Paalanen98262232011-12-01 10:42:22 +0200866 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200867
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200868 wl_client_add_resource(client, &shsurf->resource);
869}
870
871static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200872 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500873};
874
Kristian Høgsberg07937562011-04-12 17:25:42 -0400875static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500876handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200877{
878 proc->pid = 0;
879}
880
881static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200882launch_screensaver(struct wl_shell *shell)
883{
884 if (shell->screensaver.binding)
885 return;
886
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200887 if (!shell->screensaver.path)
888 return;
889
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500890 if (shell->screensaver.process.pid != 0) {
891 fprintf(stderr, "old screensaver still running\n");
892 return;
893 }
894
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500895 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200896 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200897 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200898 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200899}
900
901static void
902terminate_screensaver(struct wl_shell *shell)
903{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200904 if (shell->screensaver.process.pid == 0)
905 return;
906
907 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200908}
909
910static void
911show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
912{
913 struct wl_list *list;
914
915 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500916 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200917 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500918 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200919
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500920 wl_list_remove(&surface->surface->layer_link);
921 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200922 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200923 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200924}
925
926static void
927hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
928{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500929 wl_list_remove(&surface->surface->layer_link);
930 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200931 surface->surface->output = NULL;
932}
933
934static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400935desktop_shell_set_background(struct wl_client *client,
936 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100937 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400938 struct wl_resource *surface_resource)
939{
940 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200941 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500942 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200943 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400944
Pekka Paalanen98262232011-12-01 10:42:22 +0200945 if (reset_shell_surface_type(shsurf))
946 return;
947
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100948 wl_list_for_each(priv, &shell->backgrounds, link) {
949 if (priv->output == output_resource->data) {
950 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500951 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100952 wl_list_remove(&priv->link);
953 break;
954 }
955 }
956
Pekka Paalanen068ae942011-11-28 14:11:15 +0200957 shsurf->type = SHELL_SURFACE_BACKGROUND;
958 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100959
Pekka Paalanen068ae942011-11-28 14:11:15 +0200960 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100961
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200962 weston_surface_set_position(surface, shsurf->output->x,
963 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200964
Kristian Høgsberg75840622011-09-06 13:48:16 -0400965 wl_resource_post_event(resource,
966 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500967 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200968 shsurf->output->current->width,
969 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400970}
971
972static void
973desktop_shell_set_panel(struct wl_client *client,
974 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100975 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400976 struct wl_resource *surface_resource)
977{
978 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200979 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500980 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200981 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400982
Pekka Paalanen98262232011-12-01 10:42:22 +0200983 if (reset_shell_surface_type(shsurf))
984 return;
985
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100986 wl_list_for_each(priv, &shell->panels, link) {
987 if (priv->output == output_resource->data) {
988 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100990 wl_list_remove(&priv->link);
991 break;
992 }
993 }
994
Pekka Paalanen068ae942011-11-28 14:11:15 +0200995 shsurf->type = SHELL_SURFACE_PANEL;
996 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100997
Pekka Paalanen068ae942011-11-28 14:11:15 +0200998 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100999
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001000 weston_surface_set_position(surface, shsurf->output->x,
1001 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001002
Kristian Høgsberg75840622011-09-06 13:48:16 -04001003 wl_resource_post_event(resource,
1004 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001005 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +02001006 shsurf->output->current->width,
1007 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001008}
1009
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001010static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001011handle_lock_surface_destroy(struct wl_listener *listener,
1012 struct wl_resource *resource, uint32_t time)
1013{
1014 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001015 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001016
1017 fprintf(stderr, "lock surface gone\n");
1018 shell->lock_surface = NULL;
1019}
1020
1021static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001022desktop_shell_set_lock_surface(struct wl_client *client,
1023 struct wl_resource *resource,
1024 struct wl_resource *surface_resource)
1025{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001026 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001027 struct shell_surface *surface = surface_resource->data;
1028
1029 if (reset_shell_surface_type(surface))
1030 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001031
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001032 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001033
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001034 if (!shell->locked)
1035 return;
1036
Pekka Paalanen98262232011-12-01 10:42:22 +02001037 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001038
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001039 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1040 wl_list_insert(&surface_resource->destroy_listener_list,
1041 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001042
Pekka Paalanen068ae942011-11-28 14:11:15 +02001043 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001044}
1045
1046static void
1047resume_desktop(struct wl_shell *shell)
1048{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001049 struct shell_surface *tmp;
1050
1051 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1052 hide_screensaver(shell, tmp);
1053
1054 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001055
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001056 wl_list_remove(&shell->lock_layer.link);
1057 wl_list_insert(&shell->compositor->cursor_layer.link,
1058 &shell->fullscreen_layer.link);
1059 wl_list_insert(&shell->fullscreen_layer.link,
1060 &shell->panel_layer.link);
1061 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001062
1063 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001064 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001065 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001066 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001067 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001068}
1069
1070static void
1071desktop_shell_unlock(struct wl_client *client,
1072 struct wl_resource *resource)
1073{
1074 struct wl_shell *shell = resource->data;
1075
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001076 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001077
1078 if (shell->locked)
1079 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001080}
1081
Kristian Høgsberg75840622011-09-06 13:48:16 -04001082static const struct desktop_shell_interface desktop_shell_implementation = {
1083 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001084 desktop_shell_set_panel,
1085 desktop_shell_set_lock_surface,
1086 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001087};
1088
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001089static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001090get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001091{
1092 struct shell_surface *shsurf;
1093
1094 shsurf = get_shell_surface(surface);
1095 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001096 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001097 return shsurf->type;
1098}
1099
Kristian Høgsberg75840622011-09-06 13:48:16 -04001100static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001101move_binding(struct wl_input_device *device, uint32_t time,
1102 uint32_t key, uint32_t button, uint32_t state, void *data)
1103{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001104 struct weston_surface *surface =
1105 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001106
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001107 if (surface == NULL)
1108 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001109
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001110 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001111 case SHELL_SURFACE_PANEL:
1112 case SHELL_SURFACE_BACKGROUND:
1113 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001114 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001115 return;
1116 default:
1117 break;
1118 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001120 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001121}
1122
1123static void
1124resize_binding(struct wl_input_device *device, uint32_t time,
1125 uint32_t key, uint32_t button, uint32_t state, void *data)
1126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127 struct weston_surface *surface =
1128 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001129 uint32_t edges = 0;
1130 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001131 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001132
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001133 if (surface == NULL)
1134 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001135
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001136 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001137 if (!shsurf)
1138 return;
1139
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001140 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001141 case SHELL_SURFACE_PANEL:
1142 case SHELL_SURFACE_BACKGROUND:
1143 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001144 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001145 return;
1146 default:
1147 break;
1148 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001149
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001150 weston_surface_from_global(surface,
1151 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001152
Pekka Paalanen60921e52012-01-25 15:55:43 +02001153 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001154 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001155 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001156 edges |= 0;
1157 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001158 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001159
Pekka Paalanen60921e52012-01-25 15:55:43 +02001160 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001162 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001163 edges |= 0;
1164 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001165 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001166
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001167 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001169}
1170
1171static void
Scott Moreauc6d7f602012-02-23 22:28:37 -07001172zoom_grab_key(struct wl_keyboard_grab *grab,
1173 uint32_t time, uint32_t key, int32_t state)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001174{
Scott Moreauc6d7f602012-02-23 22:28:37 -07001175 struct wl_input_device *device = grab->input_device;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001176 struct weston_input_device *wd = (struct weston_input_device *) device;
1177 struct weston_compositor *compositor = wd->compositor;
1178 struct weston_output *output;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001179 struct weston_zoom_grab *zoom;
1180
1181 if (!(wd->modifier_state & MODIFIER_SUPER)) {
1182 zoom = container_of(grab, struct weston_zoom_grab, grab);
1183 wl_input_device_end_keyboard_grab(device, time);
1184 free(zoom);
1185 return;
1186 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001187
1188 wl_list_for_each(output, &compositor->output_list, link) {
1189 if (pixman_region32_contains_point(&output->region,
1190 device->x, device->y, NULL)) {
Scott Moreauc6d7f602012-02-23 22:28:37 -07001191 if (state && key == KEY_UP) {
1192 output->zoom.active = 1;
1193 output->zoom.level -= output->zoom.increment;
1194 }
1195 if (state && key == KEY_DOWN)
1196 output->zoom.level += output->zoom.increment;
1197
1198 if (output->zoom.level >= 1.0) {
1199 output->zoom.active = 0;
1200 output->zoom.level = 1.0;
1201 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001202
1203 if (output->zoom.level < output->zoom.increment)
1204 output->zoom.level = output->zoom.increment;
1205
1206 weston_output_update_zoom(output, device->x, device->y);
1207 }
1208 }
1209}
1210
Scott Moreauc6d7f602012-02-23 22:28:37 -07001211static const struct wl_keyboard_grab_interface zoom_grab = {
1212 zoom_grab_key,
1213};
1214
Scott Moreauccbf29d2012-02-22 14:21:41 -07001215static void
Scott Moreauc6d7f602012-02-23 22:28:37 -07001216zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreauccbf29d2012-02-22 14:21:41 -07001217 uint32_t key, uint32_t button, uint32_t state, void *data)
1218{
1219 struct weston_input_device *wd = (struct weston_input_device *) device;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001220 struct weston_zoom_grab *zoom;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001221
Scott Moreauc6d7f602012-02-23 22:28:37 -07001222 zoom = malloc(sizeof *zoom);
1223 if (!zoom)
1224 return;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001225
Scott Moreauc6d7f602012-02-23 22:28:37 -07001226 zoom->grab.interface = &zoom_grab;
1227
1228 wl_input_device_start_keyboard_grab(&wd->input_device, &zoom->grab, time);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001229}
1230
1231static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001232terminate_binding(struct wl_input_device *device, uint32_t time,
1233 uint32_t key, uint32_t button, uint32_t state, void *data)
1234{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001236
1237 if (state)
1238 wl_display_terminate(compositor->wl_display);
1239}
1240
1241static void
Scott Moreau447013d2012-02-18 05:05:29 -07001242rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001243 uint32_t time, int32_t x, int32_t y)
1244{
1245 struct rotate_grab *rotate =
1246 container_of(grab, struct rotate_grab, grab);
1247 struct wl_input_device *device = grab->input_device;
1248 struct shell_surface *surface = rotate->surface;
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001249 GLfloat cx = 0.5f * surface->surface->geometry.width;
1250 GLfloat cy = 0.5f * surface->surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001251 GLfloat dx, dy;
1252 GLfloat r;
1253
1254 dx = device->x - rotate->center.x;
1255 dy = device->y - rotate->center.y;
1256 r = sqrtf(dx * dx + dy * dy);
1257
1258 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001259 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001260
1261 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001262 struct weston_matrix *matrix =
1263 &surface->rotation.transform.matrix;
1264
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001265 weston_matrix_init(&rotate->rotation);
1266 rotate->rotation.d[0] = dx / r;
1267 rotate->rotation.d[4] = -dy / r;
1268 rotate->rotation.d[1] = -rotate->rotation.d[4];
1269 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001270
1271 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001272 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001273 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1274 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001275 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001276
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001277 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001278 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001279 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001280 } else {
1281 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001282 weston_matrix_init(&surface->rotation.rotation);
1283 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001284 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001285
1286 /* Repaint implies weston_surface_update_transform(), which
1287 * lazily applies the damage due to rotation update.
1288 */
1289 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001290}
1291
1292static void
Scott Moreau447013d2012-02-18 05:05:29 -07001293rotate_grab_button(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001294 uint32_t time, int32_t button, int32_t state)
1295{
1296 struct rotate_grab *rotate =
1297 container_of(grab, struct rotate_grab, grab);
1298 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001299 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001300
1301 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001302 weston_matrix_multiply(&surface->rotation.rotation,
1303 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001304 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001305 free(rotate);
1306 }
1307}
1308
Scott Moreau447013d2012-02-18 05:05:29 -07001309static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001310 noop_grab_focus,
1311 rotate_grab_motion,
1312 rotate_grab_button,
1313};
1314
1315static void
1316rotate_binding(struct wl_input_device *device, uint32_t time,
1317 uint32_t key, uint32_t button, uint32_t state, void *data)
1318{
1319 struct weston_surface *base_surface =
1320 (struct weston_surface *) device->pointer_focus;
1321 struct shell_surface *surface;
1322 struct rotate_grab *rotate;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001323 GLfloat dx, dy;
1324 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001325
1326 if (base_surface == NULL)
1327 return;
1328
1329 surface = get_shell_surface(base_surface);
1330 if (!surface)
1331 return;
1332
1333 switch (surface->type) {
1334 case SHELL_SURFACE_PANEL:
1335 case SHELL_SURFACE_BACKGROUND:
1336 case SHELL_SURFACE_FULLSCREEN:
1337 case SHELL_SURFACE_SCREENSAVER:
1338 return;
1339 default:
1340 break;
1341 }
1342
Pekka Paalanen460099f2012-01-20 16:48:25 +02001343 rotate = malloc(sizeof *rotate);
1344 if (!rotate)
1345 return;
1346
1347 rotate->grab.interface = &rotate_grab_interface;
1348 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001349
1350 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001351 surface->surface->geometry.width / 2,
1352 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001353 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001354
Scott Moreau447013d2012-02-18 05:05:29 -07001355 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001356
1357 dx = device->x - rotate->center.x;
1358 dy = device->y - rotate->center.y;
1359 r = sqrtf(dx * dx + dy * dy);
1360 if (r > 20.0f) {
1361 struct weston_matrix inverse;
1362
1363 weston_matrix_init(&inverse);
1364 inverse.d[0] = dx / r;
1365 inverse.d[4] = dy / r;
1366 inverse.d[1] = -inverse.d[4];
1367 inverse.d[5] = inverse.d[0];
1368 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1369 } else {
1370 weston_matrix_init(&surface->rotation.rotation);
1371 weston_matrix_init(&rotate->rotation);
1372 }
1373
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001374 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001375}
1376
1377static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001378activate(struct weston_shell *base, struct weston_surface *es,
1379 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001380{
1381 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001382 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001383
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001384 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001385
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001386 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001387 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001388
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001389 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001390 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001391 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001392 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001393 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001394
Pekka Paalanen77346a62011-11-30 16:26:35 +02001395 case SHELL_SURFACE_SCREENSAVER:
1396 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001397 if (shell->lock_surface)
1398 weston_surface_restack(es,
1399 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001400 break;
Alex Wu4539b082012-03-01 12:57:46 +08001401 case SHELL_SURFACE_FULLSCREEN:
1402 /* should on top of panels */
1403 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001404 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001405 weston_surface_restack(es,
1406 &shell->toplevel_layer.surface_list);
1407 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001408 }
1409}
1410
1411static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001412click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001413 uint32_t time, uint32_t key,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001414 uint32_t button, uint32_t state, void *data)
1415{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 struct weston_input_device *wd = (struct weston_input_device *) device;
1417 struct weston_compositor *compositor = data;
1418 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001419 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001420
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001421 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu4539b082012-03-01 12:57:46 +08001422 upper = container_of(focus->link.prev, struct weston_surface, link);
1423 if (focus->link.prev != &compositor->surface_list &&
1424 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1425 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1426 shell_stack_fullscreen(get_shell_surface(upper));
1427 focus = upper;
1428 }
1429
Scott Moreau447013d2012-02-18 05:05:29 -07001430 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001431 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001432}
1433
1434static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001436{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001437 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001439 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001440 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001441 uint32_t time;
1442
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001443 if (shell->locked) {
1444 wl_list_for_each(output, &shell->compositor->output_list, link)
1445 /* TODO: find a way to jump to other DPMS levels */
1446 if (output->set_dpms)
1447 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001448 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001449 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001450
1451 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001452
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001453 /* Hide all surfaces by removing the fullscreen, panel and
1454 * toplevel layers. This way nothing else can show or receive
1455 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001456
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001457 wl_list_remove(&shell->panel_layer.link);
1458 wl_list_remove(&shell->toplevel_layer.link);
1459 wl_list_remove(&shell->fullscreen_layer.link);
1460 wl_list_insert(&shell->compositor->cursor_layer.link,
1461 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001462
Pekka Paalanen77346a62011-11-30 16:26:35 +02001463 launch_screensaver(shell);
1464
1465 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1466 show_screensaver(shell, shsurf);
1467
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001468 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001469 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 weston_compositor_wake(shell->compositor);
1471 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001472 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001473
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001474 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001475 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001476
1477 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001478 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001479 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1480 wl_input_device_set_keyboard_focus(&device->input_device,
1481 NULL, time);
1482 }
1483
1484 /* TODO: disable bindings that should not work while locked. */
1485
1486 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001487}
1488
1489static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001491{
1492 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1493
Pekka Paalanend81c2162011-11-16 13:47:34 +02001494 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001496 return;
1497 }
1498
1499 /* If desktop-shell client has gone away, unlock immediately. */
1500 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001501 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001502 return;
1503 }
1504
1505 if (shell->prepare_event_sent)
1506 return;
1507
1508 wl_resource_post_event(shell->child.desktop_shell,
1509 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1510 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001511}
1512
1513static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001515{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001516 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001517 GLfloat x = (mode->width - surface->geometry.width) / 2;
1518 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001519
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001520 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001521}
1522
1523static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001524map(struct weston_shell *base, struct weston_surface *surface,
1525 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001526{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001527 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001528 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001529 struct shell_surface *shsurf;
1530 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Juan Zhao96879df2012-02-07 08:45:41 +08001531 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001532
Pekka Paalanen77346a62011-11-30 16:26:35 +02001533 shsurf = get_shell_surface(surface);
1534 if (shsurf)
1535 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001536
Pekka Paalanen60921e52012-01-25 15:55:43 +02001537 surface->geometry.width = width;
1538 surface->geometry.height = height;
1539 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001540
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001541 weston_compositor_update_drag_surfaces(compositor);
1542
Pekka Paalanen77346a62011-11-30 16:26:35 +02001543 /* initial positioning, see also configure() */
1544 switch (surface_type) {
1545 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001546 weston_surface_set_position(surface, 10 + random() % 400,
1547 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001548 break;
1549 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001550 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001551 break;
Alex Wu4539b082012-03-01 12:57:46 +08001552 case SHELL_SURFACE_FULLSCREEN:
1553 shell_map_fullscreen(shsurf);
1554 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001555 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001556 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001557 panel_height = get_output_panel_height(shell,surface->output);
1558 weston_surface_set_position(surface, surface->output->x,
1559 surface->output->y + panel_height);
1560 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001561 case SHELL_SURFACE_LOCK:
1562 center_on_output(surface, get_default_output(compositor));
1563 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001564 case SHELL_SURFACE_POPUP:
1565 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001566 case SHELL_SURFACE_NONE:
1567 weston_surface_set_position(surface,
1568 surface->geometry.x + sx,
1569 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001570 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001571 default:
1572 ;
1573 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001574
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001575 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001576 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001577 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001578 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001579 wl_list_insert(&shell->background_layer.surface_list,
1580 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001581 break;
1582 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001583 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001584 wl_list_insert(&shell->panel_layer.surface_list,
1585 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001586 break;
1587 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001588 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001589 wl_list_insert(&shell->lock_layer.surface_list,
1590 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001592 break;
1593 case SHELL_SURFACE_SCREENSAVER:
1594 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001595 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001596 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001597 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001598 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001599 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001600 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001601 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001602 break;
Alex Wu4539b082012-03-01 12:57:46 +08001603 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001604 case SHELL_SURFACE_NONE:
1605 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001606 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001607 wl_list_insert(&shell->toplevel_layer.surface_list,
1608 &surface->layer_link);
1609 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001610 }
1611
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001612 weston_surface_assign_output(surface);
1613 weston_compositor_repick(compositor);
1614 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1615 surface->output = shsurf->output;
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001616
Juan Zhao7bb92f02011-12-15 11:31:51 -05001617 switch (surface_type) {
1618 case SHELL_SURFACE_TOPLEVEL:
1619 case SHELL_SURFACE_TRANSIENT:
1620 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001621 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001622 if (!shell->locked)
1623 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001624 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001625 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001626 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001627 break;
1628 default:
1629 break;
1630 }
1631
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001632 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001633 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001634}
1635
1636static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001637configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001638 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001639{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001640 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001641 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001642 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001643 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001644
Pekka Paalanen77346a62011-11-30 16:26:35 +02001645 shsurf = get_shell_surface(surface);
1646 if (shsurf)
1647 surface_type = shsurf->type;
1648
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001649 surface->geometry.x = x;
1650 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001651 surface->geometry.width = width;
1652 surface->geometry.height = height;
1653 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001654
1655 switch (surface_type) {
1656 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001657 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001658 break;
Alex Wu4539b082012-03-01 12:57:46 +08001659 case SHELL_SURFACE_FULLSCREEN:
1660 shell_configure_fullscreen(shsurf);
1661 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1662 shell_stack_fullscreen(shsurf);
1663 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001664 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001665 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001666 surface->geometry.x = surface->output->x;
1667 surface->geometry.y = surface->output->y +
1668 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001669 break;
Alex Wu4539b082012-03-01 12:57:46 +08001670 case SHELL_SURFACE_TOPLEVEL:
1671 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001672 default:
1673 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001674 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001675
Alex Wu4539b082012-03-01 12:57:46 +08001676 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001677 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001678 weston_surface_assign_output(surface);
Alex Wu4539b082012-03-01 12:57:46 +08001679 weston_compositor_repick(surface->compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001680
1681 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1682 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001683 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1684 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001685 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001686}
1687
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001688static int launch_desktop_shell_process(struct wl_shell *shell);
1689
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001690static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001691desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001692{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001693 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001694 struct wl_shell *shell =
1695 container_of(process, struct wl_shell, child.process);
1696
1697 shell->child.process.pid = 0;
1698 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001699
1700 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1701 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001702 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001703 shell->child.deathstamp = time;
1704 shell->child.deathcount = 0;
1705 }
1706
1707 shell->child.deathcount++;
1708 if (shell->child.deathcount > 5) {
1709 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1710 return;
1711 }
1712
1713 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1714 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001715}
1716
1717static int
1718launch_desktop_shell_process(struct wl_shell *shell)
1719{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001720 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001721
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001722 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001723 &shell->child.process,
1724 shell_exe,
1725 desktop_shell_sigchld);
1726
1727 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001728 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001729 return 0;
1730}
1731
1732static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001733bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1734{
1735 struct wl_shell *shell = data;
1736
1737 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001738 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001739}
1740
Kristian Høgsberg75840622011-09-06 13:48:16 -04001741static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001742unbind_desktop_shell(struct wl_resource *resource)
1743{
1744 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001745
1746 if (shell->locked)
1747 resume_desktop(shell);
1748
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001749 shell->child.desktop_shell = NULL;
1750 shell->prepare_event_sent = false;
1751 free(resource);
1752}
1753
1754static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001755bind_desktop_shell(struct wl_client *client,
1756 void *data, uint32_t version, uint32_t id)
1757{
1758 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001759 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001760
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001761 resource = wl_client_add_object(client, &desktop_shell_interface,
1762 &desktop_shell_implementation,
1763 id, shell);
1764
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001765 if (client == shell->child.client) {
1766 resource->destroy = unbind_desktop_shell;
1767 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001768 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001769 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001770
1771 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1772 "permission to bind desktop_shell denied");
1773 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001774}
1775
Pekka Paalanen6e168112011-11-24 11:34:05 +02001776static void
1777screensaver_set_surface(struct wl_client *client,
1778 struct wl_resource *resource,
1779 struct wl_resource *shell_surface_resource,
1780 struct wl_resource *output_resource)
1781{
1782 struct wl_shell *shell = resource->data;
1783 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001784 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001785
Pekka Paalanen98262232011-12-01 10:42:22 +02001786 if (reset_shell_surface_type(surface))
1787 return;
1788
Pekka Paalanen77346a62011-11-30 16:26:35 +02001789 surface->type = SHELL_SURFACE_SCREENSAVER;
1790
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001791 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001792 surface->output = output;
1793 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001794}
1795
1796static const struct screensaver_interface screensaver_implementation = {
1797 screensaver_set_surface
1798};
1799
1800static void
1801unbind_screensaver(struct wl_resource *resource)
1802{
1803 struct wl_shell *shell = resource->data;
1804
Pekka Paalanen77346a62011-11-30 16:26:35 +02001805 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001806 free(resource);
1807}
1808
1809static void
1810bind_screensaver(struct wl_client *client,
1811 void *data, uint32_t version, uint32_t id)
1812{
1813 struct wl_shell *shell = data;
1814 struct wl_resource *resource;
1815
1816 resource = wl_client_add_object(client, &screensaver_interface,
1817 &screensaver_implementation,
1818 id, shell);
1819
Pekka Paalanen77346a62011-11-30 16:26:35 +02001820 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001821 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001822 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001823 return;
1824 }
1825
1826 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1827 "interface object already bound");
1828 wl_resource_destroy(resource, 0);
1829}
1830
Kristian Høgsberg07045392012-02-19 18:52:44 -05001831struct switcher {
1832 struct weston_compositor *compositor;
1833 struct weston_surface *current;
1834 struct wl_listener listener;
1835 struct wl_keyboard_grab grab;
1836};
1837
1838static void
1839switcher_next(struct switcher *switcher)
1840{
1841 struct weston_compositor *compositor = switcher->compositor;
1842 struct weston_surface *surface;
1843 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1844
1845 wl_list_for_each(surface, &compositor->surface_list, link) {
1846 /* Workaround for cursor surfaces. */
1847 if (surface->surface.resource.destroy_listener_list.next == NULL)
1848 continue;
1849
1850 switch (get_shell_surface_type(surface)) {
1851 case SHELL_SURFACE_TOPLEVEL:
1852 case SHELL_SURFACE_FULLSCREEN:
1853 case SHELL_SURFACE_MAXIMIZED:
1854 if (first == NULL)
1855 first = surface;
1856 if (prev == switcher->current)
1857 next = surface;
1858 prev = surface;
1859 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001860 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001861 weston_surface_damage(surface);
1862 break;
1863 default:
1864 break;
1865 }
1866 }
1867
1868 if (next == NULL)
1869 next = first;
1870
1871 wl_list_remove(&switcher->listener.link);
1872 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1873 &switcher->listener.link);
1874
1875 switcher->current = next;
1876 next->alpha = 255;
1877}
1878
1879static void
1880switcher_handle_surface_destroy(struct wl_listener *listener,
1881 struct wl_resource *resource, uint32_t time)
1882{
1883 struct switcher *switcher =
1884 container_of(listener, struct switcher, listener);
1885
1886 switcher_next(switcher);
1887}
1888
1889static void
1890switcher_destroy(struct switcher *switcher, uint32_t time)
1891{
1892 struct weston_compositor *compositor = switcher->compositor;
1893 struct weston_surface *surface;
1894 struct weston_input_device *device =
1895 (struct weston_input_device *) switcher->grab.input_device;
1896
1897 wl_list_for_each(surface, &compositor->surface_list, link) {
1898 surface->alpha = 255;
1899 weston_surface_damage(surface);
1900 }
1901
1902 activate(compositor->shell, switcher->current, device, time);
1903 wl_list_remove(&switcher->listener.link);
1904 wl_input_device_end_keyboard_grab(&device->input_device, time);
1905 free(switcher);
1906}
1907
1908static void
1909switcher_key(struct wl_keyboard_grab *grab,
1910 uint32_t time, uint32_t key, int32_t state)
1911{
1912 struct switcher *switcher = container_of(grab, struct switcher, grab);
1913 struct weston_input_device *device =
1914 (struct weston_input_device *) grab->input_device;
1915
1916 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1917 switcher_destroy(switcher, time);
1918 } else if (key == KEY_TAB && state) {
1919 switcher_next(switcher);
1920 }
1921};
1922
1923static const struct wl_keyboard_grab_interface switcher_grab = {
1924 switcher_key
1925};
1926
1927static void
1928switcher_binding(struct wl_input_device *device, uint32_t time,
1929 uint32_t key, uint32_t button,
1930 uint32_t state, void *data)
1931{
1932 struct weston_compositor *compositor = data;
1933 struct switcher *switcher;
1934
1935 switcher = malloc(sizeof *switcher);
1936 switcher->compositor = compositor;
1937 switcher->current = NULL;
1938 switcher->listener.func = switcher_handle_surface_destroy;
1939 wl_list_init(&switcher->listener.link);
1940
1941 switcher->grab.interface = &switcher_grab;
1942 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1943 switcher_next(switcher);
1944}
1945
Pekka Paalanen3c647232011-12-22 13:43:43 +02001946static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001947backlight_binding(struct wl_input_device *device, uint32_t time,
1948 uint32_t key, uint32_t button, uint32_t state, void *data)
1949{
1950 struct weston_compositor *compositor = data;
1951 struct weston_output *output;
1952
1953 /* TODO: we're limiting to simple use cases, where we assume just
1954 * control on the primary display. We'd have to extend later if we
1955 * ever get support for setting backlights on random desktop LCD
1956 * panels though */
1957 output = get_default_output(compositor);
1958 if (!output)
1959 return;
1960
1961 if (!output->set_backlight)
1962 return;
1963
1964 if ((key == KEY_F9 || key == KEY_BRIGHTNESSDOWN) &&
1965 output->backlight_current > 1)
1966 output->backlight_current--;
1967 else if ((key == KEY_F10 || key == KEY_BRIGHTNESSUP) &&
1968 output->backlight_current < 10)
1969 output->backlight_current++;
1970
1971 output->set_backlight(output, output->backlight_current);
1972}
1973
1974static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05001975debug_repaint_binding(struct wl_input_device *device, uint32_t time,
1976 uint32_t key, uint32_t button, uint32_t state, void *data)
1977{
1978 struct weston_compositor *compositor = data;
1979 struct wl_shell *shell =
1980 container_of(compositor->shell, struct wl_shell, shell);
1981 struct weston_surface *surface;
1982
1983 if (shell->debug_repaint_surface) {
1984 weston_surface_destroy(shell->debug_repaint_surface);
1985 shell->debug_repaint_surface = NULL;
1986 } else {
1987 surface = weston_surface_create(compositor);
1988 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
1989 weston_surface_configure(surface, 0, 0, 8192, 8192);
1990 wl_list_insert(&compositor->fade_layer.surface_list,
1991 &surface->layer_link);
1992 weston_surface_assign_output(surface);
1993 pixman_region32_init(&surface->input);
1994
1995 /* Here's the dirty little trick that makes the
1996 * repaint debugging work: we force an
1997 * update_transform first to update dependent state
1998 * and clear the geometry.dirty bit. Then we clear
1999 * the surface damage so it only gets repainted
2000 * piecewise as we repaint other things. */
2001
2002 weston_surface_update_transform(surface);
2003 pixman_region32_fini(&surface->damage);
2004 pixman_region32_init(&surface->damage);
2005 shell->debug_repaint_surface = surface;
2006 }
2007}
2008
2009static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002010shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002011{
2012 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2013
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002014 if (shell->child.client)
2015 wl_client_destroy(shell->child.client);
2016
Pekka Paalanen3c647232011-12-22 13:43:43 +02002017 free(shell->screensaver.path);
2018 free(shell);
2019}
2020
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002021int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002023
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002024WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002025shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002026{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002027 struct wl_shell *shell;
2028
2029 shell = malloc(sizeof *shell);
2030 if (shell == NULL)
2031 return -1;
2032
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002033 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002034 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002035 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002036 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002037 shell->shell.map = map;
2038 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002039 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002040
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002041 wl_list_init(&shell->backgrounds);
2042 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002043 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002044
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002045 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2046 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2047 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2048 weston_layer_init(&shell->background_layer,
2049 &shell->toplevel_layer.link);
2050 wl_list_init(&shell->lock_layer.surface_list);
2051
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002052 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002053
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002054 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2055 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002056 return -1;
2057
Kristian Høgsberg75840622011-09-06 13:48:16 -04002058 if (wl_display_add_global(ec->wl_display,
2059 &desktop_shell_interface,
2060 shell, bind_desktop_shell) == NULL)
2061 return -1;
2062
Pekka Paalanen6e168112011-11-24 11:34:05 +02002063 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2064 shell, bind_screensaver) == NULL)
2065 return -1;
2066
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002067 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002068 if (launch_desktop_shell_process(shell) != 0)
2069 return -1;
2070
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002071 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002072 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002073 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002074 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002075 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002076 MODIFIER_CTRL | MODIFIER_ALT,
2077 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002078 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002079 click_to_activate_binding, ec);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002080 weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07002081 zoom_binding, shell);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002082 weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07002083 zoom_binding, shell);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002084 weston_compositor_add_binding(ec, 0, BTN_LEFT,
2085 MODIFIER_SUPER | MODIFIER_ALT,
2086 rotate_binding, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002087 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
2088 switcher_binding, ec);
2089
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002090 /* brightness */
2091 weston_compositor_add_binding(ec, KEY_F9, 0, MODIFIER_CTRL,
2092 backlight_binding, ec);
2093 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0,
2094 backlight_binding, ec);
2095 weston_compositor_add_binding(ec, KEY_F10, 0, MODIFIER_CTRL,
2096 backlight_binding, ec);
2097 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0,
2098 backlight_binding, ec);
2099
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002100 weston_compositor_add_binding(ec, KEY_SPACE, 0, MODIFIER_SUPER,
2101 debug_repaint_binding, ec);
2102
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002103 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002104
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002105 return 0;
2106}