blob: 7e58fd56f75a5a9e32a3a0fe6af39b8ebbd3bc4b [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
Tiago Vignattibe143262012-04-16 17:31:41 +030041struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040043
44 struct wl_listener lock_listener;
45 struct wl_listener unlock_listener;
46 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050048 struct weston_layer fullscreen_layer;
49 struct weston_layer panel_layer;
50 struct weston_layer toplevel_layer;
51 struct weston_layer background_layer;
52 struct weston_layer lock_layer;
53
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020056 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020057 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020058
59 unsigned deathcount;
60 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062
63 bool locked;
64 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020065
Pekka Paalanen068ae942011-11-28 14:11:15 +020066 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050067 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010068
69 struct wl_list backgrounds;
70 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020071
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020073 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020074 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 struct wl_resource *binding;
76 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050079
80 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040081};
82
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050083enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020084 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050085
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086 SHELL_SURFACE_PANEL,
87 SHELL_SURFACE_BACKGROUND,
88 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020089 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090
91 SHELL_SURFACE_TOPLEVEL,
92 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080094 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050095 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020096};
97
Scott Moreauff1db4a2012-04-17 19:06:18 -060098struct ping_timer {
99 struct wl_event_source *source;
100 uint32_t pong_received;
101 uint32_t serial;
102};
103
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200104struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200105 struct wl_resource resource;
106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200108 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500109 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300110 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200111
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500112 enum shell_surface_type type;
113 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800114 bool saved_position_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600115 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700123 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500124 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200125 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500126 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400127 struct wl_input_device *device;
128 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 } popup;
130
Alex Wu4539b082012-03-01 12:57:46 +0800131 struct {
132 enum wl_shell_surface_fullscreen_method type;
133 struct weston_transform transform; /* matrix from x, y */
134 uint32_t framerate;
135 struct weston_surface *black_surface;
136 } fullscreen;
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138 struct ping_timer *ping_timer;
139
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500140 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100142 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300143
144 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200145};
146
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300147struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700148 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300149 struct shell_surface *shsurf;
150 struct wl_listener shsurf_destroy_listener;
151};
152
153struct weston_move_grab {
154 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500155 int32_t dx, dy;
156};
157
Pekka Paalanen460099f2012-01-20 16:48:25 +0200158struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300159 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500160 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200161 struct {
162 int32_t x;
163 int32_t y;
164 } center;
165};
166
Alex Wubd3354b2012-04-17 17:20:49 +0800167static struct shell_surface *
168get_shell_surface(struct weston_surface *surface);
169
170static struct desktop_shell *
171shell_surface_get_shell(struct shell_surface *shsurf);
172
173static bool
174shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
175{
176 struct desktop_shell *shell;
177 struct weston_surface *top_fs_es;
178
179 shell = shell_surface_get_shell(shsurf);
180
181 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
182 return false;
183
184 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
185 struct weston_surface,
186 layer_link);
187 return (shsurf == get_shell_surface(top_fs_es));
188}
189
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500190static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400191destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300192{
193 struct shell_grab *grab;
194
195 grab = container_of(listener, struct shell_grab,
196 shsurf_destroy_listener);
197
198 grab->shsurf = NULL;
199}
200
201static void
202shell_grab_init(struct shell_grab *grab,
203 const struct wl_pointer_grab_interface *interface,
204 struct shell_surface *shsurf)
205{
206 grab->grab.interface = interface;
207 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400208 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
209 wl_signal_add(&shsurf->resource.destroy_signal,
210 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300211
212}
213
214static void
215shell_grab_finish(struct shell_grab *grab)
216{
217 wl_list_remove(&grab->shsurf_destroy_listener.link);
218}
219
220static void
Alex Wu4539b082012-03-01 12:57:46 +0800221center_on_output(struct weston_surface *surface,
222 struct weston_output *output);
223
224static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300225shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200226{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200227 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200228 char *path = NULL;
229 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200230
231 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200232 { "path", CONFIG_KEY_STRING, &path },
233 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200234 };
235
236 struct config_section cs[] = {
237 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
238 };
239
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200240 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500241 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200242 free(config_file);
243
Pekka Paalanen7296e792011-12-07 16:22:00 +0200244 shell->screensaver.path = path;
245 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200246}
247
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200248static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400249noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500250 struct wl_surface *surface, int32_t x, int32_t y)
251{
252 grab->focus = NULL;
253}
254
255static void
Scott Moreau447013d2012-02-18 05:05:29 -0700256move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500257 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500259 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500260 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261 struct shell_surface *shsurf = move->base.shsurf;
262 struct weston_surface *es;
263
264 if (!shsurf)
265 return;
266
267 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500269 weston_surface_configure(es,
270 device->x + move->dx,
271 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200272 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500273}
274
275static void
Scott Moreau447013d2012-02-18 05:05:29 -0700276move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400277 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500278{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
280 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500281 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500282
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500283 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400285 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500286 free(grab);
287 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500288}
289
Scott Moreau447013d2012-02-18 05:05:29 -0700290static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500291 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500292 move_grab_motion,
293 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500294};
295
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400296static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600297ping_timeout_handler(void *data)
298{
299 struct shell_surface *shsurf = data;
300
301 if (!shsurf || !shsurf->ping_timer)
302 return 1;
303
304 if (shsurf->ping_timer->pong_received) {
305 free(shsurf->ping_timer);
306 shsurf->ping_timer = NULL;
307 } else {
308 /* Client is not responding */
309 shsurf->unresponsive = 1;
310 }
311
312 return 1;
313}
314
315static void
316ping_handler(struct weston_surface *surface, uint32_t serial)
317{
318 struct shell_surface *shsurf;
319 shsurf = get_shell_surface(surface);
320 struct wl_event_loop *loop;
321 int ping_timeout = 15000;
322
323 if (!shsurf)
324 return;
325
326 if (!shsurf->ping_timer) {
327 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
328 if (!shsurf->ping_timer)
329 return;
330
331 shsurf->ping_timer->serial = serial;
332 shsurf->ping_timer->pong_received = 0;
333 loop = wl_display_get_event_loop(surface->compositor->wl_display);
334 shsurf->ping_timer->source =
335 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
336 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
337
338 wl_shell_surface_send_ping(&shsurf->resource, serial);
339 }
340}
341
342static void
343shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
344 uint32_t serial)
345{
346 struct shell_surface *shsurf = resource->data;
347
348 if (!shsurf || !shsurf->ping_timer)
349 return;
350
351 if (shsurf->ping_timer->serial == serial) {
352 shsurf->ping_timer->pong_received = 1;
353 shsurf->unresponsive = 0;
354 free(shsurf->ping_timer);
355 shsurf->ping_timer = NULL;
356 }
357}
358
359static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500360weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400361 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500362{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500363 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300364 struct shell_surface *shsurf = get_shell_surface(es);
365
366 if (!shsurf)
367 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500368
369 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400370 if (!move)
371 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500372
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300373 shell_grab_init(&move->base, &move_grab_interface, shsurf);
374
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200375 move->dx = es->geometry.x - wd->input_device.grab_x;
376 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500377
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400379 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500380
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400381 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400382
383 return 0;
384}
385
386static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200387shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400388 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200391 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400392
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500393 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400394 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500395 wd->input_device.pointer_focus != &shsurf->surface->surface)
396 return;
397
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400398 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400399 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500400}
401
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500402struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300403 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500404 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200405 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500406};
407
408static void
Scott Moreau447013d2012-02-18 05:05:29 -0700409resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500410 uint32_t time, int32_t x, int32_t y)
411{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500412 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500413 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500414 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200415 int32_t from_x, from_y;
416 int32_t to_x, to_y;
417
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300418 if (!resize->base.shsurf)
419 return;
420
421 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200422 device->grab_x, device->grab_y,
423 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300424 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200425 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500426
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200427 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200428 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200429 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200430 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500431 } else {
432 width = resize->width;
433 }
434
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200435 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200436 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200437 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200438 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500439 } else {
440 height = resize->height;
441 }
442
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300443 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400444 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500445}
446
447static void
Scott Moreau447013d2012-02-18 05:05:29 -0700448resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400449 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500450{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300451 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500452 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500453
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500454 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300455 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400456 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500457 free(grab);
458 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500459}
460
Scott Moreau447013d2012-02-18 05:05:29 -0700461static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500462 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500463 resize_grab_motion,
464 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500465};
466
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400467static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500468weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400469 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500470{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500471 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500472
Alex Wu4539b082012-03-01 12:57:46 +0800473 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
474 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500475
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200476 if (edges == 0 || edges > 15 ||
477 (edges & 3) == 3 || (edges & 12) == 12)
478 return 0;
479
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500480 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400481 if (!resize)
482 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500483
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300484 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
485
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500486 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200487 resize->width = shsurf->surface->geometry.width;
488 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400489
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300490 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400491 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500492
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400493 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400494
495 return 0;
496}
497
498static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200499shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400500 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200501 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500503 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200504 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400505
Alex Wu4539b082012-03-01 12:57:46 +0800506 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
507 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400508
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500509 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400510 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500511 wd->input_device.pointer_focus != &shsurf->surface->surface)
512 return;
513
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400514 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400515 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500516}
517
Juan Zhao96879df2012-02-07 08:45:41 +0800518static struct weston_output *
519get_default_output(struct weston_compositor *compositor)
520{
521 return container_of(compositor->output_list.next,
522 struct weston_output, link);
523}
524
Alex Wu4539b082012-03-01 12:57:46 +0800525static void
526shell_unset_fullscreen(struct shell_surface *shsurf)
527{
528 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800529 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
530 shell_surface_is_top_fullscreen(shsurf)) {
531 weston_output_switch_mode(shsurf->fullscreen_output,
532 shsurf->fullscreen_output->origin);
533 }
Alex Wu4539b082012-03-01 12:57:46 +0800534 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
535 shsurf->fullscreen.framerate = 0;
536 wl_list_remove(&shsurf->fullscreen.transform.link);
537 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800538 if (shsurf->fullscreen.black_surface)
539 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800540 shsurf->fullscreen.black_surface = NULL;
541 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300542 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800543 weston_surface_set_position(shsurf->surface,
544 shsurf->saved_x, shsurf->saved_y);
545}
546
Pekka Paalanen98262232011-12-01 10:42:22 +0200547static int
548reset_shell_surface_type(struct shell_surface *surface)
549{
550 switch (surface->type) {
551 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800552 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200553 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800554 case SHELL_SURFACE_MAXIMIZED:
555 surface->output = get_default_output(surface->surface->compositor);
556 weston_surface_set_position(surface->surface,
557 surface->saved_x,
558 surface->saved_y);
559 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200560 case SHELL_SURFACE_PANEL:
561 case SHELL_SURFACE_BACKGROUND:
562 wl_list_remove(&surface->link);
563 wl_list_init(&surface->link);
564 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200565 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200566 case SHELL_SURFACE_LOCK:
567 wl_resource_post_error(&surface->resource,
568 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200569 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200570 return -1;
571 case SHELL_SURFACE_NONE:
572 case SHELL_SURFACE_TOPLEVEL:
573 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500574 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200575 break;
576 }
577
578 surface->type = SHELL_SURFACE_NONE;
579 return 0;
580}
581
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500582static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200583shell_surface_set_toplevel(struct wl_client *client,
584 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400585
586{
Pekka Paalanen98262232011-12-01 10:42:22 +0200587 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400588
Pekka Paalanen98262232011-12-01 10:42:22 +0200589 if (reset_shell_surface_type(surface))
590 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400591
Pekka Paalanen98262232011-12-01 10:42:22 +0200592 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400593}
594
595static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200596shell_surface_set_transient(struct wl_client *client,
597 struct wl_resource *resource,
598 struct wl_resource *parent_resource,
599 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400600{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200601 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500602 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200603 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500604 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400605
Pekka Paalanen98262232011-12-01 10:42:22 +0200606 if (reset_shell_surface_type(shsurf))
607 return;
608
Alex Wu4539b082012-03-01 12:57:46 +0800609 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800610 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200611 weston_surface_set_position(es, pes->geometry.x + x,
612 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400613
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200614 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400615}
616
Tiago Vignattibe143262012-04-16 17:31:41 +0300617static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800618shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200619{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400620 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800621}
622
623static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300624get_output_panel_height(struct desktop_shell *shell,
625 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800626{
627 struct shell_surface *priv;
628 int panel_height = 0;
629
630 if (!output)
631 return 0;
632
Tiago Vignattibe143262012-04-16 17:31:41 +0300633 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800634 if (priv->output == output) {
635 panel_height = priv->surface->geometry.height;
636 break;
637 }
638 }
639 return panel_height;
640}
641
642static void
643shell_surface_set_maximized(struct wl_client *client,
644 struct wl_resource *resource,
645 struct wl_resource *output_resource )
646{
647 struct shell_surface *shsurf = resource->data;
648 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300649 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800650 uint32_t edges = 0, panel_height = 0;
651
652 /* get the default output, if the client set it as NULL
653 check whether the ouput is available */
654 if (output_resource)
655 shsurf->output = output_resource->data;
656 else
657 shsurf->output = get_default_output(es->compositor);
658
659 if (reset_shell_surface_type(shsurf))
660 return;
661
662 shsurf->saved_x = es->geometry.x;
663 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800664 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800665
Tiago Vignattibe143262012-04-16 17:31:41 +0300666 shell = shell_surface_get_shell(shsurf);
667 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800668 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500669
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400670 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500671 es->output->current->width,
672 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800673
674 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200675}
676
Alex Wu21858432012-04-01 20:13:08 +0800677static void
678black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
679
Alex Wu4539b082012-03-01 12:57:46 +0800680static struct weston_surface *
681create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800682 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800683 GLfloat x, GLfloat y, int w, int h)
684{
685 struct weston_surface *surface = NULL;
686
687 surface = weston_surface_create(ec);
688 if (surface == NULL) {
689 fprintf(stderr, "no memory\n");
690 return NULL;
691 }
692
Alex Wu21858432012-04-01 20:13:08 +0800693 surface->configure = black_surface_configure;
694 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800695 weston_surface_configure(surface, x, y, w, h);
696 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
697 return surface;
698}
699
700/* Create black surface and append it to the associated fullscreen surface.
701 * Handle size dismatch and positioning according to the method. */
702static void
703shell_configure_fullscreen(struct shell_surface *shsurf)
704{
705 struct weston_output *output = shsurf->fullscreen_output;
706 struct weston_surface *surface = shsurf->surface;
707 struct weston_matrix *matrix;
708 float scale;
709
710 center_on_output(surface, output);
711
712 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500713 shsurf->fullscreen.black_surface =
714 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800715 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500716 output->x, output->y,
717 output->current->width,
718 output->current->height);
719
720 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
721 wl_list_insert(&surface->layer_link,
722 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800723 shsurf->fullscreen.black_surface->output = output;
724
725 switch (shsurf->fullscreen.type) {
726 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
727 break;
728 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
729 matrix = &shsurf->fullscreen.transform.matrix;
730 weston_matrix_init(matrix);
731 scale = (float)output->current->width/(float)surface->geometry.width;
732 weston_matrix_scale(matrix, scale, scale, 1);
733 wl_list_remove(&shsurf->fullscreen.transform.link);
734 wl_list_insert(surface->geometry.transformation_list.prev,
735 &shsurf->fullscreen.transform.link);
736 weston_surface_set_position(surface, output->x, output->y);
737 break;
738 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800739 if (shell_surface_is_top_fullscreen(shsurf)) {
740 struct weston_mode mode = {0,
741 surface->geometry.width,
742 surface->geometry.height,
743 shsurf->fullscreen.framerate};
744
745 if (weston_output_switch_mode(output, &mode) == 0) {
746 weston_surface_configure(shsurf->fullscreen.black_surface,
747 output->x, output->y,
748 output->current->width,
749 output->current->height);
750 weston_surface_set_position(surface, output->x, output->y);
751 break;
752 }
753 }
Alex Wu4539b082012-03-01 12:57:46 +0800754 break;
755 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
756 break;
757 default:
758 break;
759 }
760}
761
762/* make the fullscreen and black surface at the top */
763static void
764shell_stack_fullscreen(struct shell_surface *shsurf)
765{
Alex Wubd3354b2012-04-17 17:20:49 +0800766 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800767 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300768 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800769
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500770 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500771 wl_list_insert(&shell->fullscreen_layer.surface_list,
772 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800773 weston_surface_damage(surface);
774
775 if (!shsurf->fullscreen.black_surface)
776 shsurf->fullscreen.black_surface =
777 create_black_surface(surface->compositor,
778 surface,
779 output->x, output->y,
780 output->current->width,
781 output->current->height);
782
783 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500784 wl_list_insert(&surface->layer_link,
785 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500786 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800787}
788
789static void
790shell_map_fullscreen(struct shell_surface *shsurf)
791{
Alex Wu4539b082012-03-01 12:57:46 +0800792 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800793 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800794}
795
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400796static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500798 struct wl_resource *resource,
799 uint32_t method,
800 uint32_t framerate,
801 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400802{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200803 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500804 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800805
806 if (output_resource)
807 shsurf->output = output_resource->data;
808 else
809 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400810
Pekka Paalanen98262232011-12-01 10:42:22 +0200811 if (reset_shell_surface_type(shsurf))
812 return;
813
Alex Wu4539b082012-03-01 12:57:46 +0800814 shsurf->fullscreen_output = shsurf->output;
815 shsurf->fullscreen.type = method;
816 shsurf->fullscreen.framerate = framerate;
817 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400818
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200819 shsurf->saved_x = es->geometry.x;
820 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800821 shsurf->saved_position_valid = true;
822
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300823 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300824 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500825
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400826 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500827 shsurf->output->current->width,
828 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400829}
830
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500831static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400832popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500833 struct wl_surface *surface, int32_t x, int32_t y)
834{
835 struct wl_input_device *device = grab->input_device;
836 struct shell_surface *priv =
837 container_of(grab, struct shell_surface, popup.grab);
838 struct wl_client *client = priv->surface->surface.resource.client;
839
Pekka Paalanencb108432012-01-19 16:25:40 +0200840 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400841 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500842 grab->focus = surface;
843 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400844 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500845 grab->focus = NULL;
846 }
847}
848
849static void
Scott Moreau447013d2012-02-18 05:05:29 -0700850popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200851 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500852{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500853 struct wl_resource *resource;
854
855 resource = grab->input_device->pointer_focus_resource;
856 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500857 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500858}
859
860static void
Scott Moreau447013d2012-02-18 05:05:29 -0700861popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400862 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500863{
864 struct wl_resource *resource;
865 struct shell_surface *shsurf =
866 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400867 struct wl_display *display;
868 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500869
870 resource = grab->input_device->pointer_focus_resource;
871 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400872 display = wl_client_get_display(resource->client);
873 serial = wl_display_get_serial(display);
874 wl_input_device_send_button(resource, serial,
875 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500876 } else if (state == 0 &&
877 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400878 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500879 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400880 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500881 shsurf->popup.grab.input_device = NULL;
882 }
883
884 if (state == 0)
885 shsurf->popup.initial_up = 1;
886}
887
Scott Moreau447013d2012-02-18 05:05:29 -0700888static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500889 popup_grab_focus,
890 popup_grab_motion,
891 popup_grab_button,
892};
893
894static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400895shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500896{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400897 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500898 struct weston_surface *es = shsurf->surface;
899 struct weston_surface *parent = shsurf->parent->surface;
900
901 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500902 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500903
Pekka Paalanen938269a2012-02-07 14:19:01 +0200904 weston_surface_update_transform(parent);
905 if (parent->transform.enabled) {
906 shsurf->popup.parent_transform.matrix =
907 parent->transform.matrix;
908 } else {
909 /* construct x, y translation matrix */
910 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
911 shsurf->popup.parent_transform.matrix.d[12] =
912 parent->geometry.x;
913 shsurf->popup.parent_transform.matrix.d[13] =
914 parent->geometry.y;
915 }
916 wl_list_insert(es->geometry.transformation_list.prev,
917 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200918 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500919
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500920 shsurf->popup.initial_up = 0;
921
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400922 /* We don't require the grab to still be active, but if another
923 * grab has started in the meantime, we end the popup now. */
924 if (device->grab_serial == shsurf->popup.serial) {
925 wl_input_device_start_pointer_grab(device,
926 &shsurf->popup.grab);
927 } else {
928 wl_shell_surface_send_popup_done(&shsurf->resource);
929 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500930}
931
932static void
933shell_surface_set_popup(struct wl_client *client,
934 struct wl_resource *resource,
935 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400936 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500937 struct wl_resource *parent_resource,
938 int32_t x, int32_t y, uint32_t flags)
939{
940 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500941
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500942 shsurf->type = SHELL_SURFACE_POPUP;
943 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400944 shsurf->popup.device = input_device_resource->data;
945 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500946 shsurf->popup.x = x;
947 shsurf->popup.y = y;
948}
949
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200950static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -0600951 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200952 shell_surface_move,
953 shell_surface_resize,
954 shell_surface_set_toplevel,
955 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500956 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800957 shell_surface_set_popup,
958 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200959};
960
961static void
962destroy_shell_surface(struct wl_resource *resource)
963{
964 struct shell_surface *shsurf = resource->data;
965
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500966 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400967 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500968
Alex Wubd3354b2012-04-17 17:20:49 +0800969 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
970 shell_surface_is_top_fullscreen(shsurf)) {
971 weston_output_switch_mode(shsurf->fullscreen_output,
972 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300973 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200974
Alex Wuaa08e2d2012-03-05 11:01:40 +0800975 if (shsurf->fullscreen.black_surface)
976 weston_surface_destroy(shsurf->fullscreen.black_surface);
977
Alex Wubd3354b2012-04-17 17:20:49 +0800978 /* As destroy_resource() use wl_list_for_each_safe(),
979 * we can always remove the listener.
980 */
981 wl_list_remove(&shsurf->surface_destroy_listener.link);
982 shsurf->surface->configure = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600983 if (shsurf->ping_timer)
984 free(shsurf->ping_timer);
Alex Wubd3354b2012-04-17 17:20:49 +0800985
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200986 wl_list_remove(&shsurf->link);
987 free(shsurf);
988}
989
990static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400991shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200992{
993 struct shell_surface *shsurf = container_of(listener,
994 struct shell_surface,
995 surface_destroy_listener);
996
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400997 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200998}
999
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001000static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001001get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001002{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001003 struct wl_listener *listener;
1004
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001005 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1006 shell_handle_surface_destroy);
1007 if (listener)
1008 return container_of(listener, struct shell_surface,
1009 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001010
1011 return NULL;
1012}
1013
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001014static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001015shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1016
1017static void
Pekka Paalanen46229672011-11-29 15:49:31 +02001018shell_get_shell_surface(struct wl_client *client,
1019 struct wl_resource *resource,
1020 uint32_t id,
1021 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001022{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001023 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001024 struct shell_surface *shsurf;
1025
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001026 if (get_shell_surface(surface)) {
1027 wl_resource_post_error(surface_resource,
1028 WL_DISPLAY_ERROR_INVALID_OBJECT,
Alex Wubd3354b2012-04-17 17:20:49 +08001029 "desktop_shell::get_shell_surface already requested");
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001030 return;
1031 }
1032
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001033 if (surface->configure) {
1034 wl_resource_post_error(surface_resource,
1035 WL_DISPLAY_ERROR_INVALID_OBJECT,
1036 "surface->configure already set");
1037 return;
1038 }
1039
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001040 shsurf = calloc(1, sizeof *shsurf);
1041 if (!shsurf) {
1042 wl_resource_post_no_memory(resource);
1043 return;
1044 }
1045
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001046 surface->configure = shell_surface_configure;
1047
Scott Moreauff1db4a2012-04-17 19:06:18 -06001048 shsurf->unresponsive = 0;
1049
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001050 shsurf->resource.destroy = destroy_shell_surface;
1051 shsurf->resource.object.id = id;
1052 shsurf->resource.object.interface = &wl_shell_surface_interface;
1053 shsurf->resource.object.implementation =
1054 (void (**)(void)) &shell_surface_implementation;
1055 shsurf->resource.data = shsurf;
1056
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001057 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +08001058 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001059 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001060 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1061 shsurf->fullscreen.framerate = 0;
1062 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001063 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001064 wl_list_init(&shsurf->fullscreen.transform.link);
1065
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001066 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1067 wl_signal_add(&surface->surface.resource.destroy_signal,
1068 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001069
1070 /* init link so its safe to always remove it in destroy_shell_surface */
1071 wl_list_init(&shsurf->link);
1072
Pekka Paalanen460099f2012-01-20 16:48:25 +02001073 /* empty when not in use */
1074 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001075 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001076
Pekka Paalanen98262232011-12-01 10:42:22 +02001077 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001078
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001079 wl_client_add_resource(client, &shsurf->resource);
1080}
1081
1082static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001083 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001084};
1085
Kristian Høgsberg07937562011-04-12 17:25:42 -04001086static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001087handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001088{
1089 proc->pid = 0;
1090}
1091
1092static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001093launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001094{
1095 if (shell->screensaver.binding)
1096 return;
1097
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001098 if (!shell->screensaver.path)
1099 return;
1100
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001101 if (shell->screensaver.process.pid != 0) {
1102 fprintf(stderr, "old screensaver still running\n");
1103 return;
1104 }
1105
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001106 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001107 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001108 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001109 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001110}
1111
1112static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001113terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001114{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001115 if (shell->screensaver.process.pid == 0)
1116 return;
1117
1118 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001119}
1120
1121static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001122show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001123{
1124 struct wl_list *list;
1125
1126 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001127 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001128 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001129 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001131 wl_list_remove(&surface->surface->layer_link);
1132 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001133 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001134 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001135}
1136
1137static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001138hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001139{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001140 wl_list_remove(&surface->surface->layer_link);
1141 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001142 surface->surface->output = NULL;
1143}
1144
1145static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001146desktop_shell_set_background(struct wl_client *client,
1147 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001148 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001149 struct wl_resource *surface_resource)
1150{
Tiago Vignattibe143262012-04-16 17:31:41 +03001151 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001152 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001153 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001154 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001155
Pekka Paalanen98262232011-12-01 10:42:22 +02001156 if (reset_shell_surface_type(shsurf))
1157 return;
1158
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001159 wl_list_for_each(priv, &shell->backgrounds, link) {
1160 if (priv->output == output_resource->data) {
1161 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001162 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001163 wl_list_remove(&priv->link);
1164 break;
1165 }
1166 }
1167
Pekka Paalanen068ae942011-11-28 14:11:15 +02001168 shsurf->type = SHELL_SURFACE_BACKGROUND;
1169 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001170
Pekka Paalanen068ae942011-11-28 14:11:15 +02001171 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001172
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001173 weston_surface_set_position(surface, shsurf->output->x,
1174 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001175
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001176 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001177 surface_resource,
1178 shsurf->output->current->width,
1179 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001180}
1181
1182static void
1183desktop_shell_set_panel(struct wl_client *client,
1184 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001185 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001186 struct wl_resource *surface_resource)
1187{
Tiago Vignattibe143262012-04-16 17:31:41 +03001188 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001189 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001191 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001192
Pekka Paalanen98262232011-12-01 10:42:22 +02001193 if (reset_shell_surface_type(shsurf))
1194 return;
1195
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001196 wl_list_for_each(priv, &shell->panels, link) {
1197 if (priv->output == output_resource->data) {
1198 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001199 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001200 wl_list_remove(&priv->link);
1201 break;
1202 }
1203 }
1204
Pekka Paalanen068ae942011-11-28 14:11:15 +02001205 shsurf->type = SHELL_SURFACE_PANEL;
1206 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001207
Pekka Paalanen068ae942011-11-28 14:11:15 +02001208 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001209
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001210 weston_surface_set_position(surface, shsurf->output->x,
1211 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001212
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001213 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001214 surface_resource,
1215 shsurf->output->current->width,
1216 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001217}
1218
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001219static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001220handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001221{
Tiago Vignattibe143262012-04-16 17:31:41 +03001222 struct desktop_shell *shell =
1223 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001224
1225 fprintf(stderr, "lock surface gone\n");
1226 shell->lock_surface = NULL;
1227}
1228
1229static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001230desktop_shell_set_lock_surface(struct wl_client *client,
1231 struct wl_resource *resource,
1232 struct wl_resource *surface_resource)
1233{
Tiago Vignattibe143262012-04-16 17:31:41 +03001234 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001235 struct shell_surface *surface = surface_resource->data;
1236
1237 if (reset_shell_surface_type(surface))
1238 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001239
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001240 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001241
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001242 if (!shell->locked)
1243 return;
1244
Pekka Paalanen98262232011-12-01 10:42:22 +02001245 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001246
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001247 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1248 wl_signal_add(&surface_resource->destroy_signal,
1249 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001250
Pekka Paalanen068ae942011-11-28 14:11:15 +02001251 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001252}
1253
1254static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001255resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001256{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001257 struct shell_surface *tmp;
1258
1259 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1260 hide_screensaver(shell, tmp);
1261
1262 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001263
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001264 wl_list_remove(&shell->lock_layer.link);
1265 wl_list_insert(&shell->compositor->cursor_layer.link,
1266 &shell->fullscreen_layer.link);
1267 wl_list_insert(&shell->fullscreen_layer.link,
1268 &shell->panel_layer.link);
1269 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001270
1271 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001272 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001273 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001274 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001275}
1276
1277static void
1278desktop_shell_unlock(struct wl_client *client,
1279 struct wl_resource *resource)
1280{
Tiago Vignattibe143262012-04-16 17:31:41 +03001281 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001282
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001283 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001284
1285 if (shell->locked)
1286 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001287}
1288
Kristian Høgsberg75840622011-09-06 13:48:16 -04001289static const struct desktop_shell_interface desktop_shell_implementation = {
1290 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001291 desktop_shell_set_panel,
1292 desktop_shell_set_lock_surface,
1293 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001294};
1295
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001296static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001297get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001298{
1299 struct shell_surface *shsurf;
1300
1301 shsurf = get_shell_surface(surface);
1302 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001303 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001304 return shsurf->type;
1305}
1306
Kristian Høgsberg75840622011-09-06 13:48:16 -04001307static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001308move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001309 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001310{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001311 struct weston_surface *surface =
1312 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001313
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001314 if (surface == NULL)
1315 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001316
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001317 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001318 case SHELL_SURFACE_PANEL:
1319 case SHELL_SURFACE_BACKGROUND:
1320 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001321 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001322 return;
1323 default:
1324 break;
1325 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001326
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001327 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001328}
1329
1330static void
1331resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001332 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001333{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001334 struct weston_surface *surface =
1335 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001336 uint32_t edges = 0;
1337 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001338 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001339
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001340 if (surface == NULL)
1341 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001342
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001343 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001344 if (!shsurf)
1345 return;
1346
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001347 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001348 case SHELL_SURFACE_PANEL:
1349 case SHELL_SURFACE_BACKGROUND:
1350 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001351 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001352 return;
1353 default:
1354 break;
1355 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001356
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001357 weston_surface_from_global(surface,
1358 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001359
Pekka Paalanen60921e52012-01-25 15:55:43 +02001360 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001361 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001362 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001363 edges |= 0;
1364 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001365 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001366
Pekka Paalanen60921e52012-01-25 15:55:43 +02001367 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001368 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001369 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001370 edges |= 0;
1371 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001372 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001373
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001374 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001375 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001376}
1377
1378static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001379surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1380 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1381{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001382 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001383 struct shell_surface *shsurf;
1384 struct weston_surface *surface =
1385 (struct weston_surface *) device->pointer_focus;
1386
1387 if (surface == NULL)
1388 return;
1389
1390 shsurf = get_shell_surface(surface);
1391 if (!shsurf)
1392 return;
1393
1394 switch (shsurf->type) {
1395 case SHELL_SURFACE_BACKGROUND:
1396 case SHELL_SURFACE_SCREENSAVER:
1397 return;
1398 default:
1399 break;
1400 }
1401
1402 surface->alpha += value * step;
1403
1404 if (surface->alpha > 255)
1405 surface->alpha = 255;
1406 if (surface->alpha < step)
1407 surface->alpha = step;
1408
1409 surface->geometry.dirty = 1;
1410 weston_surface_damage(surface);
1411}
1412
1413static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001414zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001415 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001416{
1417 struct weston_input_device *wd = (struct weston_input_device *) device;
1418 struct weston_compositor *compositor = wd->compositor;
1419 struct weston_output *output;
1420
1421 wl_list_for_each(output, &compositor->output_list, link) {
1422 if (pixman_region32_contains_point(&output->region,
1423 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001424 output->zoom.active = 1;
1425 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001426
1427 if (output->zoom.level >= 1.0) {
1428 output->zoom.active = 0;
1429 output->zoom.level = 1.0;
1430 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001431
1432 if (output->zoom.level < output->zoom.increment)
1433 output->zoom.level = output->zoom.increment;
1434
1435 weston_output_update_zoom(output, device->x, device->y);
1436 }
1437 }
1438}
1439
Scott Moreauccbf29d2012-02-22 14:21:41 -07001440static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001441terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001442 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001443{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001445
1446 if (state)
1447 wl_display_terminate(compositor->wl_display);
1448}
1449
1450static void
Scott Moreau447013d2012-02-18 05:05:29 -07001451rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001452 uint32_t time, int32_t x, int32_t y)
1453{
1454 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001455 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001456 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001457 struct shell_surface *shsurf = rotate->base.shsurf;
1458 struct weston_surface *surface;
1459 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1460
1461 if (!shsurf)
1462 return;
1463
1464 surface = shsurf->surface;
1465
1466 cx = 0.5f * surface->geometry.width;
1467 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001468
1469 dx = device->x - rotate->center.x;
1470 dy = device->y - rotate->center.y;
1471 r = sqrtf(dx * dx + dy * dy);
1472
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001473 wl_list_remove(&shsurf->rotation.transform.link);
1474 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001475
1476 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001477 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001478 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001479
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001480 weston_matrix_init(&rotate->rotation);
1481 rotate->rotation.d[0] = dx / r;
1482 rotate->rotation.d[4] = -dy / r;
1483 rotate->rotation.d[1] = -rotate->rotation.d[4];
1484 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001485
1486 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001487 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001488 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001489 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001490 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001491
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001492 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001493 &shsurf->surface->geometry.transformation_list,
1494 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001495 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001496 wl_list_init(&shsurf->rotation.transform.link);
1497 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001498 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001499 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001500
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001501 /* We need to adjust the position of the surface
1502 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001503 cposx = surface->geometry.x + cx;
1504 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001505 dposx = rotate->center.x - cposx;
1506 dposy = rotate->center.y - cposy;
1507 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001508 weston_surface_set_position(surface,
1509 surface->geometry.x + dposx,
1510 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001511 }
1512
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001513 /* Repaint implies weston_surface_update_transform(), which
1514 * lazily applies the damage due to rotation update.
1515 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001516 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001517}
1518
1519static void
Scott Moreau447013d2012-02-18 05:05:29 -07001520rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001521 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001522{
1523 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001524 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001525 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001526 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001527
1528 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001529 if (shsurf)
1530 weston_matrix_multiply(&shsurf->rotation.rotation,
1531 &rotate->rotation);
1532 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001533 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001534 free(rotate);
1535 }
1536}
1537
Scott Moreau447013d2012-02-18 05:05:29 -07001538static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001539 noop_grab_focus,
1540 rotate_grab_motion,
1541 rotate_grab_button,
1542};
1543
1544static void
1545rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001546 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001547{
1548 struct weston_surface *base_surface =
1549 (struct weston_surface *) device->pointer_focus;
1550 struct shell_surface *surface;
1551 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001552 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001553 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001554
1555 if (base_surface == NULL)
1556 return;
1557
1558 surface = get_shell_surface(base_surface);
1559 if (!surface)
1560 return;
1561
1562 switch (surface->type) {
1563 case SHELL_SURFACE_PANEL:
1564 case SHELL_SURFACE_BACKGROUND:
1565 case SHELL_SURFACE_FULLSCREEN:
1566 case SHELL_SURFACE_SCREENSAVER:
1567 return;
1568 default:
1569 break;
1570 }
1571
Pekka Paalanen460099f2012-01-20 16:48:25 +02001572 rotate = malloc(sizeof *rotate);
1573 if (!rotate)
1574 return;
1575
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001576 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001577
1578 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001579 surface->surface->geometry.width / 2,
1580 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001581 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001582
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001583 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001584
1585 dx = device->x - rotate->center.x;
1586 dy = device->y - rotate->center.y;
1587 r = sqrtf(dx * dx + dy * dy);
1588 if (r > 20.0f) {
1589 struct weston_matrix inverse;
1590
1591 weston_matrix_init(&inverse);
1592 inverse.d[0] = dx / r;
1593 inverse.d[4] = dy / r;
1594 inverse.d[1] = -inverse.d[4];
1595 inverse.d[5] = inverse.d[0];
1596 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001597
1598 weston_matrix_init(&rotate->rotation);
1599 rotate->rotation.d[0] = dx / r;
1600 rotate->rotation.d[4] = -dy / r;
1601 rotate->rotation.d[1] = -rotate->rotation.d[4];
1602 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001603 } else {
1604 weston_matrix_init(&surface->rotation.rotation);
1605 weston_matrix_init(&rotate->rotation);
1606 }
1607
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001608 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001609}
1610
1611static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001612activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001614{
Alex Wu21858432012-04-01 20:13:08 +08001615 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001616
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001617 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001618
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001619 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001620 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001621 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001622 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001623 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001624
Pekka Paalanen77346a62011-11-30 16:26:35 +02001625 case SHELL_SURFACE_SCREENSAVER:
1626 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001627 if (shell->lock_surface)
1628 weston_surface_restack(es,
1629 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001630 break;
Alex Wu4539b082012-03-01 12:57:46 +08001631 case SHELL_SURFACE_FULLSCREEN:
1632 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001633 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001634 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001635 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001636 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001637 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001638 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1639 wl_list_for_each_reverse_safe(surf,
1640 prev,
1641 &shell->fullscreen_layer.surface_list,
1642 layer_link)
1643 weston_surface_restack(surf,
1644 &shell->toplevel_layer.surface_list);
1645 }
1646
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001647 weston_surface_restack(es,
1648 &shell->toplevel_layer.surface_list);
1649 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001650 }
1651}
1652
Alex Wu21858432012-04-01 20:13:08 +08001653/* no-op func for checking black surface */
1654static void
1655black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1656{
1657}
1658
1659static bool
1660is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1661{
1662 if (es->configure == black_surface_configure) {
1663 if (fs_surface)
1664 *fs_surface = (struct weston_surface *)es->private;
1665 return true;
1666 }
1667 return false;
1668}
1669
Kristian Høgsberg75840622011-09-06 13:48:16 -04001670static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001671click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001672 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001673 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001674{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001675 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001676 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001677 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001678 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001679
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001680 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001681 if (!focus)
1682 return;
1683
Alex Wu21858432012-04-01 20:13:08 +08001684 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001685 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001686
Alex Wu9c35e6b2012-03-05 14:13:13 +08001687 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001688 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001689}
1690
1691static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001692lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001693{
Tiago Vignattibe143262012-04-16 17:31:41 +03001694 struct desktop_shell *shell =
1695 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001696 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001697 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001698 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001699
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001700 if (shell->locked) {
1701 wl_list_for_each(output, &shell->compositor->output_list, link)
1702 /* TODO: find a way to jump to other DPMS levels */
1703 if (output->set_dpms)
1704 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001705 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001706 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001707
1708 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001709
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001710 /* Hide all surfaces by removing the fullscreen, panel and
1711 * toplevel layers. This way nothing else can show or receive
1712 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001713
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001714 wl_list_remove(&shell->panel_layer.link);
1715 wl_list_remove(&shell->toplevel_layer.link);
1716 wl_list_remove(&shell->fullscreen_layer.link);
1717 wl_list_insert(&shell->compositor->cursor_layer.link,
1718 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001719
Pekka Paalanen77346a62011-11-30 16:26:35 +02001720 launch_screensaver(shell);
1721
1722 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1723 show_screensaver(shell, shsurf);
1724
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001725 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001726 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001727 weston_compositor_wake(shell->compositor);
1728 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001729 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001730
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001731 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001732 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001733
1734 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001735 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1736 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001737 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001738 }
1739
1740 /* TODO: disable bindings that should not work while locked. */
1741
1742 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001743}
1744
1745static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001746unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001747{
Tiago Vignattibe143262012-04-16 17:31:41 +03001748 struct desktop_shell *shell =
1749 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001750
Pekka Paalanend81c2162011-11-16 13:47:34 +02001751 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001752 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001753 return;
1754 }
1755
1756 /* If desktop-shell client has gone away, unlock immediately. */
1757 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001758 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001759 return;
1760 }
1761
1762 if (shell->prepare_event_sent)
1763 return;
1764
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001765 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001766 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001767}
1768
1769static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001770center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001771{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001772 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001773 GLfloat x = (mode->width - surface->geometry.width) / 2;
1774 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001775
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001776 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001777}
1778
1779static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001780map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001781 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001783 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001784 struct shell_surface *shsurf;
1785 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001786 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001787 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001788
Pekka Paalanen77346a62011-11-30 16:26:35 +02001789 shsurf = get_shell_surface(surface);
1790 if (shsurf)
1791 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001792
Pekka Paalanen60921e52012-01-25 15:55:43 +02001793 surface->geometry.width = width;
1794 surface->geometry.height = height;
1795 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001796
1797 /* initial positioning, see also configure() */
1798 switch (surface_type) {
1799 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001800 weston_surface_set_position(surface, 10 + random() % 400,
1801 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001802 break;
1803 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001804 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001805 break;
Alex Wu4539b082012-03-01 12:57:46 +08001806 case SHELL_SURFACE_FULLSCREEN:
1807 shell_map_fullscreen(shsurf);
1808 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001809 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001810 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001811 panel_height = get_output_panel_height(shell,surface->output);
1812 weston_surface_set_position(surface, surface->output->x,
1813 surface->output->y + panel_height);
1814 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001815 case SHELL_SURFACE_LOCK:
1816 center_on_output(surface, get_default_output(compositor));
1817 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001818 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001819 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001820 case SHELL_SURFACE_NONE:
1821 weston_surface_set_position(surface,
1822 surface->geometry.x + sx,
1823 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001824 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001825 default:
1826 ;
1827 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001828
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001829 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001830 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001831 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001832 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001833 wl_list_insert(&shell->background_layer.surface_list,
1834 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001835 break;
1836 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001837 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001838 wl_list_insert(&shell->panel_layer.surface_list,
1839 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001840 break;
1841 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001842 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001843 wl_list_insert(&shell->lock_layer.surface_list,
1844 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001845 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001846 break;
1847 case SHELL_SURFACE_SCREENSAVER:
1848 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001849 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001850 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001851 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001852 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001853 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001854 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001855 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001856 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001857 case SHELL_SURFACE_POPUP:
1858 case SHELL_SURFACE_TRANSIENT:
1859 parent = shsurf->parent->surface;
1860 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1861 break;
Alex Wu4539b082012-03-01 12:57:46 +08001862 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001863 case SHELL_SURFACE_NONE:
1864 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001865 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001866 wl_list_insert(&shell->toplevel_layer.surface_list,
1867 &surface->layer_link);
1868 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001869 }
1870
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001871 if (surface_type != SHELL_SURFACE_NONE) {
1872 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001873 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1874 surface->output = shsurf->output;
1875 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001876
Juan Zhao7bb92f02011-12-15 11:31:51 -05001877 switch (surface_type) {
1878 case SHELL_SURFACE_TOPLEVEL:
1879 case SHELL_SURFACE_TRANSIENT:
1880 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001881 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001882 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001883 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001884 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001885 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001886 break;
1887 default:
1888 break;
1889 }
1890
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001891 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001892 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001893}
1894
1895static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001896configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001897 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001898{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001899 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1900 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001901
Pekka Paalanen77346a62011-11-30 16:26:35 +02001902 shsurf = get_shell_surface(surface);
1903 if (shsurf)
1904 surface_type = shsurf->type;
1905
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001906 surface->geometry.x = x;
1907 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001908 surface->geometry.width = width;
1909 surface->geometry.height = height;
1910 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001911
1912 switch (surface_type) {
1913 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001914 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001915 break;
Alex Wu4539b082012-03-01 12:57:46 +08001916 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08001917 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001918 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001919 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001920 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001921 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001922 surface->geometry.x = surface->output->x;
1923 surface->geometry.y = surface->output->y +
1924 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001925 break;
Alex Wu4539b082012-03-01 12:57:46 +08001926 case SHELL_SURFACE_TOPLEVEL:
1927 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001928 default:
1929 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001930 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001931
Alex Wu4539b082012-03-01 12:57:46 +08001932 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001933 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001934 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001935
1936 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1937 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001938 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1939 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001940 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001941}
1942
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001943static void
1944shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1945{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001946 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03001947 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001948
1949 if (!weston_surface_is_mapped(es)) {
1950 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001951 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001952 es->geometry.width != es->buffer->width ||
1953 es->geometry.height != es->buffer->height) {
1954 GLfloat from_x, from_y;
1955 GLfloat to_x, to_y;
1956
1957 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1958 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1959 configure(shell, es,
1960 es->geometry.x + to_x - from_x,
1961 es->geometry.y + to_y - from_y,
1962 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001963 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001964 }
1965}
1966
Tiago Vignattibe143262012-04-16 17:31:41 +03001967static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001968
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001969static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001970desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001971{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001972 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03001973 struct desktop_shell *shell =
1974 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001975
1976 shell->child.process.pid = 0;
1977 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001978
1979 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1980 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001981 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001982 shell->child.deathstamp = time;
1983 shell->child.deathcount = 0;
1984 }
1985
1986 shell->child.deathcount++;
1987 if (shell->child.deathcount > 5) {
1988 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1989 return;
1990 }
1991
1992 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1993 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001994}
1995
1996static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001997launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001998{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001999 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002000
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002002 &shell->child.process,
2003 shell_exe,
2004 desktop_shell_sigchld);
2005
2006 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002007 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002008 return 0;
2009}
2010
2011static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002012bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2013{
Tiago Vignattibe143262012-04-16 17:31:41 +03002014 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002015
2016 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002017 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002018}
2019
Kristian Høgsberg75840622011-09-06 13:48:16 -04002020static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002021unbind_desktop_shell(struct wl_resource *resource)
2022{
Tiago Vignattibe143262012-04-16 17:31:41 +03002023 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002024
2025 if (shell->locked)
2026 resume_desktop(shell);
2027
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002028 shell->child.desktop_shell = NULL;
2029 shell->prepare_event_sent = false;
2030 free(resource);
2031}
2032
2033static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002034bind_desktop_shell(struct wl_client *client,
2035 void *data, uint32_t version, uint32_t id)
2036{
Tiago Vignattibe143262012-04-16 17:31:41 +03002037 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002038 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002039
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002040 resource = wl_client_add_object(client, &desktop_shell_interface,
2041 &desktop_shell_implementation,
2042 id, shell);
2043
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002044 if (client == shell->child.client) {
2045 resource->destroy = unbind_desktop_shell;
2046 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002047 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002048 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002049
2050 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2051 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002052 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002053}
2054
Pekka Paalanen6e168112011-11-24 11:34:05 +02002055static void
2056screensaver_set_surface(struct wl_client *client,
2057 struct wl_resource *resource,
2058 struct wl_resource *shell_surface_resource,
2059 struct wl_resource *output_resource)
2060{
Tiago Vignattibe143262012-04-16 17:31:41 +03002061 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002062 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002063 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002064
Pekka Paalanen98262232011-12-01 10:42:22 +02002065 if (reset_shell_surface_type(surface))
2066 return;
2067
Pekka Paalanen77346a62011-11-30 16:26:35 +02002068 surface->type = SHELL_SURFACE_SCREENSAVER;
2069
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002070 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002071 surface->output = output;
2072 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002073}
2074
2075static const struct screensaver_interface screensaver_implementation = {
2076 screensaver_set_surface
2077};
2078
2079static void
2080unbind_screensaver(struct wl_resource *resource)
2081{
Tiago Vignattibe143262012-04-16 17:31:41 +03002082 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002083
Pekka Paalanen77346a62011-11-30 16:26:35 +02002084 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002085 free(resource);
2086}
2087
2088static void
2089bind_screensaver(struct wl_client *client,
2090 void *data, uint32_t version, uint32_t id)
2091{
Tiago Vignattibe143262012-04-16 17:31:41 +03002092 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002093 struct wl_resource *resource;
2094
2095 resource = wl_client_add_object(client, &screensaver_interface,
2096 &screensaver_implementation,
2097 id, shell);
2098
Pekka Paalanen77346a62011-11-30 16:26:35 +02002099 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002100 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002101 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002102 return;
2103 }
2104
2105 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2106 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002107 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002108}
2109
Kristian Høgsberg07045392012-02-19 18:52:44 -05002110struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002111 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002112 struct weston_surface *current;
2113 struct wl_listener listener;
2114 struct wl_keyboard_grab grab;
2115};
2116
2117static void
2118switcher_next(struct switcher *switcher)
2119{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002120 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002121 struct weston_surface *surface;
2122 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002123 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002124
2125 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002126 switch (get_shell_surface_type(surface)) {
2127 case SHELL_SURFACE_TOPLEVEL:
2128 case SHELL_SURFACE_FULLSCREEN:
2129 case SHELL_SURFACE_MAXIMIZED:
2130 if (first == NULL)
2131 first = surface;
2132 if (prev == switcher->current)
2133 next = surface;
2134 prev = surface;
2135 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002136 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002137 weston_surface_damage(surface);
2138 break;
2139 default:
2140 break;
2141 }
Alex Wu1659daa2012-04-01 20:13:09 +08002142
2143 if (is_black_surface(surface, NULL)) {
2144 surface->alpha = 64;
2145 surface->geometry.dirty = 1;
2146 weston_surface_damage(surface);
2147 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002148 }
2149
2150 if (next == NULL)
2151 next = first;
2152
Alex Wu07b26062012-03-12 16:06:01 +08002153 if (next == NULL)
2154 return;
2155
Kristian Høgsberg07045392012-02-19 18:52:44 -05002156 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002157 wl_signal_add(&next->surface.resource.destroy_signal,
2158 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002159
2160 switcher->current = next;
2161 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002162
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002163 shsurf = get_shell_surface(switcher->current);
2164 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2165 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002166}
2167
2168static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002169switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002170{
2171 struct switcher *switcher =
2172 container_of(listener, struct switcher, listener);
2173
2174 switcher_next(switcher);
2175}
2176
2177static void
2178switcher_destroy(struct switcher *switcher, uint32_t time)
2179{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002180 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002181 struct weston_surface *surface;
2182 struct weston_input_device *device =
2183 (struct weston_input_device *) switcher->grab.input_device;
2184
2185 wl_list_for_each(surface, &compositor->surface_list, link) {
2186 surface->alpha = 255;
2187 weston_surface_damage(surface);
2188 }
2189
Alex Wu07b26062012-03-12 16:06:01 +08002190 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002191 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002192 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002193 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002194 free(switcher);
2195}
2196
2197static void
2198switcher_key(struct wl_keyboard_grab *grab,
2199 uint32_t time, uint32_t key, int32_t state)
2200{
2201 struct switcher *switcher = container_of(grab, struct switcher, grab);
2202 struct weston_input_device *device =
2203 (struct weston_input_device *) grab->input_device;
2204
2205 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2206 switcher_destroy(switcher, time);
2207 } else if (key == KEY_TAB && state) {
2208 switcher_next(switcher);
2209 }
2210};
2211
2212static const struct wl_keyboard_grab_interface switcher_grab = {
2213 switcher_key
2214};
2215
2216static void
2217switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002218 uint32_t key, uint32_t button, uint32_t axis,
2219 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002220{
Tiago Vignattibe143262012-04-16 17:31:41 +03002221 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002222 struct switcher *switcher;
2223
2224 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002225 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002226 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002227 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002228 wl_list_init(&switcher->listener.link);
2229
2230 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002231 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2232 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002233 switcher_next(switcher);
2234}
2235
Pekka Paalanen3c647232011-12-22 13:43:43 +02002236static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002237backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002238 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002239{
2240 struct weston_compositor *compositor = data;
2241 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002242 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002243
2244 /* TODO: we're limiting to simple use cases, where we assume just
2245 * control on the primary display. We'd have to extend later if we
2246 * ever get support for setting backlights on random desktop LCD
2247 * panels though */
2248 output = get_default_output(compositor);
2249 if (!output)
2250 return;
2251
2252 if (!output->set_backlight)
2253 return;
2254
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002255 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2256 backlight_new = output->backlight_current - 25;
2257 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2258 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002259
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002260 if (backlight_new < 5)
2261 backlight_new = 5;
2262 if (backlight_new > 255)
2263 backlight_new = 255;
2264
2265 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002266 output->set_backlight(output, output->backlight_current);
2267}
2268
2269static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002270debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002271 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002272{
Tiago Vignattibe143262012-04-16 17:31:41 +03002273 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002274 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002275 struct weston_surface *surface;
2276
2277 if (shell->debug_repaint_surface) {
2278 weston_surface_destroy(shell->debug_repaint_surface);
2279 shell->debug_repaint_surface = NULL;
2280 } else {
2281 surface = weston_surface_create(compositor);
2282 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2283 weston_surface_configure(surface, 0, 0, 8192, 8192);
2284 wl_list_insert(&compositor->fade_layer.surface_list,
2285 &surface->layer_link);
2286 weston_surface_assign_output(surface);
2287 pixman_region32_init(&surface->input);
2288
2289 /* Here's the dirty little trick that makes the
2290 * repaint debugging work: we force an
2291 * update_transform first to update dependent state
2292 * and clear the geometry.dirty bit. Then we clear
2293 * the surface damage so it only gets repainted
2294 * piecewise as we repaint other things. */
2295
2296 weston_surface_update_transform(surface);
2297 pixman_region32_fini(&surface->damage);
2298 pixman_region32_init(&surface->damage);
2299 shell->debug_repaint_surface = surface;
2300 }
2301}
2302
2303static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002304shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002305{
Scott Moreauff1db4a2012-04-17 19:06:18 -06002306 struct weston_surface *surface;
2307 struct shell_surface *shsurf;
Tiago Vignattibe143262012-04-16 17:31:41 +03002308 struct desktop_shell *shell =
2309 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002310
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002311 if (shell->child.client)
2312 wl_client_destroy(shell->child.client);
2313
Scott Moreauff1db4a2012-04-17 19:06:18 -06002314 wl_list_for_each(surface, &shell->compositor->surface_list, link) {
2315 shsurf = get_shell_surface(surface);
2316 if (!shsurf)
2317 continue;
2318 if (shsurf->ping_timer)
2319 free(shsurf->ping_timer);
2320 }
2321
Pekka Paalanen3c647232011-12-22 13:43:43 +02002322 free(shell->screensaver.path);
2323 free(shell);
2324}
2325
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002326int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002327shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002328
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002329WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002330shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002331{
Tiago Vignattibe143262012-04-16 17:31:41 +03002332 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002333
2334 shell = malloc(sizeof *shell);
2335 if (shell == NULL)
2336 return -1;
2337
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002338 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002339 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002340
2341 shell->destroy_listener.notify = shell_destroy;
2342 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2343 shell->lock_listener.notify = lock;
2344 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2345 shell->unlock_listener.notify = unlock;
2346 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002347 ec->ping_handler = ping_handler;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002348
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002349 wl_list_init(&shell->backgrounds);
2350 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002351 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002352
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002353 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2354 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2355 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2356 weston_layer_init(&shell->background_layer,
2357 &shell->toplevel_layer.link);
2358 wl_list_init(&shell->lock_layer.surface_list);
2359
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002360 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002361
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002362 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2363 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002364 return -1;
2365
Kristian Høgsberg75840622011-09-06 13:48:16 -04002366 if (wl_display_add_global(ec->wl_display,
2367 &desktop_shell_interface,
2368 shell, bind_desktop_shell) == NULL)
2369 return -1;
2370
Pekka Paalanen6e168112011-11-24 11:34:05 +02002371 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2372 shell, bind_screensaver) == NULL)
2373 return -1;
2374
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002375 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002376 if (launch_desktop_shell_process(shell) != 0)
2377 return -1;
2378
Scott Moreau6a3633d2012-03-20 08:47:59 -06002379 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2380 move_binding, shell);
2381 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2382 resize_binding, shell);
2383 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2384 MODIFIER_CTRL | MODIFIER_ALT,
2385 terminate_binding, ec);
2386 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002387 click_to_activate_binding, shell);
Scott Moreau1b45a792012-03-22 10:58:23 -06002388 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002389 MODIFIER_SUPER | MODIFIER_ALT,
2390 surface_opacity_binding, NULL);
2391 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002392 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002393 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002394 MODIFIER_SUPER | MODIFIER_ALT,
2395 rotate_binding, NULL);
2396 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002397 switcher_binding, shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002398
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002399 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002400 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2401 backlight_binding, ec);
2402 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2403 backlight_binding, ec);
2404 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2405 backlight_binding, ec);
2406 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2407 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002408
Scott Moreau6a3633d2012-03-20 08:47:59 -06002409 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002410 debug_repaint_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002411
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002412 return 0;
2413}