blob: accc1c5689de2154e609347a403206bda30f13f5 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
43 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
45 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020048 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020049
50 unsigned deathcount;
51 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020053
54 bool locked;
55 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020056
Pekka Paalanen068ae942011-11-28 14:11:15 +020057 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050058 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020059 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060
61 struct wl_list backgrounds;
62 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020063
Pekka Paalanen77346a62011-11-30 16:26:35 +020064 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020065 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020066 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020067 struct wl_resource *binding;
68 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020070 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040071};
72
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050073enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020074 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050075
Pekka Paalanen57da4a82011-11-23 16:42:16 +020076 SHELL_SURFACE_PANEL,
77 SHELL_SURFACE_BACKGROUND,
78 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020079 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080
81 SHELL_SURFACE_TOPLEVEL,
82 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050083 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080084 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050085 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086};
87
Pekka Paalanen56cdea92011-11-23 16:14:12 +020088struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020089 struct wl_resource resource;
90
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020092 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020094
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050095 enum shell_surface_type type;
96 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +080097 bool saved_position_valid;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010098
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050099 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200100 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500101 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200102 } rotation;
103
104 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700105 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500106 uint32_t time;
107 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200108 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500109 int32_t initial_up;
110 } popup;
111
Alex Wu4539b082012-03-01 12:57:46 +0800112 struct {
113 enum wl_shell_surface_fullscreen_method type;
114 struct weston_transform transform; /* matrix from x, y */
115 uint32_t framerate;
116 struct weston_surface *black_surface;
117 } fullscreen;
118
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500119 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500120 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100121 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200122};
123
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124struct weston_move_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700125 struct wl_pointer_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500126 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500127 int32_t dx, dy;
128};
129
Scott Moreauc6d7f602012-02-23 22:28:37 -0700130struct weston_zoom_grab {
131 struct wl_keyboard_grab grab;
132};
133
Pekka Paalanen460099f2012-01-20 16:48:25 +0200134struct rotate_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700135 struct wl_pointer_grab grab;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500137 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200138 struct {
139 int32_t x;
140 int32_t y;
141 } center;
142};
143
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500144static void
Alex Wu4539b082012-03-01 12:57:46 +0800145center_on_output(struct weston_surface *surface,
146 struct weston_output *output);
147
148static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200149shell_configuration(struct wl_shell *shell)
150{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200151 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200152 char *path = NULL;
153 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200154
155 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200156 { "path", CONFIG_KEY_STRING, &path },
157 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200158 };
159
160 struct config_section cs[] = {
161 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
162 };
163
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500164 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500165 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200166 free(config_file);
167
Pekka Paalanen7296e792011-12-07 16:22:00 +0200168 shell->screensaver.path = path;
169 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200170}
171
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200172static void
Scott Moreau447013d2012-02-18 05:05:29 -0700173noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500174 struct wl_surface *surface, int32_t x, int32_t y)
175{
176 grab->focus = NULL;
177}
178
179static void
Scott Moreau447013d2012-02-18 05:05:29 -0700180move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500181 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500183 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500184 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500185 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500187 weston_surface_configure(es,
188 device->x + move->dx,
189 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200190 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500191}
192
193static void
Scott Moreau447013d2012-02-18 05:05:29 -0700194move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500195 uint32_t time, int32_t button, int32_t state)
196{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500197 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500199 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700200 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500201 free(grab);
202 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203}
204
Scott Moreau447013d2012-02-18 05:05:29 -0700205static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500206 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207 move_grab_motion,
208 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209};
210
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400211static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500212weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500213 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500215 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216
217 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400218 if (!move)
219 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220
221 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200222 move->dx = es->geometry.x - wd->input_device.grab_x;
223 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500224 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225
Scott Moreau447013d2012-02-18 05:05:29 -0700226 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200228 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400229
230 return 0;
231}
232
233static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200234shell_surface_move(struct wl_client *client, struct wl_resource *resource,
235 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400236{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500237 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200238 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400239
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500240 if (wd->input_device.button_count == 0 ||
241 wd->input_device.grab_time != time ||
242 wd->input_device.pointer_focus != &shsurf->surface->surface)
243 return;
244
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500245 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400246 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500247}
248
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500249struct weston_resize_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700250 struct wl_pointer_grab grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500251 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200252 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200253 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254};
255
256static void
Scott Moreau447013d2012-02-18 05:05:29 -0700257resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258 uint32_t time, int32_t x, int32_t y)
259{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500260 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500262 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200263 int32_t from_x, from_y;
264 int32_t to_x, to_y;
265
266 weston_surface_from_global(resize->shsurf->surface,
267 device->grab_x, device->grab_y,
268 &from_x, &from_y);
269 weston_surface_from_global(resize->shsurf->surface,
270 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500271
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200272 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200273 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200274 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200275 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276 } else {
277 width = resize->width;
278 }
279
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200280 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200281 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200282 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200283 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500284 } else {
285 height = resize->height;
286 }
287
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200288 wl_resource_post_event(&resize->shsurf->resource,
289 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
290 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500291}
292
293static void
Scott Moreau447013d2012-02-18 05:05:29 -0700294resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500295 uint32_t time, int32_t button, int32_t state)
296{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500297 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500298
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500299 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700300 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500301 free(grab);
302 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500303}
304
Scott Moreau447013d2012-02-18 05:05:29 -0700305static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500306 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500307 resize_grab_motion,
308 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500309};
310
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400311static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500312weston_surface_resize(struct shell_surface *shsurf,
313 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200314 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317
Alex Wu4539b082012-03-01 12:57:46 +0800318 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
319 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500320
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200321 if (edges == 0 || edges > 15 ||
322 (edges & 3) == 3 || (edges & 12) == 12)
323 return 0;
324
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500325 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400326 if (!resize)
327 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500328
329 resize->grab.interface = &resize_grab_interface;
330 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200331 resize->width = shsurf->surface->geometry.width;
332 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200333 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400334
Scott Moreau447013d2012-02-18 05:05:29 -0700335 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500336
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200337 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400338
339 return 0;
340}
341
342static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200343shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
344 struct wl_resource *input_resource, uint32_t time,
345 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400346{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500347 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200348 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400349
Alex Wu4539b082012-03-01 12:57:46 +0800350 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
351 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400352
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500353 if (wd->input_device.button_count == 0 ||
354 wd->input_device.grab_time != time ||
355 wd->input_device.pointer_focus != &shsurf->surface->surface)
356 return;
357
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500358 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400359 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500360}
361
Juan Zhao96879df2012-02-07 08:45:41 +0800362static struct weston_output *
363get_default_output(struct weston_compositor *compositor)
364{
365 return container_of(compositor->output_list.next,
366 struct weston_output, link);
367}
368
Alex Wu4539b082012-03-01 12:57:46 +0800369static void
370shell_unset_fullscreen(struct shell_surface *shsurf)
371{
372 /* undo all fullscreen things here */
373 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
374 shsurf->fullscreen.framerate = 0;
375 wl_list_remove(&shsurf->fullscreen.transform.link);
376 wl_list_init(&shsurf->fullscreen.transform.link);
377 weston_surface_destroy(shsurf->fullscreen.black_surface);
378 shsurf->fullscreen.black_surface = NULL;
379 shsurf->fullscreen_output = NULL;
380 shsurf->surface->force_configure = 1;
381 weston_surface_set_position(shsurf->surface,
382 shsurf->saved_x, shsurf->saved_y);
383}
384
Pekka Paalanen98262232011-12-01 10:42:22 +0200385static int
386reset_shell_surface_type(struct shell_surface *surface)
387{
388 switch (surface->type) {
389 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800390 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200391 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800392 case SHELL_SURFACE_MAXIMIZED:
393 surface->output = get_default_output(surface->surface->compositor);
394 weston_surface_set_position(surface->surface,
395 surface->saved_x,
396 surface->saved_y);
397 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200398 case SHELL_SURFACE_PANEL:
399 case SHELL_SURFACE_BACKGROUND:
400 wl_list_remove(&surface->link);
401 wl_list_init(&surface->link);
402 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200403 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200404 case SHELL_SURFACE_LOCK:
405 wl_resource_post_error(&surface->resource,
406 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200407 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200408 return -1;
409 case SHELL_SURFACE_NONE:
410 case SHELL_SURFACE_TOPLEVEL:
411 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500412 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200413 break;
414 }
415
416 surface->type = SHELL_SURFACE_NONE;
417 return 0;
418}
419
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500420static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200421shell_surface_set_toplevel(struct wl_client *client,
422 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400423
424{
Pekka Paalanen98262232011-12-01 10:42:22 +0200425 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400426
Pekka Paalanen98262232011-12-01 10:42:22 +0200427 if (reset_shell_surface_type(surface))
428 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400429
Pekka Paalanen98262232011-12-01 10:42:22 +0200430 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400431}
432
433static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200434shell_surface_set_transient(struct wl_client *client,
435 struct wl_resource *resource,
436 struct wl_resource *parent_resource,
437 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400438{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200439 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500440 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200441 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500442 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400443
Pekka Paalanen98262232011-12-01 10:42:22 +0200444 if (reset_shell_surface_type(shsurf))
445 return;
446
Alex Wu4539b082012-03-01 12:57:46 +0800447 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800448 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200449 weston_surface_set_position(es, pes->geometry.x + x,
450 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400451
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200452 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400453}
454
Juan Zhao96879df2012-02-07 08:45:41 +0800455static struct wl_shell *
456shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200457{
Juan Zhao96879df2012-02-07 08:45:41 +0800458 struct weston_surface *es = shsurf->surface;
459 struct weston_shell *shell = es->compositor->shell;
460
461 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
462}
463
464static int
465get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
466{
467 struct shell_surface *priv;
468 int panel_height = 0;
469
470 if (!output)
471 return 0;
472
473 wl_list_for_each(priv, &wlshell->panels, link) {
474 if (priv->output == output) {
475 panel_height = priv->surface->geometry.height;
476 break;
477 }
478 }
479 return panel_height;
480}
481
482static void
483shell_surface_set_maximized(struct wl_client *client,
484 struct wl_resource *resource,
485 struct wl_resource *output_resource )
486{
487 struct shell_surface *shsurf = resource->data;
488 struct weston_surface *es = shsurf->surface;
489 struct wl_shell *wlshell = NULL;
490 uint32_t edges = 0, panel_height = 0;
491
492 /* get the default output, if the client set it as NULL
493 check whether the ouput is available */
494 if (output_resource)
495 shsurf->output = output_resource->data;
496 else
497 shsurf->output = get_default_output(es->compositor);
498
499 if (reset_shell_surface_type(shsurf))
500 return;
501
502 shsurf->saved_x = es->geometry.x;
503 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800504 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800505
506 wlshell = shell_surface_get_shell(shsurf);
507 panel_height = get_output_panel_height(wlshell, es->output);
508 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
509 wl_resource_post_event(&shsurf->resource,
510 WL_SHELL_SURFACE_CONFIGURE,
511 weston_compositor_get_time(), edges,
512 es->output->current->width,
513 es->output->current->height - panel_height);
514
515 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200516}
517
Alex Wu4539b082012-03-01 12:57:46 +0800518static struct weston_surface *
519create_black_surface(struct weston_compositor *ec,
520 GLfloat x, GLfloat y, int w, int h)
521{
522 struct weston_surface *surface = NULL;
523
524 surface = weston_surface_create(ec);
525 if (surface == NULL) {
526 fprintf(stderr, "no memory\n");
527 return NULL;
528 }
529
530 weston_surface_configure(surface, x, y, w, h);
531 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
532 return surface;
533}
534
535/* Create black surface and append it to the associated fullscreen surface.
536 * Handle size dismatch and positioning according to the method. */
537static void
538shell_configure_fullscreen(struct shell_surface *shsurf)
539{
540 struct weston_output *output = shsurf->fullscreen_output;
541 struct weston_surface *surface = shsurf->surface;
542 struct weston_matrix *matrix;
543 float scale;
544
545 center_on_output(surface, output);
546
547 if (!shsurf->fullscreen.black_surface)
548 shsurf->fullscreen.black_surface = create_black_surface(surface->compositor,
549 output->x, output->y,
550 output->current->width, output->current->height);
551 wl_list_remove(&shsurf->fullscreen.black_surface->link);
552 wl_list_insert(&surface->link, &shsurf->fullscreen.black_surface->link);
553 shsurf->fullscreen.black_surface->output = output;
554
555 switch (shsurf->fullscreen.type) {
556 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
557 break;
558 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
559 matrix = &shsurf->fullscreen.transform.matrix;
560 weston_matrix_init(matrix);
561 scale = (float)output->current->width/(float)surface->geometry.width;
562 weston_matrix_scale(matrix, scale, scale, 1);
563 wl_list_remove(&shsurf->fullscreen.transform.link);
564 wl_list_insert(surface->geometry.transformation_list.prev,
565 &shsurf->fullscreen.transform.link);
566 weston_surface_set_position(surface, output->x, output->y);
567 break;
568 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
569 break;
570 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
571 break;
572 default:
573 break;
574 }
575}
576
577/* make the fullscreen and black surface at the top */
578static void
579shell_stack_fullscreen(struct shell_surface *shsurf)
580{
581 struct weston_surface *surface = shsurf->surface;
582 struct wl_shell *shell = shell_surface_get_shell(shsurf);
583 struct wl_list *list;
584
585 wl_list_remove(&surface->link);
586 wl_list_remove(&shsurf->fullscreen.black_surface->link);
587
588 if (shell->locked) {
589 wl_list_insert(&shell->hidden_surface_list, &surface->link);
590 wl_list_insert(&surface->link, &shsurf->fullscreen.black_surface->link);
591 } else {
592 list = weston_compositor_top(surface->compositor);
593 wl_list_insert(list, &surface->link);
594 wl_list_insert(&surface->link, &shsurf->fullscreen.black_surface->link);
595
596 weston_surface_damage(surface);
597 weston_surface_damage(shsurf->fullscreen.black_surface);
598 }
599}
600
601static void
602shell_map_fullscreen(struct shell_surface *shsurf)
603{
604 shell_configure_fullscreen(shsurf);
605 shell_stack_fullscreen(shsurf);
606}
607
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400608static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200609shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500610 struct wl_resource *resource,
611 uint32_t method,
612 uint32_t framerate,
613 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400614{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200615 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500616 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800617
618 if (output_resource)
619 shsurf->output = output_resource->data;
620 else
621 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400622
Pekka Paalanen98262232011-12-01 10:42:22 +0200623 if (reset_shell_surface_type(shsurf))
624 return;
625
Alex Wu4539b082012-03-01 12:57:46 +0800626 shsurf->fullscreen_output = shsurf->output;
627 shsurf->fullscreen.type = method;
628 shsurf->fullscreen.framerate = framerate;
629 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400630
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200631 shsurf->saved_x = es->geometry.x;
632 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800633 shsurf->saved_position_valid = true;
634
635 if (es->output)
636 shsurf->surface->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500637
638 wl_resource_post_event(resource,
639 WL_SHELL_SURFACE_CONFIGURE,
640 weston_compositor_get_time(), 0,
Alex Wu88277d12012-02-22 14:50:46 +0800641 shsurf->output->current->width,
642 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400643}
644
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500645static void
Scott Moreau447013d2012-02-18 05:05:29 -0700646popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500647 struct wl_surface *surface, int32_t x, int32_t y)
648{
649 struct wl_input_device *device = grab->input_device;
650 struct shell_surface *priv =
651 container_of(grab, struct shell_surface, popup.grab);
652 struct wl_client *client = priv->surface->surface.resource.client;
653
Pekka Paalanencb108432012-01-19 16:25:40 +0200654 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200655 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500656 grab->focus = surface;
657 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200658 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500659 grab->focus = NULL;
660 }
661}
662
663static void
Scott Moreau447013d2012-02-18 05:05:29 -0700664popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200665 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500666{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500667 struct wl_resource *resource;
668
669 resource = grab->input_device->pointer_focus_resource;
670 if (resource)
671 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200672 time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500673}
674
675static void
Scott Moreau447013d2012-02-18 05:05:29 -0700676popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500677 uint32_t time, int32_t button, int32_t state)
678{
679 struct wl_resource *resource;
680 struct shell_surface *shsurf =
681 container_of(grab, struct shell_surface, popup.grab);
682
683 resource = grab->input_device->pointer_focus_resource;
684 if (resource) {
685 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
686 time, button, state);
687 } else if (state == 0 &&
688 (shsurf->popup.initial_up ||
689 time - shsurf->popup.time > 500)) {
690 wl_resource_post_event(&shsurf->resource,
691 WL_SHELL_SURFACE_POPUP_DONE);
Scott Moreau447013d2012-02-18 05:05:29 -0700692 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500693 shsurf->popup.grab.input_device = NULL;
694 }
695
696 if (state == 0)
697 shsurf->popup.initial_up = 1;
698}
699
Scott Moreau447013d2012-02-18 05:05:29 -0700700static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500701 popup_grab_focus,
702 popup_grab_motion,
703 popup_grab_button,
704};
705
706static void
707shell_map_popup(struct shell_surface *shsurf, uint32_t time)
708{
709 struct wl_input_device *device;
710 struct weston_surface *es = shsurf->surface;
711 struct weston_surface *parent = shsurf->parent->surface;
712
713 es->output = parent->output;
714
715 shsurf->popup.grab.interface = &popup_grab_interface;
716 device = es->compositor->input_device;
717
Pekka Paalanen938269a2012-02-07 14:19:01 +0200718 weston_surface_update_transform(parent);
719 if (parent->transform.enabled) {
720 shsurf->popup.parent_transform.matrix =
721 parent->transform.matrix;
722 } else {
723 /* construct x, y translation matrix */
724 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
725 shsurf->popup.parent_transform.matrix.d[12] =
726 parent->geometry.x;
727 shsurf->popup.parent_transform.matrix.d[13] =
728 parent->geometry.y;
729 }
730 wl_list_insert(es->geometry.transformation_list.prev,
731 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200732 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500733
734 shsurf->popup.grab.input_device = device;
735 shsurf->popup.time = device->grab_time;
736 shsurf->popup.initial_up = 0;
737
Scott Moreau447013d2012-02-18 05:05:29 -0700738 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500739 &shsurf->popup.grab, shsurf->popup.time);
740}
741
742static void
743shell_surface_set_popup(struct wl_client *client,
744 struct wl_resource *resource,
745 struct wl_resource *input_device_resource,
746 uint32_t time,
747 struct wl_resource *parent_resource,
748 int32_t x, int32_t y, uint32_t flags)
749{
750 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500751
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500752 shsurf->type = SHELL_SURFACE_POPUP;
753 shsurf->parent = parent_resource->data;
754 shsurf->popup.x = x;
755 shsurf->popup.y = y;
756}
757
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200758static const struct wl_shell_surface_interface shell_surface_implementation = {
759 shell_surface_move,
760 shell_surface_resize,
761 shell_surface_set_toplevel,
762 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500763 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800764 shell_surface_set_popup,
765 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200766};
767
768static void
769destroy_shell_surface(struct wl_resource *resource)
770{
771 struct shell_surface *shsurf = resource->data;
772
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500773 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700774 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500775
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200776 /* in case cleaning up a dead client destroys shell_surface first */
777 if (shsurf->surface)
778 wl_list_remove(&shsurf->surface_destroy_listener.link);
779
780 wl_list_remove(&shsurf->link);
781 free(shsurf);
782}
783
784static void
785shell_handle_surface_destroy(struct wl_listener *listener,
786 struct wl_resource *resource, uint32_t time)
787{
788 struct shell_surface *shsurf = container_of(listener,
789 struct shell_surface,
790 surface_destroy_listener);
791
792 shsurf->surface = NULL;
793 wl_resource_destroy(&shsurf->resource, time);
794}
795
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200796static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500797get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200798{
799 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
800 struct wl_listener *listener;
801
802 /* search the destroy listener list for our callback */
803 wl_list_for_each(listener, lst, link) {
804 if (listener->func == shell_handle_surface_destroy) {
805 return container_of(listener, struct shell_surface,
806 surface_destroy_listener);
807 }
808 }
809
810 return NULL;
811}
812
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200813static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200814shell_get_shell_surface(struct wl_client *client,
815 struct wl_resource *resource,
816 uint32_t id,
817 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200818{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500819 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200820 struct shell_surface *shsurf;
821
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200822 if (get_shell_surface(surface)) {
823 wl_resource_post_error(surface_resource,
824 WL_DISPLAY_ERROR_INVALID_OBJECT,
825 "wl_shell::get_shell_surface already requested");
826 return;
827 }
828
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200829 shsurf = calloc(1, sizeof *shsurf);
830 if (!shsurf) {
831 wl_resource_post_no_memory(resource);
832 return;
833 }
834
835 shsurf->resource.destroy = destroy_shell_surface;
836 shsurf->resource.object.id = id;
837 shsurf->resource.object.interface = &wl_shell_surface_interface;
838 shsurf->resource.object.implementation =
839 (void (**)(void)) &shell_surface_implementation;
840 shsurf->resource.data = shsurf;
841
Alex Wu4539b082012-03-01 12:57:46 +0800842 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200843 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800844 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
845 shsurf->fullscreen.framerate = 0;
846 shsurf->fullscreen.black_surface = NULL;
847 wl_list_init(&shsurf->fullscreen.transform.link);
848
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200849 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
850 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
851 &shsurf->surface_destroy_listener.link);
852
853 /* init link so its safe to always remove it in destroy_shell_surface */
854 wl_list_init(&shsurf->link);
855
Pekka Paalanen460099f2012-01-20 16:48:25 +0200856 /* empty when not in use */
857 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500858 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200859
Pekka Paalanen98262232011-12-01 10:42:22 +0200860 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200861
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200862 wl_client_add_resource(client, &shsurf->resource);
863}
864
865static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200866 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500867};
868
Kristian Høgsberg07937562011-04-12 17:25:42 -0400869static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500870handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200871{
872 proc->pid = 0;
873}
874
875static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200876launch_screensaver(struct wl_shell *shell)
877{
878 if (shell->screensaver.binding)
879 return;
880
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200881 if (!shell->screensaver.path)
882 return;
883
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500884 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200885 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200886 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200887 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200888}
889
890static void
891terminate_screensaver(struct wl_shell *shell)
892{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200893 if (shell->screensaver.process.pid == 0)
894 return;
895
896 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200897}
898
899static void
900show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
901{
902 struct wl_list *list;
903
904 if (shell->lock_surface)
905 list = &shell->lock_surface->surface->link;
906 else
907 list = &shell->compositor->surface_list;
908
909 wl_list_remove(&surface->surface->link);
910 wl_list_insert(list, &surface->surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200911 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200912 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200913}
914
915static void
916hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
917{
918 wl_list_remove(&surface->surface->link);
919 wl_list_init(&surface->surface->link);
920 surface->surface->output = NULL;
921}
922
923static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400924desktop_shell_set_background(struct wl_client *client,
925 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100926 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400927 struct wl_resource *surface_resource)
928{
929 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200930 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500931 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200932 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400933
Pekka Paalanen98262232011-12-01 10:42:22 +0200934 if (reset_shell_surface_type(shsurf))
935 return;
936
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100937 wl_list_for_each(priv, &shell->backgrounds, link) {
938 if (priv->output == output_resource->data) {
939 priv->surface->output = NULL;
940 wl_list_remove(&priv->surface->link);
941 wl_list_remove(&priv->link);
942 break;
943 }
944 }
945
Pekka Paalanen068ae942011-11-28 14:11:15 +0200946 shsurf->type = SHELL_SURFACE_BACKGROUND;
947 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100948
Pekka Paalanen068ae942011-11-28 14:11:15 +0200949 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100950
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200951 weston_surface_set_position(surface, shsurf->output->x,
952 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200953
Kristian Høgsberg75840622011-09-06 13:48:16 -0400954 wl_resource_post_event(resource,
955 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500956 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200957 shsurf->output->current->width,
958 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400959}
960
961static void
962desktop_shell_set_panel(struct wl_client *client,
963 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100964 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400965 struct wl_resource *surface_resource)
966{
967 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200968 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200970 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400971
Pekka Paalanen98262232011-12-01 10:42:22 +0200972 if (reset_shell_surface_type(shsurf))
973 return;
974
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100975 wl_list_for_each(priv, &shell->panels, link) {
976 if (priv->output == output_resource->data) {
977 priv->surface->output = NULL;
978 wl_list_remove(&priv->surface->link);
979 wl_list_remove(&priv->link);
980 break;
981 }
982 }
983
Pekka Paalanen068ae942011-11-28 14:11:15 +0200984 shsurf->type = SHELL_SURFACE_PANEL;
985 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100986
Pekka Paalanen068ae942011-11-28 14:11:15 +0200987 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100988
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200989 weston_surface_set_position(surface, shsurf->output->x,
990 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200991
Kristian Høgsberg75840622011-09-06 13:48:16 -0400992 wl_resource_post_event(resource,
993 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500994 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200995 shsurf->output->current->width,
996 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400997}
998
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200999static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001000handle_lock_surface_destroy(struct wl_listener *listener,
1001 struct wl_resource *resource, uint32_t time)
1002{
1003 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001004 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001005
1006 fprintf(stderr, "lock surface gone\n");
1007 shell->lock_surface = NULL;
1008}
1009
1010static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001011desktop_shell_set_lock_surface(struct wl_client *client,
1012 struct wl_resource *resource,
1013 struct wl_resource *surface_resource)
1014{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001015 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001016 struct shell_surface *surface = surface_resource->data;
1017
1018 if (reset_shell_surface_type(surface))
1019 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001020
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001021 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001022
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001023 if (!shell->locked)
1024 return;
1025
Pekka Paalanen98262232011-12-01 10:42:22 +02001026 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001027
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001028 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1029 wl_list_insert(&surface_resource->destroy_listener_list,
1030 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001031
Pekka Paalanen068ae942011-11-28 14:11:15 +02001032 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001033}
1034
1035static void
1036resume_desktop(struct wl_shell *shell)
1037{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001038 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +02001039 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001040 struct shell_surface *tmp;
1041
1042 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1043 hide_screensaver(shell, tmp);
1044
1045 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001046
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001047 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001048 weston_surface_assign_output(surface);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001049
Pekka Paalanenfe340832011-11-25 16:07:52 +02001050 if (wl_list_empty(&shell->backgrounds)) {
1051 list = &shell->compositor->surface_list;
1052 } else {
1053 struct shell_surface *background;
1054 background = container_of(shell->backgrounds.prev,
1055 struct shell_surface, link);
1056 list = background->surface->link.prev;
1057 }
1058
1059 if (!wl_list_empty(&shell->hidden_surface_list))
1060 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001061 wl_list_init(&shell->hidden_surface_list);
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øgsberga82c4862012-01-26 01:03:58 -05001383 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001384
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001386
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001387 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001388 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001389
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001390 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001391 case SHELL_SURFACE_BACKGROUND:
1392 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001393 wl_list_remove(&es->link);
1394 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001395 break;
1396 case SHELL_SURFACE_PANEL:
1397 /* already put on top */
1398 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001399 case SHELL_SURFACE_SCREENSAVER:
1400 /* always below lock surface */
1401 if (shell->lock_surface) {
1402 wl_list_remove(&es->link);
1403 wl_list_insert(&shell->lock_surface->surface->link,
1404 &es->link);
1405 }
1406 break;
Alex Wu4539b082012-03-01 12:57:46 +08001407 case SHELL_SURFACE_FULLSCREEN:
1408 /* should on top of panels */
1409 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001410 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001411 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001412 list = weston_compositor_top(compositor);
1413
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001414 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001415 struct shell_surface *panel;
1416 wl_list_for_each(panel, &shell->panels, link) {
1417 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001418 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001419 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001420 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001421 }
1422}
1423
1424static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001425click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001426 uint32_t time, uint32_t key,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001427 uint32_t button, uint32_t state, void *data)
1428{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429 struct weston_input_device *wd = (struct weston_input_device *) device;
1430 struct weston_compositor *compositor = data;
1431 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001432 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001433
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001434 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu4539b082012-03-01 12:57:46 +08001435 upper = container_of(focus->link.prev, struct weston_surface, link);
1436 if (focus->link.prev != &compositor->surface_list &&
1437 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1438 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1439 shell_stack_fullscreen(get_shell_surface(upper));
1440 focus = upper;
1441 }
1442
Scott Moreau447013d2012-02-18 05:05:29 -07001443 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001444 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001445}
1446
1447static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001449{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001450 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001451 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452 struct weston_surface *cur;
1453 struct weston_surface *tmp;
1454 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001455 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001456 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001457 uint32_t time;
1458
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001459 if (shell->locked) {
1460 wl_list_for_each(output, &shell->compositor->output_list, link)
1461 /* TODO: find a way to jump to other DPMS levels */
1462 if (output->set_dpms)
1463 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001464 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001465 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001466
1467 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001468
1469 /* Move all surfaces from compositor's list to our hidden list,
1470 * except the background. This way nothing else can show or
1471 * receive input events while we are locked. */
1472
1473 if (!wl_list_empty(&shell->hidden_surface_list)) {
1474 fprintf(stderr,
1475 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1476 __func__);
1477 }
1478
1479 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1480 /* skip input device sprites, cur->surface is uninitialised */
1481 if (cur->surface.resource.client == NULL)
1482 continue;
1483
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001484 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001485 continue;
1486
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001487 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001488 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001489 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001490 }
1491
Pekka Paalanen77346a62011-11-30 16:26:35 +02001492 launch_screensaver(shell);
1493
1494 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1495 show_screensaver(shell, shsurf);
1496
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001497 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001498 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001499 weston_compositor_wake(shell->compositor);
1500 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001501 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001502
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001503 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001505
1506 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001508 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1509 wl_input_device_set_keyboard_focus(&device->input_device,
1510 NULL, time);
1511 }
1512
1513 /* TODO: disable bindings that should not work while locked. */
1514
1515 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001516}
1517
1518static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001519unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001520{
1521 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1522
Pekka Paalanend81c2162011-11-16 13:47:34 +02001523 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001525 return;
1526 }
1527
1528 /* If desktop-shell client has gone away, unlock immediately. */
1529 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001530 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001531 return;
1532 }
1533
1534 if (shell->prepare_event_sent)
1535 return;
1536
1537 wl_resource_post_event(shell->child.desktop_shell,
1538 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1539 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001540}
1541
1542static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001543center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001546 GLfloat x = (mode->width - surface->geometry.width) / 2;
1547 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001548
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001549 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001550}
1551
1552static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001553map(struct weston_shell *base, struct weston_surface *surface,
1554 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001555{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001556 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001557 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001558 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001559 struct shell_surface *shsurf;
1560 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1561 int do_configure;
Juan Zhao96879df2012-02-07 08:45:41 +08001562 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001563
Pekka Paalanen77346a62011-11-30 16:26:35 +02001564 shsurf = get_shell_surface(surface);
1565 if (shsurf)
1566 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001567
Pekka Paalanen77346a62011-11-30 16:26:35 +02001568 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001569 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001570 do_configure = 0;
1571 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001572 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001573 do_configure = 1;
1574 }
1575
Pekka Paalanen60921e52012-01-25 15:55:43 +02001576 surface->geometry.width = width;
1577 surface->geometry.height = height;
1578 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001579
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001580 weston_compositor_update_drag_surfaces(compositor);
1581
Pekka Paalanen77346a62011-11-30 16:26:35 +02001582 /* initial positioning, see also configure() */
1583 switch (surface_type) {
1584 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001585 weston_surface_set_position(surface, 10 + random() % 400,
1586 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001587 break;
1588 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001589 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001590 break;
Alex Wu4539b082012-03-01 12:57:46 +08001591 case SHELL_SURFACE_FULLSCREEN:
1592 shell_map_fullscreen(shsurf);
1593 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001594 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001595 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001596 panel_height = get_output_panel_height(shell,surface->output);
1597 weston_surface_set_position(surface, surface->output->x,
1598 surface->output->y + panel_height);
1599 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001600 case SHELL_SURFACE_LOCK:
1601 center_on_output(surface, get_default_output(compositor));
1602 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001603 case SHELL_SURFACE_POPUP:
1604 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001605 case SHELL_SURFACE_NONE:
1606 weston_surface_set_position(surface,
1607 surface->geometry.x + sx,
1608 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001609 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001610 default:
1611 ;
1612 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001613
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001614 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001615 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001616 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001617 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001618 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001619 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001620 break;
1621 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001622 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001623 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001624 break;
1625 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001626 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001627 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001628
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001629 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001630 do_configure = 1;
1631 break;
1632 case SHELL_SURFACE_SCREENSAVER:
1633 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001634 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001635 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001636 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001637 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001638 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001639 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001640 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001641 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001642 break;
Alex Wu4539b082012-03-01 12:57:46 +08001643 case SHELL_SURFACE_FULLSCREEN:
1644 do_configure = 1;
1645 break;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001646 case SHELL_SURFACE_NONE:
Ander Conselvan de Oliveiraba727bd2012-02-23 13:29:25 +02001647 do_configure = 0;
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001648 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001649 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001650 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001651 if (!wl_list_empty(&shell->panels)) {
1652 struct shell_surface *panel =
1653 container_of(shell->panels.prev,
1654 struct shell_surface, link);
1655 wl_list_insert(&panel->surface->link, &surface->link);
1656 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001657 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001658 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001659 }
1660
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001661 if (do_configure) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001662 weston_surface_assign_output(surface);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001663 weston_compositor_repick(compositor);
Juan Zhao96879df2012-02-07 08:45:41 +08001664 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1665 surface->output = shsurf->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001666 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001667
Juan Zhao7bb92f02011-12-15 11:31:51 -05001668 switch (surface_type) {
1669 case SHELL_SURFACE_TOPLEVEL:
1670 case SHELL_SURFACE_TRANSIENT:
1671 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001672 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001673 if (!shell->locked)
1674 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001675 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001676 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001677 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001678 break;
1679 default:
1680 break;
1681 }
1682
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001683 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001684 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001685}
1686
1687static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001688configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001689 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001690{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001691 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001692 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001693 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001694 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001695
Pekka Paalanen77346a62011-11-30 16:26:35 +02001696 shsurf = get_shell_surface(surface);
1697 if (shsurf)
1698 surface_type = shsurf->type;
1699
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001700 surface->geometry.x = x;
1701 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001702 surface->geometry.width = width;
1703 surface->geometry.height = height;
1704 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001705
1706 switch (surface_type) {
1707 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001708 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001709 break;
Alex Wu4539b082012-03-01 12:57:46 +08001710 case SHELL_SURFACE_FULLSCREEN:
1711 shell_configure_fullscreen(shsurf);
1712 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1713 shell_stack_fullscreen(shsurf);
1714 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001715 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001716 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001717 surface->geometry.x = surface->output->x;
1718 surface->geometry.y = surface->output->y +
1719 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001720 break;
Alex Wu4539b082012-03-01 12:57:46 +08001721 case SHELL_SURFACE_TOPLEVEL:
1722 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001723 default:
1724 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001725 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001726
Alex Wu4539b082012-03-01 12:57:46 +08001727 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001728 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001729 weston_surface_assign_output(surface);
Alex Wu4539b082012-03-01 12:57:46 +08001730 weston_compositor_repick(surface->compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001731
1732 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1733 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001734 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1735 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001736 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001737}
1738
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001739static int launch_desktop_shell_process(struct wl_shell *shell);
1740
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001741static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001742desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001743{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001744 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001745 struct wl_shell *shell =
1746 container_of(process, struct wl_shell, child.process);
1747
1748 shell->child.process.pid = 0;
1749 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001750
1751 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1752 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001753 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001754 shell->child.deathstamp = time;
1755 shell->child.deathcount = 0;
1756 }
1757
1758 shell->child.deathcount++;
1759 if (shell->child.deathcount > 5) {
1760 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1761 return;
1762 }
1763
1764 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1765 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001766}
1767
1768static int
1769launch_desktop_shell_process(struct wl_shell *shell)
1770{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001771 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001772
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001774 &shell->child.process,
1775 shell_exe,
1776 desktop_shell_sigchld);
1777
1778 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001779 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001780 return 0;
1781}
1782
1783static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001784bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1785{
1786 struct wl_shell *shell = data;
1787
1788 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001789 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001790}
1791
Kristian Høgsberg75840622011-09-06 13:48:16 -04001792static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001793unbind_desktop_shell(struct wl_resource *resource)
1794{
1795 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001796
1797 if (shell->locked)
1798 resume_desktop(shell);
1799
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001800 shell->child.desktop_shell = NULL;
1801 shell->prepare_event_sent = false;
1802 free(resource);
1803}
1804
1805static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001806bind_desktop_shell(struct wl_client *client,
1807 void *data, uint32_t version, uint32_t id)
1808{
1809 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001810 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001811
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001812 resource = wl_client_add_object(client, &desktop_shell_interface,
1813 &desktop_shell_implementation,
1814 id, shell);
1815
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001816 if (client == shell->child.client) {
1817 resource->destroy = unbind_desktop_shell;
1818 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001819 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001820 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001821
1822 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1823 "permission to bind desktop_shell denied");
1824 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001825}
1826
Pekka Paalanen6e168112011-11-24 11:34:05 +02001827static void
1828screensaver_set_surface(struct wl_client *client,
1829 struct wl_resource *resource,
1830 struct wl_resource *shell_surface_resource,
1831 struct wl_resource *output_resource)
1832{
1833 struct wl_shell *shell = resource->data;
1834 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001835 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001836
Pekka Paalanen98262232011-12-01 10:42:22 +02001837 if (reset_shell_surface_type(surface))
1838 return;
1839
Pekka Paalanen77346a62011-11-30 16:26:35 +02001840 surface->type = SHELL_SURFACE_SCREENSAVER;
1841
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001842 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001843 surface->output = output;
1844 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001845}
1846
1847static const struct screensaver_interface screensaver_implementation = {
1848 screensaver_set_surface
1849};
1850
1851static void
1852unbind_screensaver(struct wl_resource *resource)
1853{
1854 struct wl_shell *shell = resource->data;
1855
Pekka Paalanen77346a62011-11-30 16:26:35 +02001856 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001857 free(resource);
1858}
1859
1860static void
1861bind_screensaver(struct wl_client *client,
1862 void *data, uint32_t version, uint32_t id)
1863{
1864 struct wl_shell *shell = data;
1865 struct wl_resource *resource;
1866
1867 resource = wl_client_add_object(client, &screensaver_interface,
1868 &screensaver_implementation,
1869 id, shell);
1870
Pekka Paalanen77346a62011-11-30 16:26:35 +02001871 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001872 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001873 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001874 return;
1875 }
1876
1877 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1878 "interface object already bound");
1879 wl_resource_destroy(resource, 0);
1880}
1881
Kristian Høgsberg07045392012-02-19 18:52:44 -05001882struct switcher {
1883 struct weston_compositor *compositor;
1884 struct weston_surface *current;
1885 struct wl_listener listener;
1886 struct wl_keyboard_grab grab;
1887};
1888
1889static void
1890switcher_next(struct switcher *switcher)
1891{
1892 struct weston_compositor *compositor = switcher->compositor;
1893 struct weston_surface *surface;
1894 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1895
1896 wl_list_for_each(surface, &compositor->surface_list, link) {
1897 /* Workaround for cursor surfaces. */
1898 if (surface->surface.resource.destroy_listener_list.next == NULL)
1899 continue;
1900
1901 switch (get_shell_surface_type(surface)) {
1902 case SHELL_SURFACE_TOPLEVEL:
1903 case SHELL_SURFACE_FULLSCREEN:
1904 case SHELL_SURFACE_MAXIMIZED:
1905 if (first == NULL)
1906 first = surface;
1907 if (prev == switcher->current)
1908 next = surface;
1909 prev = surface;
1910 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001911 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001912 weston_surface_damage(surface);
1913 break;
1914 default:
1915 break;
1916 }
1917 }
1918
1919 if (next == NULL)
1920 next = first;
1921
1922 wl_list_remove(&switcher->listener.link);
1923 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1924 &switcher->listener.link);
1925
1926 switcher->current = next;
1927 next->alpha = 255;
1928}
1929
1930static void
1931switcher_handle_surface_destroy(struct wl_listener *listener,
1932 struct wl_resource *resource, uint32_t time)
1933{
1934 struct switcher *switcher =
1935 container_of(listener, struct switcher, listener);
1936
1937 switcher_next(switcher);
1938}
1939
1940static void
1941switcher_destroy(struct switcher *switcher, uint32_t time)
1942{
1943 struct weston_compositor *compositor = switcher->compositor;
1944 struct weston_surface *surface;
1945 struct weston_input_device *device =
1946 (struct weston_input_device *) switcher->grab.input_device;
1947
1948 wl_list_for_each(surface, &compositor->surface_list, link) {
1949 surface->alpha = 255;
1950 weston_surface_damage(surface);
1951 }
1952
1953 activate(compositor->shell, switcher->current, device, time);
1954 wl_list_remove(&switcher->listener.link);
1955 wl_input_device_end_keyboard_grab(&device->input_device, time);
1956 free(switcher);
1957}
1958
1959static void
1960switcher_key(struct wl_keyboard_grab *grab,
1961 uint32_t time, uint32_t key, int32_t state)
1962{
1963 struct switcher *switcher = container_of(grab, struct switcher, grab);
1964 struct weston_input_device *device =
1965 (struct weston_input_device *) grab->input_device;
1966
1967 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1968 switcher_destroy(switcher, time);
1969 } else if (key == KEY_TAB && state) {
1970 switcher_next(switcher);
1971 }
1972};
1973
1974static const struct wl_keyboard_grab_interface switcher_grab = {
1975 switcher_key
1976};
1977
1978static void
1979switcher_binding(struct wl_input_device *device, uint32_t time,
1980 uint32_t key, uint32_t button,
1981 uint32_t state, void *data)
1982{
1983 struct weston_compositor *compositor = data;
1984 struct switcher *switcher;
1985
1986 switcher = malloc(sizeof *switcher);
1987 switcher->compositor = compositor;
1988 switcher->current = NULL;
1989 switcher->listener.func = switcher_handle_surface_destroy;
1990 wl_list_init(&switcher->listener.link);
1991
1992 switcher->grab.interface = &switcher_grab;
1993 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1994 switcher_next(switcher);
1995}
1996
Pekka Paalanen3c647232011-12-22 13:43:43 +02001997static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001998backlight_binding(struct wl_input_device *device, uint32_t time,
1999 uint32_t key, uint32_t button, uint32_t state, void *data)
2000{
2001 struct weston_compositor *compositor = data;
2002 struct weston_output *output;
2003
2004 /* TODO: we're limiting to simple use cases, where we assume just
2005 * control on the primary display. We'd have to extend later if we
2006 * ever get support for setting backlights on random desktop LCD
2007 * panels though */
2008 output = get_default_output(compositor);
2009 if (!output)
2010 return;
2011
2012 if (!output->set_backlight)
2013 return;
2014
2015 if ((key == KEY_F9 || key == KEY_BRIGHTNESSDOWN) &&
2016 output->backlight_current > 1)
2017 output->backlight_current--;
2018 else if ((key == KEY_F10 || key == KEY_BRIGHTNESSUP) &&
2019 output->backlight_current < 10)
2020 output->backlight_current++;
2021
2022 output->set_backlight(output, output->backlight_current);
2023}
2024
2025static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002026shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002027{
2028 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2029
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002030 if (shell->child.client)
2031 wl_client_destroy(shell->child.client);
2032
Pekka Paalanen3c647232011-12-22 13:43:43 +02002033 free(shell->screensaver.path);
2034 free(shell);
2035}
2036
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002037int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002038shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002039
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002040WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002041shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002042{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002043 struct wl_shell *shell;
2044
2045 shell = malloc(sizeof *shell);
2046 if (shell == NULL)
2047 return -1;
2048
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002049 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002050 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002051 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002052 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002053 shell->shell.map = map;
2054 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002055 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002056
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002057 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002058 wl_list_init(&shell->backgrounds);
2059 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002060 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002061
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002062 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002063
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002064 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2065 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002066 return -1;
2067
Kristian Høgsberg75840622011-09-06 13:48:16 -04002068 if (wl_display_add_global(ec->wl_display,
2069 &desktop_shell_interface,
2070 shell, bind_desktop_shell) == NULL)
2071 return -1;
2072
Pekka Paalanen6e168112011-11-24 11:34:05 +02002073 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2074 shell, bind_screensaver) == NULL)
2075 return -1;
2076
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002077 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002078 if (launch_desktop_shell_process(shell) != 0)
2079 return -1;
2080
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002081 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002082 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002084 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002085 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002086 MODIFIER_CTRL | MODIFIER_ALT,
2087 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002088 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002089 click_to_activate_binding, ec);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002090 weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07002091 zoom_binding, shell);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002092 weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
Scott Moreauc6d7f602012-02-23 22:28:37 -07002093 zoom_binding, shell);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002094 weston_compositor_add_binding(ec, 0, BTN_LEFT,
2095 MODIFIER_SUPER | MODIFIER_ALT,
2096 rotate_binding, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002097 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
2098 switcher_binding, ec);
2099
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002100 /* brightness */
2101 weston_compositor_add_binding(ec, KEY_F9, 0, MODIFIER_CTRL,
2102 backlight_binding, ec);
2103 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0,
2104 backlight_binding, ec);
2105 weston_compositor_add_binding(ec, KEY_F10, 0, MODIFIER_CTRL,
2106 backlight_binding, ec);
2107 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0,
2108 backlight_binding, ec);
2109
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002110 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002111
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002112 return 0;
2113}