blob: 28cf7d48d1f080dbf9b2500c65bb661f251eb45a [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øgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
42
Juan Zhaoe10d2792012-04-25 19:09:52 +080043enum animation_type {
44 ANIMATION_NONE,
45
46 ANIMATION_ZOOM,
47 ANIMATION_FADE
48};
49
Jonas Ådahle3cddce2012-06-13 00:01:22 +020050struct workspace {
51 struct weston_layer layer;
52};
53
Tiago Vignattibe143262012-04-16 17:31:41 +030054struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040056
57 struct wl_listener lock_listener;
58 struct wl_listener unlock_listener;
59 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020060
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050061 struct weston_layer fullscreen_layer;
62 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050063 struct weston_layer background_layer;
64 struct weston_layer lock_layer;
65
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040066 struct wl_listener pointer_focus_listener;
67 struct weston_surface *busy_surface;
68
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020069 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050070 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020071 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020072 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020073
74 unsigned deathcount;
75 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020076 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020077
78 bool locked;
79 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020080
Pekka Paalanen068ae942011-11-28 14:11:15 +020081 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050082 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010083
84 struct wl_list backgrounds;
85 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020086
Pekka Paalanen77346a62011-11-30 16:26:35 +020087 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020088 struct wl_array array;
89 unsigned int current;
90 unsigned int num;
91 } workspaces;
92
93 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020094 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020095 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020096 struct wl_resource *binding;
97 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050098 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020099 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500100
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300101 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800102 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500103 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400104};
105
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500106enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200107 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500108
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109 SHELL_SURFACE_PANEL,
110 SHELL_SURFACE_BACKGROUND,
111 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200112 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500113
114 SHELL_SURFACE_TOPLEVEL,
115 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800117 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500118 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200119};
120
Scott Moreauff1db4a2012-04-17 19:06:18 -0600121struct ping_timer {
122 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600123 uint32_t serial;
124};
125
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200126struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200127 struct wl_resource resource;
128
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200130 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500131 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300132 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200133
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400134 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400135 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500136 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800137 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800138 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600139 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100140
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200142 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500143 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200144 } rotation;
145
146 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700147 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500148 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200149 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500150 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100151 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400152 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500153 } popup;
154
Alex Wu4539b082012-03-01 12:57:46 +0800155 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300156 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400157 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300158 } transient;
159
160 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800161 enum wl_shell_surface_fullscreen_method type;
162 struct weston_transform transform; /* matrix from x, y */
163 uint32_t framerate;
164 struct weston_surface *black_surface;
165 } fullscreen;
166
Scott Moreauff1db4a2012-04-17 19:06:18 -0600167 struct ping_timer *ping_timer;
168
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500169 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500170 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100171 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400172
173 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200174};
175
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700177 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300178 struct shell_surface *shsurf;
179 struct wl_listener shsurf_destroy_listener;
180};
181
182struct weston_move_grab {
183 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100184 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500185};
186
Pekka Paalanen460099f2012-01-20 16:48:25 +0200187struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500189 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200190 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400191 GLfloat x;
192 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200193 } center;
194};
195
Alex Wubd3354b2012-04-17 17:20:49 +0800196static struct shell_surface *
197get_shell_surface(struct weston_surface *surface);
198
199static struct desktop_shell *
200shell_surface_get_shell(struct shell_surface *shsurf);
201
202static bool
203shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
204{
205 struct desktop_shell *shell;
206 struct weston_surface *top_fs_es;
207
208 shell = shell_surface_get_shell(shsurf);
209
210 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
211 return false;
212
213 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
214 struct weston_surface,
215 layer_link);
216 return (shsurf == get_shell_surface(top_fs_es));
217}
218
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500219static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400220destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300221{
222 struct shell_grab *grab;
223
224 grab = container_of(listener, struct shell_grab,
225 shsurf_destroy_listener);
226
227 grab->shsurf = NULL;
228}
229
230static void
231shell_grab_init(struct shell_grab *grab,
232 const struct wl_pointer_grab_interface *interface,
233 struct shell_surface *shsurf)
234{
235 grab->grab.interface = interface;
236 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400237 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
238 wl_signal_add(&shsurf->resource.destroy_signal,
239 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300240
241}
242
243static void
244shell_grab_finish(struct shell_grab *grab)
245{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400246 if (grab->shsurf)
247 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300248}
249
250static void
Alex Wu4539b082012-03-01 12:57:46 +0800251center_on_output(struct weston_surface *surface,
252 struct weston_output *output);
253
Daniel Stone496ca172012-05-30 16:31:42 +0100254static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300255get_modifier(char *modifier)
256{
257 if (!modifier)
258 return MODIFIER_SUPER;
259
260 if (!strcmp("ctrl", modifier))
261 return MODIFIER_CTRL;
262 else if (!strcmp("alt", modifier))
263 return MODIFIER_ALT;
264 else if (!strcmp("super", modifier))
265 return MODIFIER_SUPER;
266 else
267 return MODIFIER_SUPER;
268}
269
Juan Zhaoe10d2792012-04-25 19:09:52 +0800270static enum animation_type
271get_animation_type(char *animation)
272{
273 if (!animation)
274 return ANIMATION_NONE;
275
276 if (!strcmp("zoom", animation))
277 return ANIMATION_ZOOM;
278 else if (!strcmp("fade", animation))
279 return ANIMATION_FADE;
280 else
281 return ANIMATION_NONE;
282}
283
Alex Wu4539b082012-03-01 12:57:46 +0800284static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300285shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200286{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200287 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200288 char *path = NULL;
289 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200290 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300291 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800292 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200293
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400294 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300295 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800296 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200297 { "num-workspaces",
298 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200299 };
300
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400301 struct config_key saver_keys[] = {
302 { "path", CONFIG_KEY_STRING, &path },
303 { "duration", CONFIG_KEY_INTEGER, &duration },
304 };
305
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200306 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400307 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200308 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
309 };
310
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200311 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500312 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200313 free(config_file);
314
Pekka Paalanen7296e792011-12-07 16:22:00 +0200315 shell->screensaver.path = path;
316 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300317 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800318 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200319 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
320}
321
322static void
323workspace_destroy(struct workspace *ws)
324{
325 free(ws);
326}
327
328static struct workspace *
329workspace_create(void)
330{
331 struct workspace *ws = malloc(sizeof *ws);
332 if (ws == NULL)
333 return NULL;
334
335 weston_layer_init(&ws->layer, NULL);
336
337 return ws;
338}
339
340static struct workspace *
341get_workspace(struct desktop_shell *shell, unsigned int index)
342{
343 struct workspace **pws = shell->workspaces.array.data;
344 pws += index;
345 return *pws;
346}
347
348static struct workspace *
349get_current_workspace(struct desktop_shell *shell)
350{
351 return get_workspace(shell, shell->workspaces.current);
352}
353
354static void
355activate_workspace(struct desktop_shell *shell, unsigned int index)
356{
357 struct workspace *ws;
358
359 ws = get_workspace(shell, index);
360 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
361
362 shell->workspaces.current = index;
363}
364
365static void
366change_workspace(struct desktop_shell *shell, unsigned int index)
367{
368 struct workspace *from;
369 struct workspace *to;
370 struct weston_seat *seat;
371
372 if (index == shell->workspaces.current)
373 return;
374
375 /* Don't change workspace when there is any fullscreen surfaces. */
376 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
377 return;
378
379 /* Clear keyboard focus so that no hidden surfaces will keep it. */
380 wl_list_for_each(seat, &shell->compositor->seat_list, link)
381 if (seat->seat.keyboard)
382 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
383
384 from = get_current_workspace(shell);
385 to = get_workspace(shell, index);
386
387 shell->workspaces.current = index;
388 wl_list_insert(&from->layer.link, &to->layer.link);
389 wl_list_remove(&from->layer.link);
390
391 weston_compositor_damage_all(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392}
393
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200394static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400395noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100396 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500397{
398 grab->focus = NULL;
399}
400
401static void
Scott Moreau447013d2012-02-18 05:05:29 -0700402move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100403 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500404{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500405 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100406 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300407 struct shell_surface *shsurf = move->base.shsurf;
408 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100409 int dx = wl_fixed_to_int(pointer->x + move->dx);
410 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300411
412 if (!shsurf)
413 return;
414
415 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500416
Daniel Stone103db7f2012-05-08 17:17:55 +0100417 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200418 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500419}
420
421static void
Scott Moreau447013d2012-02-18 05:05:29 -0700422move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100423 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500424{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300425 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
426 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100427 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100428 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500429
Daniel Stone4dbadb12012-05-30 16:31:51 +0100430 if (pointer->button_count == 0 &&
431 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300432 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100433 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500434 free(grab);
435 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500436}
437
Scott Moreau447013d2012-02-18 05:05:29 -0700438static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500439 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500440 move_grab_motion,
441 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500442};
443
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600444static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400445busy_cursor_grab_focus(struct wl_pointer_grab *base,
446 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600447{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400448 struct shell_grab *grab = (struct shell_grab *) base;
449 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600450
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400451 if (grab->grab.focus != surface) {
452 shell_grab_finish(grab);
453 wl_pointer_end_grab(pointer);
454 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600455 }
456}
457
458static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400459busy_cursor_grab_motion(struct wl_pointer_grab *grab,
460 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600461{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400462}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600463
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400464static void
465busy_cursor_grab_button(struct wl_pointer_grab *grab,
466 uint32_t time, uint32_t button, uint32_t state)
467{
468}
469
470static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
471 busy_cursor_grab_focus,
472 busy_cursor_grab_motion,
473 busy_cursor_grab_button,
474};
475
476static void
477set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
478{
479 struct shell_grab *grab;
480 struct desktop_shell *shell = shsurf->shell;
481 struct weston_seat *seat = (struct weston_seat *) pointer->seat;
482 struct weston_surface *sprite;
483
484 grab = malloc(sizeof *grab);
485 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600486 return;
487
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400488 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
489 grab->grab.focus = &shsurf->surface->surface;
490 wl_pointer_start_grab(pointer, &grab->grab);
491 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600492
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400493 sprite = (struct weston_surface *) seat->sprite;
494 shell->busy_surface->output = sprite->output;
495}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600496
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400497static void
498end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
499{
500 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
501
502 if (grab->grab.interface == &busy_cursor_grab_interface) {
503 shell_grab_finish(grab);
504 wl_pointer_end_grab(pointer);
505 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600506 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600507}
508
Scott Moreau9521d5e2012-04-19 13:06:17 -0600509static void
510ping_timer_destroy(struct shell_surface *shsurf)
511{
512 if (!shsurf || !shsurf->ping_timer)
513 return;
514
515 if (shsurf->ping_timer->source)
516 wl_event_source_remove(shsurf->ping_timer->source);
517
518 free(shsurf->ping_timer);
519 shsurf->ping_timer = NULL;
520}
521
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400522static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600523ping_timeout_handler(void *data)
524{
525 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400526 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600527
Scott Moreau9521d5e2012-04-19 13:06:17 -0600528 /* Client is not responding */
529 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400530
531 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
532 if (seat->seat.pointer->focus == &shsurf->surface->surface)
533 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600534
535 return 1;
536}
537
538static void
539ping_handler(struct weston_surface *surface, uint32_t serial)
540{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400541 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600542 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400543 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600544
545 if (!shsurf)
546 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400547 if (!shsurf->resource.client)
548 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600549
550 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300551 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600552 if (!shsurf->ping_timer)
553 return;
554
555 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600556 loop = wl_display_get_event_loop(surface->compositor->wl_display);
557 shsurf->ping_timer->source =
558 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
559 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
560
561 wl_shell_surface_send_ping(&shsurf->resource, serial);
562 }
563}
564
565static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400566handle_pointer_focus(struct wl_listener *listener, void *data)
567{
568 struct wl_pointer *pointer = data;
569 struct weston_surface *surface =
570 (struct weston_surface *) pointer->focus;
571 struct weston_compositor *compositor;
572 struct shell_surface *shsurf;
573 uint32_t serial;
574
575 if (!surface)
576 return;
577
578 compositor = surface->compositor;
579 shsurf = get_shell_surface(surface);
580
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300581 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400582 set_busy_cursor(shsurf, pointer);
583 } else {
584 serial = wl_display_next_serial(compositor->wl_display);
585 ping_handler(surface, serial);
586 }
587}
588
589static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600590shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
591 uint32_t serial)
592{
593 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400594 struct desktop_shell *shell = shsurf->shell;
595 struct weston_seat *seat;
596 struct weston_compositor *ec = shsurf->surface->compositor;
597 struct wl_pointer *pointer;
598 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600599
Scott Moreauff1db4a2012-04-17 19:06:18 -0600600 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400601 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600602 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400603 if (was_unresponsive) {
604 /* Received pong from previously unresponsive client */
605 wl_list_for_each(seat, &ec->seat_list, link) {
606 pointer = seat->seat.pointer;
607 if (pointer->focus ==
608 &shell->busy_surface->surface &&
609 pointer->current ==
610 &shsurf->surface->surface)
611 end_busy_cursor(shsurf, pointer);
612 }
613 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600614 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600615 }
616}
617
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400618static void
619shell_surface_set_title(struct wl_client *client,
620 struct wl_resource *resource, const char *title)
621{
622 struct shell_surface *shsurf = resource->data;
623
624 free(shsurf->title);
625 shsurf->title = strdup(title);
626}
627
628static void
629shell_surface_set_class(struct wl_client *client,
630 struct wl_resource *resource, const char *class)
631{
632 struct shell_surface *shsurf = resource->data;
633
634 free(shsurf->class);
635 shsurf->class = strdup(class);
636}
637
Scott Moreauff1db4a2012-04-17 19:06:18 -0600638static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400639surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500640{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500641 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300642
643 if (!shsurf)
644 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500645
646 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400647 if (!move)
648 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500649
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300650 shell_grab_init(&move->base, &move_grab_interface, shsurf);
651
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400652 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100653 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400654 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100655 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500656
Daniel Stone37816df2012-05-16 18:45:18 +0100657 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500658
Daniel Stone37816df2012-05-16 18:45:18 +0100659 wl_pointer_set_focus(ws->seat.pointer, NULL,
660 wl_fixed_from_int(0),
661 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400662
663 return 0;
664}
665
666static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200667shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100668 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400669{
Daniel Stone37816df2012-05-16 18:45:18 +0100670 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200671 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400672
Daniel Stone37816df2012-05-16 18:45:18 +0100673 if (ws->seat.pointer->button_count == 0 ||
674 ws->seat.pointer->grab_serial != serial ||
675 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500676 return;
677
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400678 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400679 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500680}
681
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500682struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300683 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500684 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200685 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500686};
687
688static void
Scott Moreau447013d2012-02-18 05:05:29 -0700689resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100690 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500691{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500692 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100693 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400694 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500695 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100696 wl_fixed_t from_x, from_y;
697 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200698
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400699 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300700 return;
701
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400702 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100703 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100704 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400705 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100706 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500707
Daniel Stone103db7f2012-05-08 17:17:55 +0100708 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200709 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100710 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200711 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100712 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500713 }
714
Daniel Stone103db7f2012-05-08 17:17:55 +0100715 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200716 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100717 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200718 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100719 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500720 }
721
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400722 shsurf->client->send_configure(shsurf->surface,
723 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500724}
725
726static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400727send_configure(struct weston_surface *surface,
728 uint32_t edges, int32_t width, int32_t height)
729{
730 struct shell_surface *shsurf = get_shell_surface(surface);
731
732 wl_shell_surface_send_configure(&shsurf->resource,
733 edges, width, height);
734}
735
736static const struct weston_shell_client shell_client = {
737 send_configure
738};
739
740static void
Scott Moreau447013d2012-02-18 05:05:29 -0700741resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100742 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500743{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300744 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100745 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100746 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500747
Daniel Stone4dbadb12012-05-30 16:31:51 +0100748 if (pointer->button_count == 0 &&
749 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300750 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100751 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500752 free(grab);
753 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500754}
755
Scott Moreau447013d2012-02-18 05:05:29 -0700756static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500757 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500758 resize_grab_motion,
759 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500760};
761
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400762static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400763surface_resize(struct shell_surface *shsurf,
764 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500765{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500766 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500767
Alex Wu4539b082012-03-01 12:57:46 +0800768 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
769 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500770
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200771 if (edges == 0 || edges > 15 ||
772 (edges & 3) == 3 || (edges & 12) == 12)
773 return 0;
774
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500775 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400776 if (!resize)
777 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500778
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300779 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
780
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500781 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200782 resize->width = shsurf->surface->geometry.width;
783 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400784
Daniel Stone37816df2012-05-16 18:45:18 +0100785 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500786
Daniel Stone37816df2012-05-16 18:45:18 +0100787 wl_pointer_set_focus(ws->seat.pointer, NULL,
788 wl_fixed_from_int(0),
789 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400790
791 return 0;
792}
793
794static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200795shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100796 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400798{
Daniel Stone37816df2012-05-16 18:45:18 +0100799 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200800 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400801
Alex Wu4539b082012-03-01 12:57:46 +0800802 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
803 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400804
Daniel Stone37816df2012-05-16 18:45:18 +0100805 if (ws->seat.pointer->button_count == 0 ||
806 ws->seat.pointer->grab_serial != serial ||
807 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500808 return;
809
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400810 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400811 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500812}
813
Juan Zhao96879df2012-02-07 08:45:41 +0800814static struct weston_output *
815get_default_output(struct weston_compositor *compositor)
816{
817 return container_of(compositor->output_list.next,
818 struct weston_output, link);
819}
820
Alex Wu4539b082012-03-01 12:57:46 +0800821static void
822shell_unset_fullscreen(struct shell_surface *shsurf)
823{
824 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800825 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
826 shell_surface_is_top_fullscreen(shsurf)) {
827 weston_output_switch_mode(shsurf->fullscreen_output,
828 shsurf->fullscreen_output->origin);
829 }
Alex Wu4539b082012-03-01 12:57:46 +0800830 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
831 shsurf->fullscreen.framerate = 0;
832 wl_list_remove(&shsurf->fullscreen.transform.link);
833 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800834 if (shsurf->fullscreen.black_surface)
835 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800836 shsurf->fullscreen.black_surface = NULL;
837 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800838 weston_surface_set_position(shsurf->surface,
839 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800840 if (shsurf->saved_rotation_valid) {
841 wl_list_insert(&shsurf->surface->geometry.transformation_list,
842 &shsurf->rotation.transform.link);
843 shsurf->saved_rotation_valid = false;
844 }
Alex Wu4539b082012-03-01 12:57:46 +0800845}
846
Pekka Paalanen98262232011-12-01 10:42:22 +0200847static int
848reset_shell_surface_type(struct shell_surface *surface)
849{
850 switch (surface->type) {
851 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800852 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200853 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800854 case SHELL_SURFACE_MAXIMIZED:
855 surface->output = get_default_output(surface->surface->compositor);
856 weston_surface_set_position(surface->surface,
857 surface->saved_x,
858 surface->saved_y);
859 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200860 case SHELL_SURFACE_PANEL:
861 case SHELL_SURFACE_BACKGROUND:
862 wl_list_remove(&surface->link);
863 wl_list_init(&surface->link);
864 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200865 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200866 case SHELL_SURFACE_LOCK:
867 wl_resource_post_error(&surface->resource,
868 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200869 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200870 return -1;
871 case SHELL_SURFACE_NONE:
872 case SHELL_SURFACE_TOPLEVEL:
873 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500874 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200875 break;
876 }
877
878 surface->type = SHELL_SURFACE_NONE;
879 return 0;
880}
881
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500882static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400883set_surface_type(struct shell_surface *shsurf)
884{
885 struct weston_surface *surface = shsurf->surface;
886 struct shell_surface *pshsurf = shsurf->parent;
887 struct weston_surface *pes;
888 struct shell_surface *priv;
889 struct desktop_shell *shell = shsurf->shell;
890
891 reset_shell_surface_type(shsurf);
892
893 shsurf->type = shsurf->next_type;
894 shsurf->next_type = SHELL_SURFACE_NONE;
895
896 switch (shsurf->type) {
897 case SHELL_SURFACE_TOPLEVEL:
898 break;
899 case SHELL_SURFACE_TRANSIENT:
900 pes = pshsurf->surface;
901 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300902 pes->geometry.x + shsurf->transient.x,
903 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400904 break;
905
906 case SHELL_SURFACE_MAXIMIZED:
907 shsurf->saved_x = surface->geometry.x;
908 shsurf->saved_y = surface->geometry.y;
909 shsurf->saved_position_valid = true;
910 break;
911
912 case SHELL_SURFACE_FULLSCREEN:
913 shsurf->saved_x = surface->geometry.x;
914 shsurf->saved_y = surface->geometry.y;
915 shsurf->saved_position_valid = true;
916
917 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
918 wl_list_remove(&shsurf->rotation.transform.link);
919 wl_list_init(&shsurf->rotation.transform.link);
920 shsurf->surface->geometry.dirty = 1;
921 shsurf->saved_rotation_valid = true;
922 }
923 break;
924
925 case SHELL_SURFACE_BACKGROUND:
926 wl_list_for_each(priv, &shell->backgrounds, link) {
927 if (priv->output == shsurf->output) {
928 priv->surface->output = NULL;
929 wl_list_remove(&priv->surface->layer_link);
930 wl_list_remove(&priv->link);
931 break;
932 }
933 }
934
935 wl_list_insert(&shell->backgrounds, &shsurf->link);
936
937 weston_surface_set_position(surface, shsurf->output->x,
938 shsurf->output->y);
939 break;
940
941 case SHELL_SURFACE_PANEL:
942 wl_list_for_each(priv, &shell->panels, link) {
943 if (priv->output == shsurf->output) {
944 priv->surface->output = NULL;
945 wl_list_remove(&priv->surface->layer_link);
946 wl_list_remove(&priv->link);
947 break;
948 }
949 }
950
951 wl_list_insert(&shell->panels, &shsurf->link);
952
953 weston_surface_set_position(surface, shsurf->output->x,
954 shsurf->output->y);
955 break;
956
957 default:
958 break;
959 }
960}
961
962static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300963set_toplevel(struct shell_surface *shsurf)
964{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400965 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300966}
967
968static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200969shell_surface_set_toplevel(struct wl_client *client,
970 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400971{
Pekka Paalanen98262232011-12-01 10:42:22 +0200972 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400973
Tiago Vignattibc052c92012-04-19 16:18:18 +0300974 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400975}
976
977static void
Tiago Vignatti491bac12012-05-18 16:37:43 -0400978set_transient(struct shell_surface *shsurf,
979 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
980{
981 /* assign to parents output */
982 shsurf->parent = pshsurf;
983 shsurf->transient.x = x;
984 shsurf->transient.y = y;
985 shsurf->transient.flags = flags;
986 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
987}
988
989static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200990shell_surface_set_transient(struct wl_client *client,
991 struct wl_resource *resource,
992 struct wl_resource *parent_resource,
993 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400994{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200995 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400996 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200997
Tiago Vignatti491bac12012-05-18 16:37:43 -0400998 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400999}
1000
Tiago Vignattibe143262012-04-16 17:31:41 +03001001static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001002shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001003{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001004 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001005}
1006
1007static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001008get_output_panel_height(struct desktop_shell *shell,
1009 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001010{
1011 struct shell_surface *priv;
1012 int panel_height = 0;
1013
1014 if (!output)
1015 return 0;
1016
Tiago Vignattibe143262012-04-16 17:31:41 +03001017 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +08001018 if (priv->output == output) {
1019 panel_height = priv->surface->geometry.height;
1020 break;
1021 }
1022 }
1023 return panel_height;
1024}
1025
1026static void
1027shell_surface_set_maximized(struct wl_client *client,
1028 struct wl_resource *resource,
1029 struct wl_resource *output_resource )
1030{
1031 struct shell_surface *shsurf = resource->data;
1032 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001033 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001034 uint32_t edges = 0, panel_height = 0;
1035
1036 /* get the default output, if the client set it as NULL
1037 check whether the ouput is available */
1038 if (output_resource)
1039 shsurf->output = output_resource->data;
1040 else
1041 shsurf->output = get_default_output(es->compositor);
1042
Tiago Vignattibe143262012-04-16 17:31:41 +03001043 shell = shell_surface_get_shell(shsurf);
1044 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001045 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001046
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001047 shsurf->client->send_configure(shsurf->surface, edges,
1048 es->output->current->width,
1049 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001050
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001051 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001052}
1053
Alex Wu21858432012-04-01 20:13:08 +08001054static void
1055black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1056
Alex Wu4539b082012-03-01 12:57:46 +08001057static struct weston_surface *
1058create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001059 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001060 GLfloat x, GLfloat y, int w, int h)
1061{
1062 struct weston_surface *surface = NULL;
1063
1064 surface = weston_surface_create(ec);
1065 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001066 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001067 return NULL;
1068 }
1069
Alex Wu21858432012-04-01 20:13:08 +08001070 surface->configure = black_surface_configure;
1071 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001072 weston_surface_configure(surface, x, y, w, h);
1073 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1074 return surface;
1075}
1076
1077/* Create black surface and append it to the associated fullscreen surface.
1078 * Handle size dismatch and positioning according to the method. */
1079static void
1080shell_configure_fullscreen(struct shell_surface *shsurf)
1081{
1082 struct weston_output *output = shsurf->fullscreen_output;
1083 struct weston_surface *surface = shsurf->surface;
1084 struct weston_matrix *matrix;
1085 float scale;
1086
1087 center_on_output(surface, output);
1088
1089 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001090 shsurf->fullscreen.black_surface =
1091 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001092 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001093 output->x, output->y,
1094 output->current->width,
1095 output->current->height);
1096
1097 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1098 wl_list_insert(&surface->layer_link,
1099 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001100 shsurf->fullscreen.black_surface->output = output;
1101
1102 switch (shsurf->fullscreen.type) {
1103 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1104 break;
1105 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1106 matrix = &shsurf->fullscreen.transform.matrix;
1107 weston_matrix_init(matrix);
1108 scale = (float)output->current->width/(float)surface->geometry.width;
1109 weston_matrix_scale(matrix, scale, scale, 1);
1110 wl_list_remove(&shsurf->fullscreen.transform.link);
1111 wl_list_insert(surface->geometry.transformation_list.prev,
1112 &shsurf->fullscreen.transform.link);
1113 weston_surface_set_position(surface, output->x, output->y);
1114 break;
1115 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001116 if (shell_surface_is_top_fullscreen(shsurf)) {
1117 struct weston_mode mode = {0,
1118 surface->geometry.width,
1119 surface->geometry.height,
1120 shsurf->fullscreen.framerate};
1121
1122 if (weston_output_switch_mode(output, &mode) == 0) {
1123 weston_surface_configure(shsurf->fullscreen.black_surface,
1124 output->x, output->y,
1125 output->current->width,
1126 output->current->height);
1127 weston_surface_set_position(surface, output->x, output->y);
1128 break;
1129 }
1130 }
Alex Wu4539b082012-03-01 12:57:46 +08001131 break;
1132 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1133 break;
1134 default:
1135 break;
1136 }
1137}
1138
1139/* make the fullscreen and black surface at the top */
1140static void
1141shell_stack_fullscreen(struct shell_surface *shsurf)
1142{
Alex Wubd3354b2012-04-17 17:20:49 +08001143 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001144 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001145 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001146
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001147 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001148 wl_list_insert(&shell->fullscreen_layer.surface_list,
1149 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001150 weston_surface_damage(surface);
1151
1152 if (!shsurf->fullscreen.black_surface)
1153 shsurf->fullscreen.black_surface =
1154 create_black_surface(surface->compositor,
1155 surface,
1156 output->x, output->y,
1157 output->current->width,
1158 output->current->height);
1159
1160 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001161 wl_list_insert(&surface->layer_link,
1162 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001163 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001164}
1165
1166static void
1167shell_map_fullscreen(struct shell_surface *shsurf)
1168{
Alex Wu4539b082012-03-01 12:57:46 +08001169 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001170 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001171}
1172
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001173static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001174shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001175 struct wl_resource *resource,
1176 uint32_t method,
1177 uint32_t framerate,
1178 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001179{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001180 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001181 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001182
1183 if (output_resource)
1184 shsurf->output = output_resource->data;
1185 else
1186 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001187
Alex Wu4539b082012-03-01 12:57:46 +08001188 shsurf->fullscreen_output = shsurf->output;
1189 shsurf->fullscreen.type = method;
1190 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001191 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001192
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001193 shsurf->client->send_configure(shsurf->surface, 0,
1194 shsurf->output->current->width,
1195 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001196}
1197
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001198static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001199popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001200 struct wl_surface *surface,
1201 wl_fixed_t x,
1202 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001203{
Daniel Stone37816df2012-05-16 18:45:18 +01001204 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001205 struct shell_surface *priv =
1206 container_of(grab, struct shell_surface, popup.grab);
1207 struct wl_client *client = priv->surface->surface.resource.client;
1208
Pekka Paalanencb108432012-01-19 16:25:40 +02001209 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001210 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001211 grab->focus = surface;
1212 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001213 wl_pointer_set_focus(pointer, NULL,
1214 wl_fixed_from_int(0),
1215 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001216 grab->focus = NULL;
1217 }
1218}
1219
1220static void
Scott Moreau447013d2012-02-18 05:05:29 -07001221popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001222 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001223{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001224 struct wl_resource *resource;
1225
Daniel Stone37816df2012-05-16 18:45:18 +01001226 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001227 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001228 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001229}
1230
1231static void
Scott Moreau447013d2012-02-18 05:05:29 -07001232popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001233 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001234{
1235 struct wl_resource *resource;
1236 struct shell_surface *shsurf =
1237 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001238 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001239 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001240 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001241
Daniel Stone37816df2012-05-16 18:45:18 +01001242 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001243 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001244 display = wl_client_get_display(resource->client);
1245 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001246 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001247 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001248 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001249 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001250 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001251 wl_pointer_end_grab(grab->pointer);
1252 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001253 }
1254
Daniel Stone4dbadb12012-05-30 16:31:51 +01001255 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001256 shsurf->popup.initial_up = 1;
1257}
1258
Scott Moreau447013d2012-02-18 05:05:29 -07001259static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001260 popup_grab_focus,
1261 popup_grab_motion,
1262 popup_grab_button,
1263};
1264
1265static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001266shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001267{
Daniel Stone37816df2012-05-16 18:45:18 +01001268 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001269 struct weston_surface *es = shsurf->surface;
1270 struct weston_surface *parent = shsurf->parent->surface;
1271
1272 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001273 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001274
Pekka Paalanen938269a2012-02-07 14:19:01 +02001275 weston_surface_update_transform(parent);
1276 if (parent->transform.enabled) {
1277 shsurf->popup.parent_transform.matrix =
1278 parent->transform.matrix;
1279 } else {
1280 /* construct x, y translation matrix */
1281 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1282 shsurf->popup.parent_transform.matrix.d[12] =
1283 parent->geometry.x;
1284 shsurf->popup.parent_transform.matrix.d[13] =
1285 parent->geometry.y;
1286 }
1287 wl_list_insert(es->geometry.transformation_list.prev,
1288 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001289 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001290
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001291 shsurf->popup.initial_up = 0;
1292
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001293 /* We don't require the grab to still be active, but if another
1294 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001295 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1296 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001297 } else {
1298 wl_shell_surface_send_popup_done(&shsurf->resource);
1299 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001300}
1301
1302static void
1303shell_surface_set_popup(struct wl_client *client,
1304 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001305 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001306 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001307 struct wl_resource *parent_resource,
1308 int32_t x, int32_t y, uint32_t flags)
1309{
1310 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001311
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001312 shsurf->type = SHELL_SURFACE_POPUP;
1313 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001314 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001315 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001316 shsurf->popup.x = x;
1317 shsurf->popup.y = y;
1318}
1319
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001320static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001321 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001322 shell_surface_move,
1323 shell_surface_resize,
1324 shell_surface_set_toplevel,
1325 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001326 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001327 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001328 shell_surface_set_maximized,
1329 shell_surface_set_title,
1330 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001331};
1332
1333static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001334destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001335{
Daniel Stone37816df2012-05-16 18:45:18 +01001336 if (shsurf->popup.grab.pointer)
1337 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001338
Alex Wubd3354b2012-04-17 17:20:49 +08001339 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1340 shell_surface_is_top_fullscreen(shsurf)) {
1341 weston_output_switch_mode(shsurf->fullscreen_output,
1342 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001343 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001344
Alex Wuaa08e2d2012-03-05 11:01:40 +08001345 if (shsurf->fullscreen.black_surface)
1346 weston_surface_destroy(shsurf->fullscreen.black_surface);
1347
Alex Wubd3354b2012-04-17 17:20:49 +08001348 /* As destroy_resource() use wl_list_for_each_safe(),
1349 * we can always remove the listener.
1350 */
1351 wl_list_remove(&shsurf->surface_destroy_listener.link);
1352 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001353 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001354
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001355 wl_list_remove(&shsurf->link);
1356 free(shsurf);
1357}
1358
1359static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001360shell_destroy_shell_surface(struct wl_resource *resource)
1361{
1362 struct shell_surface *shsurf = resource->data;
1363
1364 destroy_shell_surface(shsurf);
1365}
1366
1367static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001368shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001369{
1370 struct shell_surface *shsurf = container_of(listener,
1371 struct shell_surface,
1372 surface_destroy_listener);
1373
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001374 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001375 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001376 } else {
1377 wl_signal_emit(&shsurf->resource.destroy_signal,
1378 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001379 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001380 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001381}
1382
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001383static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001384get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001385{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001386 struct wl_listener *listener;
1387
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001388 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1389 shell_handle_surface_destroy);
1390 if (listener)
1391 return container_of(listener, struct shell_surface,
1392 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001393
1394 return NULL;
1395}
1396
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001397static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001398shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1399
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001400static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001401create_shell_surface(void *shell, struct weston_surface *surface,
1402 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001403{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001404 struct shell_surface *shsurf;
1405
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001406 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001407 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001408 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001409 }
1410
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001411 shsurf = calloc(1, sizeof *shsurf);
1412 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001413 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001414 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001415 }
1416
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001417 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001418 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001419
Tiago Vignattibc052c92012-04-19 16:18:18 +03001420 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001421 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001422 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001423 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001424 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001425 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1426 shsurf->fullscreen.framerate = 0;
1427 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001428 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001429 wl_list_init(&shsurf->fullscreen.transform.link);
1430
Tiago Vignattibc052c92012-04-19 16:18:18 +03001431 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001432 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1433 wl_signal_add(&surface->surface.resource.destroy_signal,
1434 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001435
1436 /* init link so its safe to always remove it in destroy_shell_surface */
1437 wl_list_init(&shsurf->link);
1438
Pekka Paalanen460099f2012-01-20 16:48:25 +02001439 /* empty when not in use */
1440 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001441 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001442
Pekka Paalanen98262232011-12-01 10:42:22 +02001443 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001444 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001445
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001446 shsurf->client = client;
1447
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001448 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001449}
1450
1451static void
1452shell_get_shell_surface(struct wl_client *client,
1453 struct wl_resource *resource,
1454 uint32_t id,
1455 struct wl_resource *surface_resource)
1456{
1457 struct weston_surface *surface = surface_resource->data;
1458 struct desktop_shell *shell = resource->data;
1459 struct shell_surface *shsurf;
1460
1461 if (get_shell_surface(surface)) {
1462 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001463 WL_DISPLAY_ERROR_INVALID_OBJECT,
1464 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001465 return;
1466 }
1467
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001468 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001469 if (!shsurf) {
1470 wl_resource_post_error(surface_resource,
1471 WL_DISPLAY_ERROR_INVALID_OBJECT,
1472 "surface->configure already set");
1473 return;
1474 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001475
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001476 shsurf->resource.destroy = shell_destroy_shell_surface;
1477 shsurf->resource.object.id = id;
1478 shsurf->resource.object.interface = &wl_shell_surface_interface;
1479 shsurf->resource.object.implementation =
1480 (void (**)(void)) &shell_surface_implementation;
1481 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001482
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001483 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001484}
1485
1486static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001487 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001488};
1489
Kristian Høgsberg07937562011-04-12 17:25:42 -04001490static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001491handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001492{
1493 proc->pid = 0;
1494}
1495
1496static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001497launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001498{
1499 if (shell->screensaver.binding)
1500 return;
1501
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001502 if (!shell->screensaver.path)
1503 return;
1504
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001505 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001506 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001507 return;
1508 }
1509
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001511 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001512 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001513 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001514}
1515
1516static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001517terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001518{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001519 if (shell->screensaver.process.pid == 0)
1520 return;
1521
1522 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001523}
1524
1525static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001526show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001527{
1528 struct wl_list *list;
1529
1530 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001531 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001532 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001533 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001534
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001535 wl_list_remove(&surface->surface->layer_link);
1536 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001537 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001538 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001539}
1540
1541static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001542hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001543{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001544 wl_list_remove(&surface->surface->layer_link);
1545 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001546 surface->surface->output = NULL;
1547}
1548
1549static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001550desktop_shell_set_background(struct wl_client *client,
1551 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001552 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001553 struct wl_resource *surface_resource)
1554{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001555 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001556
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001557 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001558 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001559
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001560 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001561 surface_resource,
1562 shsurf->output->current->width,
1563 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001564}
1565
1566static void
1567desktop_shell_set_panel(struct wl_client *client,
1568 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001569 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001570 struct wl_resource *surface_resource)
1571{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001572 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001573
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001574 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001575 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001576
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001577 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001578 surface_resource,
1579 shsurf->output->current->width,
1580 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001581}
1582
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001583static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001584handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001585{
Tiago Vignattibe143262012-04-16 17:31:41 +03001586 struct desktop_shell *shell =
1587 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001588
Martin Minarik6d118362012-06-07 18:01:59 +02001589 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001590 shell->lock_surface = NULL;
1591}
1592
1593static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001594desktop_shell_set_lock_surface(struct wl_client *client,
1595 struct wl_resource *resource,
1596 struct wl_resource *surface_resource)
1597{
Tiago Vignattibe143262012-04-16 17:31:41 +03001598 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001599 struct shell_surface *surface = surface_resource->data;
1600
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001601 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001602
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001603 if (!shell->locked)
1604 return;
1605
Pekka Paalanen98262232011-12-01 10:42:22 +02001606 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001607
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001608 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1609 wl_signal_add(&surface_resource->destroy_signal,
1610 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001611
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001612 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001613}
1614
1615static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001616resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001617{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001618 struct shell_surface *tmp;
1619
1620 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1621 hide_screensaver(shell, tmp);
1622
1623 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001624
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001625 wl_list_remove(&shell->lock_layer.link);
1626 wl_list_insert(&shell->compositor->cursor_layer.link,
1627 &shell->fullscreen_layer.link);
1628 wl_list_insert(&shell->fullscreen_layer.link,
1629 &shell->panel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001630
1631 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001632 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001633 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001634 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001635}
1636
1637static void
1638desktop_shell_unlock(struct wl_client *client,
1639 struct wl_resource *resource)
1640{
Tiago Vignattibe143262012-04-16 17:31:41 +03001641 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001642
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001643 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001644
1645 if (shell->locked)
1646 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001647}
1648
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001649static void
1650desktop_shell_set_busy_surface(struct wl_client *client,
1651 struct wl_resource *resource,
1652 struct wl_resource *surface_resource)
1653{
1654 struct desktop_shell *shell = resource->data;
1655
1656 shell->busy_surface = surface_resource->data;
1657}
1658
Kristian Høgsberg75840622011-09-06 13:48:16 -04001659static const struct desktop_shell_interface desktop_shell_implementation = {
1660 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001661 desktop_shell_set_panel,
1662 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001663 desktop_shell_unlock,
1664 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04001665};
1666
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001667static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001669{
1670 struct shell_surface *shsurf;
1671
1672 shsurf = get_shell_surface(surface);
1673 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001674 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001675 return shsurf->type;
1676}
1677
Kristian Høgsberg75840622011-09-06 13:48:16 -04001678static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001679move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001680{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001681 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001682 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001683 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001684
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001685 if (surface == NULL)
1686 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001687
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001688 shsurf = get_shell_surface(surface);
1689 if (shsurf == NULL)
1690 return;
1691
1692 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001693 case SHELL_SURFACE_PANEL:
1694 case SHELL_SURFACE_BACKGROUND:
1695 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001696 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001697 return;
1698 default:
1699 break;
1700 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001701
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001702 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001703}
1704
1705static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001706resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001707{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001709 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001710 uint32_t edges = 0;
1711 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001713
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001714 if (surface == NULL)
1715 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001716
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001717 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001718 if (!shsurf)
1719 return;
1720
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001721 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001722 case SHELL_SURFACE_PANEL:
1723 case SHELL_SURFACE_BACKGROUND:
1724 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001725 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001726 return;
1727 default:
1728 break;
1729 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001730
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001731 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001732 wl_fixed_to_int(seat->pointer->grab_x),
1733 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001734 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001735
Pekka Paalanen60921e52012-01-25 15:55:43 +02001736 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001737 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001738 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001739 edges |= 0;
1740 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001742
Pekka Paalanen60921e52012-01-25 15:55:43 +02001743 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001744 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001745 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001746 edges |= 0;
1747 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001748 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001749
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001750 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001751}
1752
1753static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001754surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001755 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001756{
Scott Moreau02709af2012-05-22 01:54:10 -06001757 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001758 struct shell_surface *shsurf;
1759 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001760 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001761
1762 if (surface == NULL)
1763 return;
1764
1765 shsurf = get_shell_surface(surface);
1766 if (!shsurf)
1767 return;
1768
1769 switch (shsurf->type) {
1770 case SHELL_SURFACE_BACKGROUND:
1771 case SHELL_SURFACE_SCREENSAVER:
1772 return;
1773 default:
1774 break;
1775 }
1776
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001777 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001778
Scott Moreau02709af2012-05-22 01:54:10 -06001779 if (surface->alpha > 1.0)
1780 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001781 if (surface->alpha < step)
1782 surface->alpha = step;
1783
1784 surface->geometry.dirty = 1;
1785 weston_surface_damage(surface);
1786}
1787
1788static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001789do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001790 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001791{
Daniel Stone37816df2012-05-16 18:45:18 +01001792 struct weston_seat *ws = (struct weston_seat *) seat;
1793 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001794 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06001795 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001796
1797 wl_list_for_each(output, &compositor->output_list, link) {
1798 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01001799 wl_fixed_to_double(seat->pointer->x),
1800 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001801 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01001802 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001803 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01001804 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001805 increment = -output->zoom.increment;
1806 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001807 increment = output->zoom.increment *
1808 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001809 else
1810 increment = 0;
1811
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04001812 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001813
Scott Moreaue6603982012-06-11 13:07:51 -06001814 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06001815 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06001816 else if (output->zoom.level > output->zoom.max_level)
1817 output->zoom.level = output->zoom.max_level;
1818 else
1819 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001820
Scott Moreaue6603982012-06-11 13:07:51 -06001821 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07001822
Daniel Stone37816df2012-05-16 18:45:18 +01001823 weston_output_update_zoom(output,
1824 seat->pointer->x,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001825 seat->pointer->y,
Scott Moreaue6603982012-06-11 13:07:51 -06001826 ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001827 }
1828 }
1829}
1830
Scott Moreauccbf29d2012-02-22 14:21:41 -07001831static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001832zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001833 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001834{
1835 do_zoom(seat, time, 0, axis, value);
1836}
1837
1838static void
1839zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1840 void *data)
1841{
1842 do_zoom(seat, time, key, 0, 0);
1843}
1844
1845static void
1846terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1847 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001848{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001849 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001850
Daniel Stone325fc2d2012-05-30 16:31:58 +01001851 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001852}
1853
1854static void
Scott Moreau447013d2012-02-18 05:05:29 -07001855rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001856 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001857{
1858 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001859 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001860 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001861 struct shell_surface *shsurf = rotate->base.shsurf;
1862 struct weston_surface *surface;
1863 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1864
1865 if (!shsurf)
1866 return;
1867
1868 surface = shsurf->surface;
1869
1870 cx = 0.5f * surface->geometry.width;
1871 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001872
Daniel Stone37816df2012-05-16 18:45:18 +01001873 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1874 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001875 r = sqrtf(dx * dx + dy * dy);
1876
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001877 wl_list_remove(&shsurf->rotation.transform.link);
1878 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001879
1880 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001881 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001882 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001883
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001884 weston_matrix_init(&rotate->rotation);
1885 rotate->rotation.d[0] = dx / r;
1886 rotate->rotation.d[4] = -dy / r;
1887 rotate->rotation.d[1] = -rotate->rotation.d[4];
1888 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001889
1890 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001891 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001892 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001893 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001894 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001895
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001896 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001897 &shsurf->surface->geometry.transformation_list,
1898 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001899 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001900 wl_list_init(&shsurf->rotation.transform.link);
1901 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001902 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001903 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001904
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001905 /* We need to adjust the position of the surface
1906 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001907 cposx = surface->geometry.x + cx;
1908 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001909 dposx = rotate->center.x - cposx;
1910 dposy = rotate->center.y - cposy;
1911 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001912 weston_surface_set_position(surface,
1913 surface->geometry.x + dposx,
1914 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001915 }
1916
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001917 /* Repaint implies weston_surface_update_transform(), which
1918 * lazily applies the damage due to rotation update.
1919 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001920 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001921}
1922
1923static void
Scott Moreau447013d2012-02-18 05:05:29 -07001924rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001925 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001926{
1927 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001928 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001929 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001930 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001931 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001932
Daniel Stone4dbadb12012-05-30 16:31:51 +01001933 if (pointer->button_count == 0 &&
1934 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001935 if (shsurf)
1936 weston_matrix_multiply(&shsurf->rotation.rotation,
1937 &rotate->rotation);
1938 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001939 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001940 free(rotate);
1941 }
1942}
1943
Scott Moreau447013d2012-02-18 05:05:29 -07001944static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001945 noop_grab_focus,
1946 rotate_grab_motion,
1947 rotate_grab_button,
1948};
1949
1950static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001951rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01001952 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001953{
1954 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001955 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001956 struct shell_surface *surface;
1957 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001958 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001959 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001960
1961 if (base_surface == NULL)
1962 return;
1963
1964 surface = get_shell_surface(base_surface);
1965 if (!surface)
1966 return;
1967
1968 switch (surface->type) {
1969 case SHELL_SURFACE_PANEL:
1970 case SHELL_SURFACE_BACKGROUND:
1971 case SHELL_SURFACE_FULLSCREEN:
1972 case SHELL_SURFACE_SCREENSAVER:
1973 return;
1974 default:
1975 break;
1976 }
1977
Pekka Paalanen460099f2012-01-20 16:48:25 +02001978 rotate = malloc(sizeof *rotate);
1979 if (!rotate)
1980 return;
1981
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001982 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001983
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04001984 weston_surface_to_global_float(surface->surface,
1985 surface->surface->geometry.width / 2,
1986 surface->surface->geometry.height / 2,
1987 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001988
Daniel Stone37816df2012-05-16 18:45:18 +01001989 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001990
Daniel Stone37816df2012-05-16 18:45:18 +01001991 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1992 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001993 r = sqrtf(dx * dx + dy * dy);
1994 if (r > 20.0f) {
1995 struct weston_matrix inverse;
1996
1997 weston_matrix_init(&inverse);
1998 inverse.d[0] = dx / r;
1999 inverse.d[4] = dy / r;
2000 inverse.d[1] = -inverse.d[4];
2001 inverse.d[5] = inverse.d[0];
2002 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002003
2004 weston_matrix_init(&rotate->rotation);
2005 rotate->rotation.d[0] = dx / r;
2006 rotate->rotation.d[4] = -dy / r;
2007 rotate->rotation.d[1] = -rotate->rotation.d[4];
2008 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002009 } else {
2010 weston_matrix_init(&surface->rotation.rotation);
2011 weston_matrix_init(&rotate->rotation);
2012 }
2013
Daniel Stone37816df2012-05-16 18:45:18 +01002014 wl_pointer_set_focus(seat->pointer, NULL,
2015 wl_fixed_from_int(0),
2016 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002017}
2018
2019static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002020activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002021 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002022{
Alex Wu21858432012-04-01 20:13:08 +08002023 struct weston_surface *surf, *prev;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002024 struct workspace *ws;
2025 struct weston_layer *ws_layer;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002026
Daniel Stone37816df2012-05-16 18:45:18 +01002027 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002028
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002029 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002030 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002031 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002032 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002033 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002034
Pekka Paalanen77346a62011-11-30 16:26:35 +02002035 case SHELL_SURFACE_SCREENSAVER:
2036 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002037 if (shell->lock_surface)
2038 weston_surface_restack(es,
2039 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002040 break;
Alex Wu4539b082012-03-01 12:57:46 +08002041 case SHELL_SURFACE_FULLSCREEN:
2042 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002043 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002044 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002045 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002046 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002047 ws = get_current_workspace(shell);
2048 ws_layer = &ws->layer;
2049
Alex Wubd3354b2012-04-17 17:20:49 +08002050 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08002051 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
2052 wl_list_for_each_reverse_safe(surf,
2053 prev,
2054 &shell->fullscreen_layer.surface_list,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002055 layer_link) {
Alex Wu21858432012-04-01 20:13:08 +08002056 weston_surface_restack(surf,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002057 &ws_layer->surface_list);
2058 }
Alex Wu21858432012-04-01 20:13:08 +08002059 }
2060
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002061 weston_surface_restack(es, &ws_layer->surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002062 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002063 }
2064}
2065
Alex Wu21858432012-04-01 20:13:08 +08002066/* no-op func for checking black surface */
2067static void
2068black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2069{
2070}
2071
2072static bool
2073is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2074{
2075 if (es->configure == black_surface_configure) {
2076 if (fs_surface)
2077 *fs_surface = (struct weston_surface *)es->private;
2078 return true;
2079 }
2080 return false;
2081}
2082
Kristian Høgsberg75840622011-09-06 13:48:16 -04002083static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002084click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002085 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002086{
Daniel Stone37816df2012-05-16 18:45:18 +01002087 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002088 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002089 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002090 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002091
Daniel Stone37816df2012-05-16 18:45:18 +01002092 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002093 if (!focus)
2094 return;
2095
Alex Wu21858432012-04-01 20:13:08 +08002096 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002097 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002098
Daniel Stone325fc2d2012-05-30 16:31:58 +01002099 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002100 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002101}
2102
2103static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002104lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002105{
Tiago Vignattibe143262012-04-16 17:31:41 +03002106 struct desktop_shell *shell =
2107 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01002108 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002109 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002110 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002111
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002112 if (shell->locked) {
2113 wl_list_for_each(output, &shell->compositor->output_list, link)
2114 /* TODO: find a way to jump to other DPMS levels */
2115 if (output->set_dpms)
2116 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002117 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002118 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002119
2120 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002121
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002122 /* Hide all surfaces by removing the fullscreen, panel and
2123 * toplevel layers. This way nothing else can show or receive
2124 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002125
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002126 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002127 wl_list_remove(&shell->fullscreen_layer.link);
2128 wl_list_insert(&shell->compositor->cursor_layer.link,
2129 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002130
Pekka Paalanen77346a62011-11-30 16:26:35 +02002131 launch_screensaver(shell);
2132
2133 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2134 show_screensaver(shell, shsurf);
2135
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002136 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002137 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002138 weston_compositor_wake(shell->compositor);
2139 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002140 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002141
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002142 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002143 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002144
2145 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01002146 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2147 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002148 }
2149
2150 /* TODO: disable bindings that should not work while locked. */
2151
2152 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002153}
2154
2155static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002156unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002157{
Tiago Vignattibe143262012-04-16 17:31:41 +03002158 struct desktop_shell *shell =
2159 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002160
Pekka Paalanend81c2162011-11-16 13:47:34 +02002161 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002162 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002163 return;
2164 }
2165
2166 /* If desktop-shell client has gone away, unlock immediately. */
2167 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002168 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002169 return;
2170 }
2171
2172 if (shell->prepare_event_sent)
2173 return;
2174
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002175 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002176 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002177}
2178
2179static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002180center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002181{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002182 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002183 GLfloat x = (mode->width - surface->geometry.width) / 2;
2184 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002185
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002186 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002187}
2188
2189static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002190map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002191 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002192{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002193 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002194 struct shell_surface *shsurf = get_shell_surface(surface);
2195 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002196 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002197 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002198 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002199 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002200
Pekka Paalanen60921e52012-01-25 15:55:43 +02002201 surface->geometry.width = width;
2202 surface->geometry.height = height;
2203 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002204
2205 /* initial positioning, see also configure() */
2206 switch (surface_type) {
2207 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002208 weston_surface_set_position(surface, 10 + random() % 400,
2209 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002210 break;
2211 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002212 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002213 break;
Alex Wu4539b082012-03-01 12:57:46 +08002214 case SHELL_SURFACE_FULLSCREEN:
2215 shell_map_fullscreen(shsurf);
2216 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002217 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002218 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002219 panel_height = get_output_panel_height(shell,surface->output);
2220 weston_surface_set_position(surface, surface->output->x,
2221 surface->output->y + panel_height);
2222 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002223 case SHELL_SURFACE_LOCK:
2224 center_on_output(surface, get_default_output(compositor));
2225 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002226 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002227 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002228 case SHELL_SURFACE_NONE:
2229 weston_surface_set_position(surface,
2230 surface->geometry.x + sx,
2231 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002232 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002233 default:
2234 ;
2235 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002236
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002237 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002238 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002239 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002240 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002241 wl_list_insert(&shell->background_layer.surface_list,
2242 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002243 break;
2244 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002245 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002246 wl_list_insert(&shell->panel_layer.surface_list,
2247 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002248 break;
2249 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002250 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002251 wl_list_insert(&shell->lock_layer.surface_list,
2252 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002253 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002254 break;
2255 case SHELL_SURFACE_SCREENSAVER:
2256 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002257 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002258 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002259 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002260 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002261 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002262 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002263 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002264 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002265 case SHELL_SURFACE_POPUP:
2266 case SHELL_SURFACE_TRANSIENT:
2267 parent = shsurf->parent->surface;
2268 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2269 break;
Alex Wu4539b082012-03-01 12:57:46 +08002270 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002271 case SHELL_SURFACE_NONE:
2272 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002273 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002274 ws = get_current_workspace(shell);
2275 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002276 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002277 }
2278
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002279 if (surface_type != SHELL_SURFACE_NONE) {
2280 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002281 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2282 surface->output = shsurf->output;
2283 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002284
Juan Zhao7bb92f02011-12-15 11:31:51 -05002285 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002286 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002287 if (shsurf->transient.flags ==
2288 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2289 break;
2290 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002291 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002292 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002293 if (!shell->locked) {
2294 wl_list_for_each(seat, &compositor->seat_list, link)
2295 activate(shell, surface, seat);
2296 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002297 break;
2298 default:
2299 break;
2300 }
2301
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002302 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002303 {
2304 switch (shell->win_animation_type) {
2305 case ANIMATION_FADE:
2306 weston_fade_run(surface, NULL, NULL);
2307 break;
2308 case ANIMATION_ZOOM:
2309 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2310 break;
2311 default:
2312 break;
2313 }
2314 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002315}
2316
2317static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002318configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002319 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002320{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002321 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2322 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002323
Pekka Paalanen77346a62011-11-30 16:26:35 +02002324 shsurf = get_shell_surface(surface);
2325 if (shsurf)
2326 surface_type = shsurf->type;
2327
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002328 surface->geometry.x = x;
2329 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002330 surface->geometry.width = width;
2331 surface->geometry.height = height;
2332 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002333
2334 switch (surface_type) {
2335 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002336 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002337 break;
Alex Wu4539b082012-03-01 12:57:46 +08002338 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002339 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002340 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002341 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002342 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002343 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002344 surface->geometry.x = surface->output->x;
2345 surface->geometry.y = surface->output->y +
2346 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002347 break;
Alex Wu4539b082012-03-01 12:57:46 +08002348 case SHELL_SURFACE_TOPLEVEL:
2349 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002350 default:
2351 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002352 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002353
Alex Wu4539b082012-03-01 12:57:46 +08002354 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002355 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002356 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002357
2358 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2359 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002360 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2361 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002362 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002363}
2364
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002365static void
2366shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2367{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002368 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002369 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002370 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002371
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002372 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002373 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002374 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002375 type_changed = 1;
2376 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002377
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002378 if (!weston_surface_is_mapped(es)) {
2379 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002380 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002381 es->geometry.width != es->buffer->width ||
2382 es->geometry.height != es->buffer->height) {
2383 GLfloat from_x, from_y;
2384 GLfloat to_x, to_y;
2385
2386 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2387 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2388 configure(shell, es,
2389 es->geometry.x + to_x - from_x,
2390 es->geometry.y + to_y - from_y,
2391 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002392 }
2393}
2394
Tiago Vignattibe143262012-04-16 17:31:41 +03002395static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002396
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002397static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002398desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002399{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002400 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002401 struct desktop_shell *shell =
2402 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002403
2404 shell->child.process.pid = 0;
2405 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002406
2407 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2408 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002409 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002410 shell->child.deathstamp = time;
2411 shell->child.deathcount = 0;
2412 }
2413
2414 shell->child.deathcount++;
2415 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002416 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002417 return;
2418 }
2419
Martin Minarik6d118362012-06-07 18:01:59 +02002420 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002421 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002422}
2423
2424static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002425launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002426{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002427 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002428
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002429 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002430 &shell->child.process,
2431 shell_exe,
2432 desktop_shell_sigchld);
2433
2434 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002435 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002436 return 0;
2437}
2438
2439static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002440bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2441{
Tiago Vignattibe143262012-04-16 17:31:41 +03002442 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002443
2444 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002445 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002446}
2447
Kristian Høgsberg75840622011-09-06 13:48:16 -04002448static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002449unbind_desktop_shell(struct wl_resource *resource)
2450{
Tiago Vignattibe143262012-04-16 17:31:41 +03002451 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002452
2453 if (shell->locked)
2454 resume_desktop(shell);
2455
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002456 shell->child.desktop_shell = NULL;
2457 shell->prepare_event_sent = false;
2458 free(resource);
2459}
2460
2461static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002462bind_desktop_shell(struct wl_client *client,
2463 void *data, uint32_t version, uint32_t id)
2464{
Tiago Vignattibe143262012-04-16 17:31:41 +03002465 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002466 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002467
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002468 resource = wl_client_add_object(client, &desktop_shell_interface,
2469 &desktop_shell_implementation,
2470 id, shell);
2471
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002472 if (client == shell->child.client) {
2473 resource->destroy = unbind_desktop_shell;
2474 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002475 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002476 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002477
2478 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2479 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002480 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002481}
2482
Pekka Paalanen6e168112011-11-24 11:34:05 +02002483static void
2484screensaver_set_surface(struct wl_client *client,
2485 struct wl_resource *resource,
2486 struct wl_resource *shell_surface_resource,
2487 struct wl_resource *output_resource)
2488{
Tiago Vignattibe143262012-04-16 17:31:41 +03002489 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002490 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002491 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002492
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002493 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002494
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002495 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002496 surface->output = output;
2497 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002498}
2499
2500static const struct screensaver_interface screensaver_implementation = {
2501 screensaver_set_surface
2502};
2503
2504static void
2505unbind_screensaver(struct wl_resource *resource)
2506{
Tiago Vignattibe143262012-04-16 17:31:41 +03002507 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002508
Pekka Paalanen77346a62011-11-30 16:26:35 +02002509 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002510 free(resource);
2511}
2512
2513static void
2514bind_screensaver(struct wl_client *client,
2515 void *data, uint32_t version, uint32_t id)
2516{
Tiago Vignattibe143262012-04-16 17:31:41 +03002517 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002518 struct wl_resource *resource;
2519
2520 resource = wl_client_add_object(client, &screensaver_interface,
2521 &screensaver_implementation,
2522 id, shell);
2523
Pekka Paalanen77346a62011-11-30 16:26:35 +02002524 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002525 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002526 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002527 return;
2528 }
2529
2530 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2531 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002532 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002533}
2534
Kristian Høgsberg07045392012-02-19 18:52:44 -05002535struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002536 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002537 struct weston_surface *current;
2538 struct wl_listener listener;
2539 struct wl_keyboard_grab grab;
2540};
2541
2542static void
2543switcher_next(struct switcher *switcher)
2544{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002545 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002546 struct weston_surface *surface;
2547 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002548 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002549
2550 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002551 switch (get_shell_surface_type(surface)) {
2552 case SHELL_SURFACE_TOPLEVEL:
2553 case SHELL_SURFACE_FULLSCREEN:
2554 case SHELL_SURFACE_MAXIMIZED:
2555 if (first == NULL)
2556 first = surface;
2557 if (prev == switcher->current)
2558 next = surface;
2559 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002560 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002561 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002562 weston_surface_damage(surface);
2563 break;
2564 default:
2565 break;
2566 }
Alex Wu1659daa2012-04-01 20:13:09 +08002567
2568 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002569 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002570 surface->geometry.dirty = 1;
2571 weston_surface_damage(surface);
2572 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002573 }
2574
2575 if (next == NULL)
2576 next = first;
2577
Alex Wu07b26062012-03-12 16:06:01 +08002578 if (next == NULL)
2579 return;
2580
Kristian Høgsberg07045392012-02-19 18:52:44 -05002581 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002582 wl_signal_add(&next->surface.resource.destroy_signal,
2583 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002584
2585 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002586 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002587
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002588 shsurf = get_shell_surface(switcher->current);
2589 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002590 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002591}
2592
2593static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002594switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002595{
2596 struct switcher *switcher =
2597 container_of(listener, struct switcher, listener);
2598
2599 switcher_next(switcher);
2600}
2601
2602static void
Daniel Stone351eb612012-05-31 15:27:47 -04002603switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002604{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002605 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002606 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002607 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002608
2609 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002610 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002611 weston_surface_damage(surface);
2612 }
2613
Alex Wu07b26062012-03-12 16:06:01 +08002614 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002615 activate(switcher->shell, switcher->current,
2616 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002617 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002618 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002619 free(switcher);
2620}
2621
2622static void
2623switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002624 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002625{
2626 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002627 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002628
Daniel Stonec9785ea2012-05-30 16:31:52 +01002629 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002630 switcher_next(switcher);
2631}
2632
2633static void
2634switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2635 uint32_t mods_depressed, uint32_t mods_latched,
2636 uint32_t mods_locked, uint32_t group)
2637{
2638 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002639 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002640
Daniel Stone351eb612012-05-31 15:27:47 -04002641 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
2642 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002643}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002644
2645static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04002646 switcher_key,
2647 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05002648};
2649
2650static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002651switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2652 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002653{
Tiago Vignattibe143262012-04-16 17:31:41 +03002654 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002655 struct switcher *switcher;
2656
2657 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002658 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002659 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002660 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002661 wl_list_init(&switcher->listener.link);
2662
2663 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002664 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2665 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002666 switcher_next(switcher);
2667}
2668
Pekka Paalanen3c647232011-12-22 13:43:43 +02002669static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002670backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2671 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002672{
2673 struct weston_compositor *compositor = data;
2674 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002675 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002676
2677 /* TODO: we're limiting to simple use cases, where we assume just
2678 * control on the primary display. We'd have to extend later if we
2679 * ever get support for setting backlights on random desktop LCD
2680 * panels though */
2681 output = get_default_output(compositor);
2682 if (!output)
2683 return;
2684
2685 if (!output->set_backlight)
2686 return;
2687
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002688 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2689 backlight_new = output->backlight_current - 25;
2690 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2691 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002692
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002693 if (backlight_new < 5)
2694 backlight_new = 5;
2695 if (backlight_new > 255)
2696 backlight_new = 255;
2697
2698 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002699 output->set_backlight(output, output->backlight_current);
2700}
2701
2702static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002703debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2704 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002705{
Tiago Vignattibe143262012-04-16 17:31:41 +03002706 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002707 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002708 struct weston_surface *surface;
2709
2710 if (shell->debug_repaint_surface) {
2711 weston_surface_destroy(shell->debug_repaint_surface);
2712 shell->debug_repaint_surface = NULL;
2713 } else {
2714 surface = weston_surface_create(compositor);
2715 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2716 weston_surface_configure(surface, 0, 0, 8192, 8192);
2717 wl_list_insert(&compositor->fade_layer.surface_list,
2718 &surface->layer_link);
2719 weston_surface_assign_output(surface);
2720 pixman_region32_init(&surface->input);
2721
2722 /* Here's the dirty little trick that makes the
2723 * repaint debugging work: we force an
2724 * update_transform first to update dependent state
2725 * and clear the geometry.dirty bit. Then we clear
2726 * the surface damage so it only gets repainted
2727 * piecewise as we repaint other things. */
2728
2729 weston_surface_update_transform(surface);
2730 pixman_region32_fini(&surface->damage);
2731 pixman_region32_init(&surface->damage);
2732 shell->debug_repaint_surface = surface;
2733 }
2734}
2735
2736static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002737force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2738 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002739{
2740 struct wl_client *client;
2741 pid_t pid;
2742 uid_t uid;
2743 gid_t gid;
2744
Daniel Stone325fc2d2012-05-30 16:31:58 +01002745 client = seat->keyboard->focus->resource.client;
2746 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002747
Daniel Stone325fc2d2012-05-30 16:31:58 +01002748 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002749}
2750
2751static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002752workspace_up_binding(struct wl_seat *seat, uint32_t time,
2753 uint32_t key, void *data)
2754{
2755 struct desktop_shell *shell = data;
2756 unsigned int new_index = shell->workspaces.current;
2757
2758 if (new_index != 0)
2759 new_index--;
2760
2761 change_workspace(shell, new_index);
2762}
2763
2764static void
2765workspace_down_binding(struct wl_seat *seat, uint32_t time,
2766 uint32_t key, void *data)
2767{
2768 struct desktop_shell *shell = data;
2769 unsigned int new_index = shell->workspaces.current;
2770
2771 if (new_index < shell->workspaces.num - 1)
2772 new_index++;
2773
2774 change_workspace(shell, new_index);
2775}
2776
2777static void
2778workspace_f_binding(struct wl_seat *seat, uint32_t time,
2779 uint32_t key, void *data)
2780{
2781 struct desktop_shell *shell = data;
2782 unsigned int new_index;
2783
2784 new_index = key - KEY_F1;
2785 if (new_index >= shell->workspaces.num)
2786 new_index = shell->workspaces.num - 1;
2787
2788 change_workspace(shell, new_index);
2789}
2790
2791
2792static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002793shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002794{
Tiago Vignattibe143262012-04-16 17:31:41 +03002795 struct desktop_shell *shell =
2796 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002797 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002798
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002799 if (shell->child.client)
2800 wl_client_destroy(shell->child.client);
2801
Kristian Høgsberg88c16072012-05-16 08:04:19 -04002802 wl_list_remove(&shell->lock_listener.link);
2803 wl_list_remove(&shell->unlock_listener.link);
2804
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002805 wl_array_for_each(ws, &shell->workspaces.array)
2806 workspace_destroy(*ws);
2807 wl_array_release(&shell->workspaces.array);
2808
Pekka Paalanen3c647232011-12-22 13:43:43 +02002809 free(shell->screensaver.path);
2810 free(shell);
2811}
2812
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002813static void
2814shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2815{
2816 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002817 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002818
2819 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01002820 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
2821 MODIFIER_CTRL | MODIFIER_ALT,
2822 terminate_binding, ec);
2823 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
2824 click_to_activate_binding,
2825 shell);
2826 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2827 MODIFIER_SUPER | MODIFIER_ALT,
2828 surface_opacity_binding, NULL);
2829 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2830 MODIFIER_SUPER, zoom_axis_binding,
2831 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002832
2833 /* configurable bindings */
2834 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002835 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
2836 zoom_key_binding, NULL);
2837 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
2838 zoom_key_binding, NULL);
2839 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
2840 shell);
2841 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
2842 resize_binding, shell);
2843 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
2844 rotate_binding, NULL);
2845 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
2846 shell);
2847 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
2848 ec);
2849 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
2850 backlight_binding, ec);
2851 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
2852 ec);
2853 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
2854 backlight_binding, ec);
2855 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
2856 debug_repaint_binding, shell);
2857 weston_compositor_add_key_binding(ec, KEY_K, mod,
2858 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002859 weston_compositor_add_key_binding(ec, KEY_UP, mod,
2860 workspace_up_binding, shell);
2861 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
2862 workspace_down_binding, shell);
2863
2864 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
2865 if (shell->workspaces.num > 1) {
2866 num_workspace_bindings = shell->workspaces.num;
2867 if (num_workspace_bindings > 6)
2868 num_workspace_bindings = 6;
2869 for (i = 0; i < num_workspace_bindings; i++)
2870 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
2871 workspace_f_binding,
2872 shell);
2873 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002874}
2875
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002876int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002877shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002878
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002879WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002880shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002881{
Tiago Vignattibe143262012-04-16 17:31:41 +03002882 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002883 struct workspace **pws;
2884 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002885
2886 shell = malloc(sizeof *shell);
2887 if (shell == NULL)
2888 return -1;
2889
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002890 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002891 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002892
2893 shell->destroy_listener.notify = shell_destroy;
2894 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2895 shell->lock_listener.notify = lock;
2896 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2897 shell->unlock_listener.notify = unlock;
2898 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002899 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002900 ec->shell_interface.create_shell_surface = create_shell_surface;
2901 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04002902 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002903 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002904 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002905
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002906 wl_list_init(&shell->backgrounds);
2907 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002908 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002909
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002910 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2911 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002912 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
2913 weston_layer_init(&shell->lock_layer, NULL);
2914
2915 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002916
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002917 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002918
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002919 for (i = 0; i < shell->workspaces.num; i++) {
2920 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
2921 if (pws == NULL)
2922 return -1;
2923
2924 *pws = workspace_create();
2925 if (*pws == NULL)
2926 return -1;
2927 }
2928 activate_workspace(shell, 0);
2929
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002930 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2931 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002932 return -1;
2933
Kristian Høgsberg75840622011-09-06 13:48:16 -04002934 if (wl_display_add_global(ec->wl_display,
2935 &desktop_shell_interface,
2936 shell, bind_desktop_shell) == NULL)
2937 return -1;
2938
Pekka Paalanen6e168112011-11-24 11:34:05 +02002939 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2940 shell, bind_screensaver) == NULL)
2941 return -1;
2942
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002943 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002944 if (launch_desktop_shell_process(shell) != 0)
2945 return -1;
2946
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002947 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03002948 if (ec->seat->seat.pointer)
2949 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
2950 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002951
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002952 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002953
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002954 return 0;
2955}