blob: 2bd23fc674565850e1294e6446af28d0da259d70 [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"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050039
Jonas Ådahle3cddce2012-06-13 00:01:22 +020040#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020041#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042
Juan Zhaoe10d2792012-04-25 19:09:52 +080043enum animation_type {
44 ANIMATION_NONE,
45
46 ANIMATION_ZOOM,
47 ANIMATION_FADE
48};
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Philipp Brüschweiler88013572012-08-06 13:44:42 +020066struct input_panel_surface {
67 struct wl_list link;
68 struct weston_surface *surface;
69 struct wl_listener listener;
70};
71
Tiago Vignattibe143262012-04-16 17:31:41 +030072struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050073 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040074
75 struct wl_listener lock_listener;
76 struct wl_listener unlock_listener;
77 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020078 struct wl_listener show_input_panel_listener;
79 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020080
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050081 struct weston_layer fullscreen_layer;
82 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050083 struct weston_layer background_layer;
84 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020085 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050086
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040087 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030088 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040089
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020092 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020093 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020094
95 unsigned deathcount;
96 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020098
99 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200100 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200101 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200102
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400103 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500104 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100105
Pekka Paalanen77346a62011-11-30 16:26:35 +0200106 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200107 struct wl_array array;
108 unsigned int current;
109 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200110
111 struct weston_animation animation;
112 int anim_dir;
113 uint32_t anim_timestamp;
114 double anim_current;
115 struct workspace *anim_from;
116 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200117 } workspaces;
118
119 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200120 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200121 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200122 struct wl_resource *binding;
123 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200125 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct {
128 struct wl_resource *binding;
129 struct wl_list surfaces;
130 } input_panel;
131
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300132 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800133 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500134 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400135};
136
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500137enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200138 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500139 SHELL_SURFACE_TOPLEVEL,
140 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800142 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200144};
145
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146struct ping_timer {
147 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148 uint32_t serial;
149};
150
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200152 struct wl_resource resource;
153
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200155 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400156 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300157 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200158
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400159 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400160 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500161 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800162 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800163 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600164 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100165
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500166 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500168 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 } rotation;
170
171 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700172 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200174 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100176 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400177 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500178 } popup;
179
Alex Wu4539b082012-03-01 12:57:46 +0800180 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300181 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400182 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 } transient;
184
185 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800186 enum wl_shell_surface_fullscreen_method type;
187 struct weston_transform transform; /* matrix from x, y */
188 uint32_t framerate;
189 struct weston_surface *black_surface;
190 } fullscreen;
191
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 struct ping_timer *ping_timer;
193
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200194 struct weston_transform workspace_transform;
195
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500196 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100198 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400199
200 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200201};
202
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300203struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700204 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205 struct shell_surface *shsurf;
206 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300207 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300208};
209
210struct weston_move_grab {
211 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100212 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500213};
214
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300216 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500217 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200218 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400219 GLfloat x;
220 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200221 } center;
222};
223
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400224static void
225activate(struct desktop_shell *shell, struct weston_surface *es,
226 struct weston_seat *seat);
227
228static struct workspace *
229get_current_workspace(struct desktop_shell *shell);
230
Alex Wubd3354b2012-04-17 17:20:49 +0800231static struct shell_surface *
232get_shell_surface(struct weston_surface *surface);
233
234static struct desktop_shell *
235shell_surface_get_shell(struct shell_surface *shsurf);
236
237static bool
238shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
239{
240 struct desktop_shell *shell;
241 struct weston_surface *top_fs_es;
242
243 shell = shell_surface_get_shell(shsurf);
244
245 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
246 return false;
247
248 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
249 struct weston_surface,
250 layer_link);
251 return (shsurf == get_shell_surface(top_fs_es));
252}
253
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500254static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400255destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300256{
257 struct shell_grab *grab;
258
259 grab = container_of(listener, struct shell_grab,
260 shsurf_destroy_listener);
261
262 grab->shsurf = NULL;
263}
264
265static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300266shell_grab_start(struct shell_grab *grab,
267 const struct wl_pointer_grab_interface *interface,
268 struct shell_surface *shsurf,
269 struct wl_pointer *pointer,
270 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300272 struct desktop_shell *shell = shsurf->shell;
273
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274 grab->grab.interface = interface;
275 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400276 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
277 wl_signal_add(&shsurf->resource.destroy_signal,
278 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300280 grab->pointer = pointer;
281 grab->grab.focus = &shsurf->surface->surface;
282
283 wl_pointer_start_grab(pointer, &grab->grab);
284 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
285 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
286 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300287}
288
289static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300290shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400292 if (grab->shsurf)
293 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300294
295 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300296}
297
298static void
Alex Wu4539b082012-03-01 12:57:46 +0800299center_on_output(struct weston_surface *surface,
300 struct weston_output *output);
301
Daniel Stone496ca172012-05-30 16:31:42 +0100302static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300303get_modifier(char *modifier)
304{
305 if (!modifier)
306 return MODIFIER_SUPER;
307
308 if (!strcmp("ctrl", modifier))
309 return MODIFIER_CTRL;
310 else if (!strcmp("alt", modifier))
311 return MODIFIER_ALT;
312 else if (!strcmp("super", modifier))
313 return MODIFIER_SUPER;
314 else
315 return MODIFIER_SUPER;
316}
317
Juan Zhaoe10d2792012-04-25 19:09:52 +0800318static enum animation_type
319get_animation_type(char *animation)
320{
321 if (!animation)
322 return ANIMATION_NONE;
323
324 if (!strcmp("zoom", animation))
325 return ANIMATION_ZOOM;
326 else if (!strcmp("fade", animation))
327 return ANIMATION_FADE;
328 else
329 return ANIMATION_NONE;
330}
331
Alex Wu4539b082012-03-01 12:57:46 +0800332static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300333shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200335 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200336 char *path = NULL;
337 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200338 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300339 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800340 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300343 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800344 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200345 { "num-workspaces",
346 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200347 };
348
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400349 struct config_key saver_keys[] = {
350 { "path", CONFIG_KEY_STRING, &path },
351 { "duration", CONFIG_KEY_INTEGER, &duration },
352 };
353
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200354 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400355 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200356 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
357 };
358
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200359 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500360 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200361 free(config_file);
362
Pekka Paalanen7296e792011-12-07 16:22:00 +0200363 shell->screensaver.path = path;
364 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300365 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800366 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200367 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
368}
369
370static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200371focus_state_destroy(struct focus_state *state)
372{
373 wl_list_remove(&state->seat_destroy_listener.link);
374 wl_list_remove(&state->surface_destroy_listener.link);
375 free(state);
376}
377
378static void
379focus_state_seat_destroy(struct wl_listener *listener, void *data)
380{
381 struct focus_state *state = container_of(listener,
382 struct focus_state,
383 seat_destroy_listener);
384
385 wl_list_remove(&state->link);
386 focus_state_destroy(state);
387}
388
389static void
390focus_state_surface_destroy(struct wl_listener *listener, void *data)
391{
392 struct focus_state *state = container_of(listener,
393 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400394 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400395 struct desktop_shell *shell;
396 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200397
Kristian Høgsberge3778222012-07-31 17:29:30 -0400398 next = NULL;
399 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
400 if (surface == state->keyboard_focus)
401 continue;
402
403 next = surface;
404 break;
405 }
406
407 if (next) {
408 shell = state->seat->compositor->shell_interface.shell;
409 activate(shell, next, state->seat);
410 } else {
411 wl_list_remove(&state->link);
412 focus_state_destroy(state);
413 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200414}
415
416static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400417focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200418{
Jonas Ådahl04769742012-06-13 00:01:24 +0200419 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200420
421 state = malloc(sizeof *state);
422 if (state == NULL)
423 return NULL;
424
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400425 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200426 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400427 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200428
429 state->seat_destroy_listener.notify = focus_state_seat_destroy;
430 state->surface_destroy_listener.notify = focus_state_surface_destroy;
431 wl_signal_add(&seat->seat.destroy_signal,
432 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400433 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200434
435 return state;
436}
437
438static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400439restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200440{
441 struct focus_state *state, *next;
442
443 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
444 if (state->keyboard_focus)
445 wl_keyboard_set_focus(state->seat->seat.keyboard,
446 &state->keyboard_focus->surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200447 }
448}
449
450static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200451workspace_destroy(struct workspace *ws)
452{
Jonas Ådahl04769742012-06-13 00:01:24 +0200453 struct focus_state *state, *next;
454
455 wl_list_for_each_safe(state, next, &ws->focus_list, link)
456 focus_state_destroy(state);
457
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200458 free(ws);
459}
460
Jonas Ådahl04769742012-06-13 00:01:24 +0200461static void
462seat_destroyed(struct wl_listener *listener, void *data)
463{
464 struct weston_seat *seat = data;
465 struct focus_state *state, *next;
466 struct workspace *ws = container_of(listener,
467 struct workspace,
468 seat_destroyed_listener);
469
470 wl_list_for_each_safe(state, next, &ws->focus_list, link)
471 if (state->seat == seat)
472 wl_list_remove(&state->link);
473}
474
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200475static struct workspace *
476workspace_create(void)
477{
478 struct workspace *ws = malloc(sizeof *ws);
479 if (ws == NULL)
480 return NULL;
481
482 weston_layer_init(&ws->layer, NULL);
483
Jonas Ådahl04769742012-06-13 00:01:24 +0200484 wl_list_init(&ws->focus_list);
485 wl_list_init(&ws->seat_destroyed_listener.link);
486 ws->seat_destroyed_listener.notify = seat_destroyed;
487
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200488 return ws;
489}
490
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200491static int
492workspace_is_empty(struct workspace *ws)
493{
494 return wl_list_empty(&ws->layer.surface_list);
495}
496
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200497static struct workspace *
498get_workspace(struct desktop_shell *shell, unsigned int index)
499{
500 struct workspace **pws = shell->workspaces.array.data;
501 pws += index;
502 return *pws;
503}
504
505static struct workspace *
506get_current_workspace(struct desktop_shell *shell)
507{
508 return get_workspace(shell, shell->workspaces.current);
509}
510
511static void
512activate_workspace(struct desktop_shell *shell, unsigned int index)
513{
514 struct workspace *ws;
515
516 ws = get_workspace(shell, index);
517 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
518
519 shell->workspaces.current = index;
520}
521
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200522static unsigned int
523get_output_height(struct weston_output *output)
524{
525 return abs(output->region.extents.y1 - output->region.extents.y2);
526}
527
528static void
529surface_translate(struct weston_surface *surface, double d)
530{
531 struct shell_surface *shsurf = get_shell_surface(surface);
532 struct weston_transform *transform;
533
534 transform = &shsurf->workspace_transform;
535 if (wl_list_empty(&transform->link))
536 wl_list_insert(surface->geometry.transformation_list.prev,
537 &shsurf->workspace_transform.link);
538
539 weston_matrix_init(&shsurf->workspace_transform.matrix);
540 weston_matrix_translate(&shsurf->workspace_transform.matrix,
541 0.0, d, 0.0);
542 surface->geometry.dirty = 1;
543}
544
545static void
546workspace_translate_out(struct workspace *ws, double fraction)
547{
548 struct weston_surface *surface;
549 unsigned int height;
550 double d;
551
552 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
553 height = get_output_height(surface->output);
554 d = height * fraction;
555
556 surface_translate(surface, d);
557 }
558}
559
560static void
561workspace_translate_in(struct workspace *ws, double fraction)
562{
563 struct weston_surface *surface;
564 unsigned int height;
565 double d;
566
567 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
568 height = get_output_height(surface->output);
569
570 if (fraction > 0)
571 d = -(height - height * fraction);
572 else
573 d = height + height * fraction;
574
575 surface_translate(surface, d);
576 }
577}
578
579static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200580reverse_workspace_change_animation(struct desktop_shell *shell,
581 unsigned int index,
582 struct workspace *from,
583 struct workspace *to)
584{
585 shell->workspaces.current = index;
586
587 shell->workspaces.anim_to = to;
588 shell->workspaces.anim_from = from;
589 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
590 shell->workspaces.anim_timestamp = 0;
591
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400592 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200593
Scott Moreau4272e632012-08-13 09:58:41 -0600594 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200595}
596
597static void
598workspace_deactivate_transforms(struct workspace *ws)
599{
600 struct weston_surface *surface;
601 struct shell_surface *shsurf;
602
603 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
604 shsurf = get_shell_surface(surface);
605 wl_list_remove(&shsurf->workspace_transform.link);
606 wl_list_init(&shsurf->workspace_transform.link);
607 shsurf->surface->geometry.dirty = 1;
608 }
609}
610
611static void
612finish_workspace_change_animation(struct desktop_shell *shell,
613 struct workspace *from,
614 struct workspace *to)
615{
Scott Moreau4272e632012-08-13 09:58:41 -0600616 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200617
618 wl_list_remove(&shell->workspaces.animation.link);
619 workspace_deactivate_transforms(from);
620 workspace_deactivate_transforms(to);
621 shell->workspaces.anim_to = NULL;
622
623 wl_list_remove(&shell->workspaces.anim_from->layer.link);
624}
625
626static void
627animate_workspace_change_frame(struct weston_animation *animation,
628 struct weston_output *output, uint32_t msecs)
629{
630 struct desktop_shell *shell =
631 container_of(animation, struct desktop_shell,
632 workspaces.animation);
633 struct workspace *from = shell->workspaces.anim_from;
634 struct workspace *to = shell->workspaces.anim_to;
635 uint32_t t;
636 double x, y;
637
638 if (workspace_is_empty(from) && workspace_is_empty(to)) {
639 finish_workspace_change_animation(shell, from, to);
640 return;
641 }
642
643 if (shell->workspaces.anim_timestamp == 0) {
644 if (shell->workspaces.anim_current == 0.0)
645 shell->workspaces.anim_timestamp = msecs;
646 else
647 shell->workspaces.anim_timestamp =
648 msecs -
649 /* Invers of movement function 'y' below. */
650 (asin(1.0 - shell->workspaces.anim_current) *
651 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
652 M_2_PI);
653 }
654
655 t = msecs - shell->workspaces.anim_timestamp;
656
657 /*
658 * x = [0, π/2]
659 * y(x) = sin(x)
660 */
661 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
662 y = sin(x);
663
664 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600665 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200666
667 workspace_translate_out(from, shell->workspaces.anim_dir * y);
668 workspace_translate_in(to, shell->workspaces.anim_dir * y);
669 shell->workspaces.anim_current = y;
670
Scott Moreau4272e632012-08-13 09:58:41 -0600671 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200672 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200673 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200674 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200675}
676
677static void
678animate_workspace_change(struct desktop_shell *shell,
679 unsigned int index,
680 struct workspace *from,
681 struct workspace *to)
682{
683 struct weston_output *output;
684
685 int dir;
686
687 if (index > shell->workspaces.current)
688 dir = -1;
689 else
690 dir = 1;
691
692 shell->workspaces.current = index;
693
694 shell->workspaces.anim_dir = dir;
695 shell->workspaces.anim_from = from;
696 shell->workspaces.anim_to = to;
697 shell->workspaces.anim_current = 0.0;
698 shell->workspaces.anim_timestamp = 0;
699
700 output = container_of(shell->compositor->output_list.next,
701 struct weston_output, link);
702 wl_list_insert(&output->animation_list,
703 &shell->workspaces.animation.link);
704
705 wl_list_insert(&from->layer.link, &to->layer.link);
706
707 workspace_translate_in(to, 0);
708
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400709 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200710
Scott Moreau4272e632012-08-13 09:58:41 -0600711 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200712}
713
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200714static void
715change_workspace(struct desktop_shell *shell, unsigned int index)
716{
717 struct workspace *from;
718 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200719
720 if (index == shell->workspaces.current)
721 return;
722
723 /* Don't change workspace when there is any fullscreen surfaces. */
724 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
725 return;
726
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200727 from = get_current_workspace(shell);
728 to = get_workspace(shell, index);
729
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200730 if (shell->workspaces.anim_from == to &&
731 shell->workspaces.anim_to == from) {
732 reverse_workspace_change_animation(shell, index, from, to);
733 return;
734 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200735
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200736 if (shell->workspaces.anim_to != NULL)
737 finish_workspace_change_animation(shell,
738 shell->workspaces.anim_from,
739 shell->workspaces.anim_to);
740
741 if (workspace_is_empty(to) && workspace_is_empty(from)) {
742 shell->workspaces.current = index;
743 wl_list_insert(&from->layer.link, &to->layer.link);
744 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200745
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400746 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200747 }
748 else
749 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200750}
751
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200752static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400753noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100754 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500755{
756 grab->focus = NULL;
757}
758
759static void
Scott Moreau447013d2012-02-18 05:05:29 -0700760move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100761 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500762{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500763 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100764 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300765 struct shell_surface *shsurf = move->base.shsurf;
766 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100767 int dx = wl_fixed_to_int(pointer->x + move->dx);
768 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300769
770 if (!shsurf)
771 return;
772
773 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500774
Daniel Stone103db7f2012-05-08 17:17:55 +0100775 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200776 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400777
778 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500779}
780
781static void
Scott Moreau447013d2012-02-18 05:05:29 -0700782move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100783 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500784{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300785 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
786 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100787 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100788 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500789
Daniel Stone4dbadb12012-05-30 16:31:51 +0100790 if (pointer->button_count == 0 &&
791 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300792 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500793 free(grab);
794 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500795}
796
Scott Moreau447013d2012-02-18 05:05:29 -0700797static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500798 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500799 move_grab_motion,
800 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500801};
802
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -0400803static int
804surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
805{
806 struct weston_move_grab *move;
807
808 if (!shsurf)
809 return -1;
810
811 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
812 return 0;
813
814 move = malloc(sizeof *move);
815 if (!move)
816 return -1;
817
818 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
819 ws->seat.pointer->grab_x;
820 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
821 ws->seat.pointer->grab_y;
822
823 shell_grab_start(&move->base, &move_grab_interface, shsurf,
824 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
825
826 return 0;
827}
828
829static void
830shell_surface_move(struct wl_client *client, struct wl_resource *resource,
831 struct wl_resource *seat_resource, uint32_t serial)
832{
833 struct weston_seat *ws = seat_resource->data;
834 struct shell_surface *shsurf = resource->data;
835
836 if (ws->seat.pointer->button_count == 0 ||
837 ws->seat.pointer->grab_serial != serial ||
838 ws->seat.pointer->focus != &shsurf->surface->surface)
839 return;
840
841 if (surface_move(shsurf, ws) < 0)
842 wl_resource_post_no_memory(resource);
843}
844
845struct weston_resize_grab {
846 struct shell_grab base;
847 uint32_t edges;
848 int32_t width, height;
849};
850
851static void
852resize_grab_motion(struct wl_pointer_grab *grab,
853 uint32_t time, wl_fixed_t x, wl_fixed_t y)
854{
855 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
856 struct wl_pointer *pointer = grab->pointer;
857 struct shell_surface *shsurf = resize->base.shsurf;
858 int32_t width, height;
859 wl_fixed_t from_x, from_y;
860 wl_fixed_t to_x, to_y;
861
862 if (!shsurf)
863 return;
864
865 weston_surface_from_global_fixed(shsurf->surface,
866 pointer->grab_x, pointer->grab_y,
867 &from_x, &from_y);
868 weston_surface_from_global_fixed(shsurf->surface,
869 pointer->x, pointer->y, &to_x, &to_y);
870
871 width = resize->width;
872 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
873 width += wl_fixed_to_int(from_x - to_x);
874 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
875 width += wl_fixed_to_int(to_x - from_x);
876 }
877
878 height = resize->height;
879 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
880 height += wl_fixed_to_int(from_y - to_y);
881 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
882 height += wl_fixed_to_int(to_y - from_y);
883 }
884
885 shsurf->client->send_configure(shsurf->surface,
886 resize->edges, width, height);
887}
888
889static void
890send_configure(struct weston_surface *surface,
891 uint32_t edges, int32_t width, int32_t height)
892{
893 struct shell_surface *shsurf = get_shell_surface(surface);
894
895 wl_shell_surface_send_configure(&shsurf->resource,
896 edges, width, height);
897}
898
899static const struct weston_shell_client shell_client = {
900 send_configure
901};
902
903static void
904resize_grab_button(struct wl_pointer_grab *grab,
905 uint32_t time, uint32_t button, uint32_t state_w)
906{
907 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
908 struct wl_pointer *pointer = grab->pointer;
909 enum wl_pointer_button_state state = state_w;
910
911 if (pointer->button_count == 0 &&
912 state == WL_POINTER_BUTTON_STATE_RELEASED) {
913 shell_grab_end(&resize->base);
914 free(grab);
915 }
916}
917
918static const struct wl_pointer_grab_interface resize_grab_interface = {
919 noop_grab_focus,
920 resize_grab_motion,
921 resize_grab_button,
922};
923
924static int
925surface_resize(struct shell_surface *shsurf,
926 struct weston_seat *ws, uint32_t edges)
927{
928 struct weston_resize_grab *resize;
929
930 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
931 return 0;
932
933 if (edges == 0 || edges > 15 ||
934 (edges & 3) == 3 || (edges & 12) == 12)
935 return 0;
936
937 resize = malloc(sizeof *resize);
938 if (!resize)
939 return -1;
940
941 resize->edges = edges;
942 resize->width = shsurf->surface->geometry.width;
943 resize->height = shsurf->surface->geometry.height;
944
945 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
946 ws->seat.pointer, edges);
947
948 return 0;
949}
950
951static void
952shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
953 struct wl_resource *seat_resource, uint32_t serial,
954 uint32_t edges)
955{
956 struct weston_seat *ws = seat_resource->data;
957 struct shell_surface *shsurf = resource->data;
958
959 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
960 return;
961
962 if (ws->seat.pointer->button_count == 0 ||
963 ws->seat.pointer->grab_serial != serial ||
964 ws->seat.pointer->focus != &shsurf->surface->surface)
965 return;
966
967 if (surface_resize(shsurf, ws, edges) < 0)
968 wl_resource_post_no_memory(resource);
969}
970
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600971static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400972busy_cursor_grab_focus(struct wl_pointer_grab *base,
973 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600974{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400975 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600976
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400977 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300978 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400979 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600980 }
981}
982
983static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400984busy_cursor_grab_motion(struct wl_pointer_grab *grab,
985 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600986{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400987}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600988
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400989static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -0400990busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400991 uint32_t time, uint32_t button, uint32_t state)
992{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -0400993 struct shell_grab *grab = (struct shell_grab *) base;
994 struct shell_surface *shsurf;
995 struct weston_surface *surface =
996 (struct weston_surface *) grab->grab.pointer->current;
997 struct weston_seat *seat =
998 (struct weston_seat *) grab->grab.pointer->seat;
999
1000 shsurf = get_shell_surface(surface);
1001 if (shsurf && button == BTN_LEFT && state) {
1002 activate(shsurf->shell, shsurf->surface, seat);
1003 surface_move(shsurf, seat);
1004 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001005}
1006
1007static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1008 busy_cursor_grab_focus,
1009 busy_cursor_grab_motion,
1010 busy_cursor_grab_button,
1011};
1012
1013static void
1014set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1015{
1016 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001017
1018 grab = malloc(sizeof *grab);
1019 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001020 return;
1021
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001022 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1023 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001024}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001025
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001026static void
1027end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1028{
1029 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1030
1031 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001032 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001033 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001034 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001035}
1036
Scott Moreau9521d5e2012-04-19 13:06:17 -06001037static void
1038ping_timer_destroy(struct shell_surface *shsurf)
1039{
1040 if (!shsurf || !shsurf->ping_timer)
1041 return;
1042
1043 if (shsurf->ping_timer->source)
1044 wl_event_source_remove(shsurf->ping_timer->source);
1045
1046 free(shsurf->ping_timer);
1047 shsurf->ping_timer = NULL;
1048}
1049
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001050static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001051ping_timeout_handler(void *data)
1052{
1053 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001054 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001055
Scott Moreau9521d5e2012-04-19 13:06:17 -06001056 /* Client is not responding */
1057 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001058
1059 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1060 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1061 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001062
1063 return 1;
1064}
1065
1066static void
1067ping_handler(struct weston_surface *surface, uint32_t serial)
1068{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001069 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001070 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001071 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001072
1073 if (!shsurf)
1074 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001075 if (!shsurf->resource.client)
1076 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001077
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001078 if (shsurf->surface == shsurf->shell->grab_surface)
1079 return;
1080
Scott Moreauff1db4a2012-04-17 19:06:18 -06001081 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001082 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001083 if (!shsurf->ping_timer)
1084 return;
1085
1086 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001087 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1088 shsurf->ping_timer->source =
1089 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1090 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1091
1092 wl_shell_surface_send_ping(&shsurf->resource, serial);
1093 }
1094}
1095
1096static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001097handle_pointer_focus(struct wl_listener *listener, void *data)
1098{
1099 struct wl_pointer *pointer = data;
1100 struct weston_surface *surface =
1101 (struct weston_surface *) pointer->focus;
1102 struct weston_compositor *compositor;
1103 struct shell_surface *shsurf;
1104 uint32_t serial;
1105
1106 if (!surface)
1107 return;
1108
1109 compositor = surface->compositor;
1110 shsurf = get_shell_surface(surface);
1111
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001112 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001113 set_busy_cursor(shsurf, pointer);
1114 } else {
1115 serial = wl_display_next_serial(compositor->wl_display);
1116 ping_handler(surface, serial);
1117 }
1118}
1119
1120static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001121create_pointer_focus_listener(struct weston_seat *seat)
1122{
1123 struct wl_listener *listener;
1124
1125 if (!seat->seat.pointer)
1126 return;
1127
1128 listener = malloc(sizeof *listener);
1129 listener->notify = handle_pointer_focus;
1130 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1131}
1132
1133static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001134shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1135 uint32_t serial)
1136{
1137 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001138 struct desktop_shell *shell = shsurf->shell;
1139 struct weston_seat *seat;
1140 struct weston_compositor *ec = shsurf->surface->compositor;
1141 struct wl_pointer *pointer;
1142 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001143
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001144 if (shsurf->ping_timer == NULL)
1145 /* Just ignore unsolicited pong. */
1146 return;
1147
Scott Moreauff1db4a2012-04-17 19:06:18 -06001148 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001149 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001150 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001151 if (was_unresponsive) {
1152 /* Received pong from previously unresponsive client */
1153 wl_list_for_each(seat, &ec->seat_list, link) {
1154 pointer = seat->seat.pointer;
1155 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001156 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001157 pointer->current ==
1158 &shsurf->surface->surface)
1159 end_busy_cursor(shsurf, pointer);
1160 }
1161 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001162 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001163 }
1164}
1165
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001166static void
1167shell_surface_set_title(struct wl_client *client,
1168 struct wl_resource *resource, const char *title)
1169{
1170 struct shell_surface *shsurf = resource->data;
1171
1172 free(shsurf->title);
1173 shsurf->title = strdup(title);
1174}
1175
1176static void
1177shell_surface_set_class(struct wl_client *client,
1178 struct wl_resource *resource, const char *class)
1179{
1180 struct shell_surface *shsurf = resource->data;
1181
1182 free(shsurf->class);
1183 shsurf->class = strdup(class);
1184}
1185
Juan Zhao96879df2012-02-07 08:45:41 +08001186static struct weston_output *
1187get_default_output(struct weston_compositor *compositor)
1188{
1189 return container_of(compositor->output_list.next,
1190 struct weston_output, link);
1191}
1192
Alex Wu4539b082012-03-01 12:57:46 +08001193static void
1194shell_unset_fullscreen(struct shell_surface *shsurf)
1195{
1196 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001197 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1198 shell_surface_is_top_fullscreen(shsurf)) {
1199 weston_output_switch_mode(shsurf->fullscreen_output,
1200 shsurf->fullscreen_output->origin);
1201 }
Alex Wu4539b082012-03-01 12:57:46 +08001202 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1203 shsurf->fullscreen.framerate = 0;
1204 wl_list_remove(&shsurf->fullscreen.transform.link);
1205 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001206 if (shsurf->fullscreen.black_surface)
1207 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001208 shsurf->fullscreen.black_surface = NULL;
1209 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001210 weston_surface_set_position(shsurf->surface,
1211 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001212 if (shsurf->saved_rotation_valid) {
1213 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1214 &shsurf->rotation.transform.link);
1215 shsurf->saved_rotation_valid = false;
1216 }
Alex Wu4539b082012-03-01 12:57:46 +08001217}
1218
Pekka Paalanen98262232011-12-01 10:42:22 +02001219static int
1220reset_shell_surface_type(struct shell_surface *surface)
1221{
1222 switch (surface->type) {
1223 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001224 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001225 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001226 case SHELL_SURFACE_MAXIMIZED:
1227 surface->output = get_default_output(surface->surface->compositor);
1228 weston_surface_set_position(surface->surface,
1229 surface->saved_x,
1230 surface->saved_y);
1231 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001232 case SHELL_SURFACE_NONE:
1233 case SHELL_SURFACE_TOPLEVEL:
1234 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001235 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001236 break;
1237 }
1238
1239 surface->type = SHELL_SURFACE_NONE;
1240 return 0;
1241}
1242
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001243static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001244set_surface_type(struct shell_surface *shsurf)
1245{
1246 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001247 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001248
1249 reset_shell_surface_type(shsurf);
1250
1251 shsurf->type = shsurf->next_type;
1252 shsurf->next_type = SHELL_SURFACE_NONE;
1253
1254 switch (shsurf->type) {
1255 case SHELL_SURFACE_TOPLEVEL:
1256 break;
1257 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001258 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001259 pes->geometry.x + shsurf->transient.x,
1260 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001261 break;
1262
1263 case SHELL_SURFACE_MAXIMIZED:
1264 shsurf->saved_x = surface->geometry.x;
1265 shsurf->saved_y = surface->geometry.y;
1266 shsurf->saved_position_valid = true;
1267 break;
1268
1269 case SHELL_SURFACE_FULLSCREEN:
1270 shsurf->saved_x = surface->geometry.x;
1271 shsurf->saved_y = surface->geometry.y;
1272 shsurf->saved_position_valid = true;
1273
1274 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1275 wl_list_remove(&shsurf->rotation.transform.link);
1276 wl_list_init(&shsurf->rotation.transform.link);
1277 shsurf->surface->geometry.dirty = 1;
1278 shsurf->saved_rotation_valid = true;
1279 }
1280 break;
1281
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001282 default:
1283 break;
1284 }
1285}
1286
1287static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001288set_toplevel(struct shell_surface *shsurf)
1289{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001290 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001291}
1292
1293static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001294shell_surface_set_toplevel(struct wl_client *client,
1295 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001296{
Pekka Paalanen98262232011-12-01 10:42:22 +02001297 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001298
Tiago Vignattibc052c92012-04-19 16:18:18 +03001299 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001300}
1301
1302static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001303set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001304 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001305{
1306 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001307 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001308 shsurf->transient.x = x;
1309 shsurf->transient.y = y;
1310 shsurf->transient.flags = flags;
1311 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1312}
1313
1314static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001315shell_surface_set_transient(struct wl_client *client,
1316 struct wl_resource *resource,
1317 struct wl_resource *parent_resource,
1318 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001319{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001320 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001321 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001322
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001323 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001324}
1325
Tiago Vignattibe143262012-04-16 17:31:41 +03001326static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001327shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001328{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001329 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001330}
1331
1332static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001333get_output_panel_height(struct desktop_shell *shell,
1334 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001335{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001336 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001337 int panel_height = 0;
1338
1339 if (!output)
1340 return 0;
1341
Juan Zhao4ab94682012-07-09 22:24:09 -07001342 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001343 if (surface->output == output) {
1344 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001345 break;
1346 }
1347 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001348
Juan Zhao96879df2012-02-07 08:45:41 +08001349 return panel_height;
1350}
1351
1352static void
1353shell_surface_set_maximized(struct wl_client *client,
1354 struct wl_resource *resource,
1355 struct wl_resource *output_resource )
1356{
1357 struct shell_surface *shsurf = resource->data;
1358 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001359 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001360 uint32_t edges = 0, panel_height = 0;
1361
1362 /* get the default output, if the client set it as NULL
1363 check whether the ouput is available */
1364 if (output_resource)
1365 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001366 else if (es->output)
1367 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001368 else
1369 shsurf->output = get_default_output(es->compositor);
1370
Tiago Vignattibe143262012-04-16 17:31:41 +03001371 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001372 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001373 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001374
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001375 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001376 shsurf->output->width,
1377 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001378
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001379 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001380}
1381
Alex Wu21858432012-04-01 20:13:08 +08001382static void
1383black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1384
Alex Wu4539b082012-03-01 12:57:46 +08001385static struct weston_surface *
1386create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001387 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001388 GLfloat x, GLfloat y, int w, int h)
1389{
1390 struct weston_surface *surface = NULL;
1391
1392 surface = weston_surface_create(ec);
1393 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001394 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001395 return NULL;
1396 }
1397
Alex Wu21858432012-04-01 20:13:08 +08001398 surface->configure = black_surface_configure;
1399 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001400 weston_surface_configure(surface, x, y, w, h);
1401 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001402 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1403
Alex Wu4539b082012-03-01 12:57:46 +08001404 return surface;
1405}
1406
1407/* Create black surface and append it to the associated fullscreen surface.
1408 * Handle size dismatch and positioning according to the method. */
1409static void
1410shell_configure_fullscreen(struct shell_surface *shsurf)
1411{
1412 struct weston_output *output = shsurf->fullscreen_output;
1413 struct weston_surface *surface = shsurf->surface;
1414 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001415 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001416
Alex Wu4539b082012-03-01 12:57:46 +08001417 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001418 shsurf->fullscreen.black_surface =
1419 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001420 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001421 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001422 output->width,
1423 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001424
1425 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1426 wl_list_insert(&surface->layer_link,
1427 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001428 shsurf->fullscreen.black_surface->output = output;
1429
1430 switch (shsurf->fullscreen.type) {
1431 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001432 if (surface->buffer)
1433 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001434 break;
1435 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1436 matrix = &shsurf->fullscreen.transform.matrix;
1437 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001438
Scott Moreau1bad5db2012-08-18 01:04:05 -06001439 output_aspect = (float) output->width /
1440 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001441 surface_aspect = (float) surface->geometry.width /
1442 (float) surface->geometry.height;
1443 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001444 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001445 (float) surface->geometry.width;
1446 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001447 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001448 (float) surface->geometry.height;
1449
Alex Wu4539b082012-03-01 12:57:46 +08001450 weston_matrix_scale(matrix, scale, scale, 1);
1451 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001452 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001453 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001454 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1455 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001456 weston_surface_set_position(surface, x, y);
1457
Alex Wu4539b082012-03-01 12:57:46 +08001458 break;
1459 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001460 if (shell_surface_is_top_fullscreen(shsurf)) {
1461 struct weston_mode mode = {0,
1462 surface->geometry.width,
1463 surface->geometry.height,
1464 shsurf->fullscreen.framerate};
1465
1466 if (weston_output_switch_mode(output, &mode) == 0) {
1467 weston_surface_configure(shsurf->fullscreen.black_surface,
1468 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001469 output->width,
1470 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001471 weston_surface_set_position(surface, output->x, output->y);
1472 break;
1473 }
1474 }
Alex Wu4539b082012-03-01 12:57:46 +08001475 break;
1476 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1477 break;
1478 default:
1479 break;
1480 }
1481}
1482
1483/* make the fullscreen and black surface at the top */
1484static void
1485shell_stack_fullscreen(struct shell_surface *shsurf)
1486{
Alex Wubd3354b2012-04-17 17:20:49 +08001487 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001488 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001489 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001490
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001491 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001492 wl_list_insert(&shell->fullscreen_layer.surface_list,
1493 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001494 weston_surface_damage(surface);
1495
1496 if (!shsurf->fullscreen.black_surface)
1497 shsurf->fullscreen.black_surface =
1498 create_black_surface(surface->compositor,
1499 surface,
1500 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001501 output->width,
1502 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001503
1504 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001505 wl_list_insert(&surface->layer_link,
1506 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001507 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001508}
1509
1510static void
1511shell_map_fullscreen(struct shell_surface *shsurf)
1512{
Alex Wu4539b082012-03-01 12:57:46 +08001513 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001514 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001515}
1516
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001517static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001518shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001519 struct wl_resource *resource,
1520 uint32_t method,
1521 uint32_t framerate,
1522 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001523{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001524 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001525 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001526
1527 if (output_resource)
1528 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001529 else if (es->output)
1530 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001531 else
1532 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001533
Alex Wu4539b082012-03-01 12:57:46 +08001534 shsurf->fullscreen_output = shsurf->output;
1535 shsurf->fullscreen.type = method;
1536 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001537 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001538
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001539 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001540 shsurf->output->width,
1541 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001542}
1543
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001545popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001546 struct wl_surface *surface,
1547 wl_fixed_t x,
1548 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001549{
Daniel Stone37816df2012-05-16 18:45:18 +01001550 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551 struct shell_surface *priv =
1552 container_of(grab, struct shell_surface, popup.grab);
1553 struct wl_client *client = priv->surface->surface.resource.client;
1554
Pekka Paalanencb108432012-01-19 16:25:40 +02001555 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001556 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001557 grab->focus = surface;
1558 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001559 wl_pointer_set_focus(pointer, NULL,
1560 wl_fixed_from_int(0),
1561 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562 grab->focus = NULL;
1563 }
1564}
1565
1566static void
Scott Moreau447013d2012-02-18 05:05:29 -07001567popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001568 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001569{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570 struct wl_resource *resource;
1571
Daniel Stone37816df2012-05-16 18:45:18 +01001572 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001573 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001574 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001575}
1576
1577static void
Scott Moreau447013d2012-02-18 05:05:29 -07001578popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001579 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001580{
1581 struct wl_resource *resource;
1582 struct shell_surface *shsurf =
1583 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001584 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001585 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001586 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001587
Daniel Stone37816df2012-05-16 18:45:18 +01001588 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001590 display = wl_client_get_display(resource->client);
1591 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001592 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001593 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001594 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001595 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001596 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001597 wl_pointer_end_grab(grab->pointer);
1598 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001599 }
1600
Daniel Stone4dbadb12012-05-30 16:31:51 +01001601 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001602 shsurf->popup.initial_up = 1;
1603}
1604
Scott Moreau447013d2012-02-18 05:05:29 -07001605static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606 popup_grab_focus,
1607 popup_grab_motion,
1608 popup_grab_button,
1609};
1610
1611static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001612shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001613{
Daniel Stone37816df2012-05-16 18:45:18 +01001614 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001615 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001616 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617
1618 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001619 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001620
Pekka Paalanen938269a2012-02-07 14:19:01 +02001621 weston_surface_update_transform(parent);
1622 if (parent->transform.enabled) {
1623 shsurf->popup.parent_transform.matrix =
1624 parent->transform.matrix;
1625 } else {
1626 /* construct x, y translation matrix */
1627 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1628 shsurf->popup.parent_transform.matrix.d[12] =
1629 parent->geometry.x;
1630 shsurf->popup.parent_transform.matrix.d[13] =
1631 parent->geometry.y;
1632 }
1633 wl_list_insert(es->geometry.transformation_list.prev,
1634 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001635 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001636
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001637 shsurf->popup.initial_up = 0;
1638
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001639 /* We don't require the grab to still be active, but if another
1640 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001641 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1642 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001643 } else {
1644 wl_shell_surface_send_popup_done(&shsurf->resource);
1645 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001646}
1647
1648static void
1649shell_surface_set_popup(struct wl_client *client,
1650 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001651 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001652 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001653 struct wl_resource *parent_resource,
1654 int32_t x, int32_t y, uint32_t flags)
1655{
1656 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001657
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001658 shsurf->type = SHELL_SURFACE_POPUP;
1659 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001660 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001661 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001662 shsurf->popup.x = x;
1663 shsurf->popup.y = y;
1664}
1665
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001666static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001667 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668 shell_surface_move,
1669 shell_surface_resize,
1670 shell_surface_set_toplevel,
1671 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001672 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001673 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001674 shell_surface_set_maximized,
1675 shell_surface_set_title,
1676 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001677};
1678
1679static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001680destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001681{
Daniel Stone37816df2012-05-16 18:45:18 +01001682 if (shsurf->popup.grab.pointer)
1683 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001684
Alex Wubd3354b2012-04-17 17:20:49 +08001685 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1686 shell_surface_is_top_fullscreen(shsurf)) {
1687 weston_output_switch_mode(shsurf->fullscreen_output,
1688 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001689 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001690
Alex Wuaa08e2d2012-03-05 11:01:40 +08001691 if (shsurf->fullscreen.black_surface)
1692 weston_surface_destroy(shsurf->fullscreen.black_surface);
1693
Alex Wubd3354b2012-04-17 17:20:49 +08001694 /* As destroy_resource() use wl_list_for_each_safe(),
1695 * we can always remove the listener.
1696 */
1697 wl_list_remove(&shsurf->surface_destroy_listener.link);
1698 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001699 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001700
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001701 wl_list_remove(&shsurf->link);
1702 free(shsurf);
1703}
1704
1705static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001706shell_destroy_shell_surface(struct wl_resource *resource)
1707{
1708 struct shell_surface *shsurf = resource->data;
1709
1710 destroy_shell_surface(shsurf);
1711}
1712
1713static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001714shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001715{
1716 struct shell_surface *shsurf = container_of(listener,
1717 struct shell_surface,
1718 surface_destroy_listener);
1719
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001720 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001721 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001722 } else {
1723 wl_signal_emit(&shsurf->resource.destroy_signal,
1724 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001725 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001726 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001727}
1728
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001729static void
1730shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1731
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001732static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001733get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001734{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001735 if (surface->configure == shell_surface_configure)
1736 return surface->private;
1737 else
1738 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001739}
1740
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001741static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001742create_shell_surface(void *shell, struct weston_surface *surface,
1743 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001744{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001745 struct shell_surface *shsurf;
1746
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001747 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001748 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001749 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001750 }
1751
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001752 shsurf = calloc(1, sizeof *shsurf);
1753 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001754 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001755 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001756 }
1757
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001758 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001759 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001760
Tiago Vignattibc052c92012-04-19 16:18:18 +03001761 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001762 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001763 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001764 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001765 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001766 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1767 shsurf->fullscreen.framerate = 0;
1768 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001769 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001770 wl_list_init(&shsurf->fullscreen.transform.link);
1771
Tiago Vignattibc052c92012-04-19 16:18:18 +03001772 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001773 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1774 wl_signal_add(&surface->surface.resource.destroy_signal,
1775 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001776
1777 /* init link so its safe to always remove it in destroy_shell_surface */
1778 wl_list_init(&shsurf->link);
1779
Pekka Paalanen460099f2012-01-20 16:48:25 +02001780 /* empty when not in use */
1781 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001782 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001783
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001784 wl_list_init(&shsurf->workspace_transform.link);
1785
Pekka Paalanen98262232011-12-01 10:42:22 +02001786 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001787 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001788
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001789 shsurf->client = client;
1790
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001791 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001792}
1793
1794static void
1795shell_get_shell_surface(struct wl_client *client,
1796 struct wl_resource *resource,
1797 uint32_t id,
1798 struct wl_resource *surface_resource)
1799{
1800 struct weston_surface *surface = surface_resource->data;
1801 struct desktop_shell *shell = resource->data;
1802 struct shell_surface *shsurf;
1803
1804 if (get_shell_surface(surface)) {
1805 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001806 WL_DISPLAY_ERROR_INVALID_OBJECT,
1807 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001808 return;
1809 }
1810
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001811 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001812 if (!shsurf) {
1813 wl_resource_post_error(surface_resource,
1814 WL_DISPLAY_ERROR_INVALID_OBJECT,
1815 "surface->configure already set");
1816 return;
1817 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001818
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001819 shsurf->resource.destroy = shell_destroy_shell_surface;
1820 shsurf->resource.object.id = id;
1821 shsurf->resource.object.interface = &wl_shell_surface_interface;
1822 shsurf->resource.object.implementation =
1823 (void (**)(void)) &shell_surface_implementation;
1824 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001825
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001826 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001827}
1828
1829static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001830 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001831};
1832
Kristian Høgsberg07937562011-04-12 17:25:42 -04001833static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001834handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001835{
1836 proc->pid = 0;
1837}
1838
1839static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001840launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001841{
1842 if (shell->screensaver.binding)
1843 return;
1844
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001845 if (!shell->screensaver.path)
1846 return;
1847
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001848 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001849 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001850 return;
1851 }
1852
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001853 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001854 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001855 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001856 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001857}
1858
1859static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001860terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001861{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001862 if (shell->screensaver.process.pid == 0)
1863 return;
1864
1865 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001866}
1867
1868static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001869configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001870{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001871 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001872
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001873 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1874 if (s->output == es->output && s != es) {
1875 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001876 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001877 }
1878 }
1879
1880 weston_surface_configure(es, es->output->x, es->output->y,
1881 es->buffer->width, es->buffer->height);
1882
1883 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001884 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001885 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001886 }
1887}
1888
1889static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001890background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1891{
1892 struct desktop_shell *shell = es->private;
1893
1894 configure_static_surface(es, &shell->background_layer);
1895}
1896
1897static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001898desktop_shell_set_background(struct wl_client *client,
1899 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001900 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001901 struct wl_resource *surface_resource)
1902{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001903 struct desktop_shell *shell = resource->data;
1904 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001905
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001906 if (surface->configure) {
1907 wl_resource_post_error(surface_resource,
1908 WL_DISPLAY_ERROR_INVALID_OBJECT,
1909 "surface role already assigned");
1910 return;
1911 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001912
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001913 surface->configure = background_configure;
1914 surface->private = shell;
1915 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001916 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001917 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001918 surface->output->width,
1919 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001920}
1921
1922static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001923panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1924{
1925 struct desktop_shell *shell = es->private;
1926
1927 configure_static_surface(es, &shell->panel_layer);
1928}
1929
1930static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001931desktop_shell_set_panel(struct wl_client *client,
1932 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001933 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001934 struct wl_resource *surface_resource)
1935{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001936 struct desktop_shell *shell = resource->data;
1937 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001938
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001939 if (surface->configure) {
1940 wl_resource_post_error(surface_resource,
1941 WL_DISPLAY_ERROR_INVALID_OBJECT,
1942 "surface role already assigned");
1943 return;
1944 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001945
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001946 surface->configure = panel_configure;
1947 surface->private = shell;
1948 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001949 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001950 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001951 surface->output->width,
1952 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001953}
1954
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001955static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001956lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1957{
1958 struct desktop_shell *shell = surface->private;
1959
1960 center_on_output(surface, get_default_output(shell->compositor));
1961
1962 if (!weston_surface_is_mapped(surface)) {
1963 wl_list_insert(&shell->lock_layer.surface_list,
1964 &surface->layer_link);
1965 weston_surface_assign_output(surface);
1966 weston_compositor_wake(shell->compositor);
1967 }
1968}
1969
1970static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001971handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001972{
Tiago Vignattibe143262012-04-16 17:31:41 +03001973 struct desktop_shell *shell =
1974 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001975
Martin Minarik6d118362012-06-07 18:01:59 +02001976 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001977 shell->lock_surface = NULL;
1978}
1979
1980static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001981desktop_shell_set_lock_surface(struct wl_client *client,
1982 struct wl_resource *resource,
1983 struct wl_resource *surface_resource)
1984{
Tiago Vignattibe143262012-04-16 17:31:41 +03001985 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001986 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001987
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001988 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001989
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001990 if (!shell->locked)
1991 return;
1992
Pekka Paalanen98262232011-12-01 10:42:22 +02001993 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001994
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001995 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1996 wl_signal_add(&surface_resource->destroy_signal,
1997 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001998
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001999 surface->configure = lock_surface_configure;
2000 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002001}
2002
2003static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002004resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002005{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002006 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002007 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002008
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002009 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2010 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002011
2012 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002013
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002014 wl_list_remove(&shell->lock_layer.link);
2015 wl_list_insert(&shell->compositor->cursor_layer.link,
2016 &shell->fullscreen_layer.link);
2017 wl_list_insert(&shell->fullscreen_layer.link,
2018 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002019 if (shell->showing_input_panels) {
2020 wl_list_insert(&shell->panel_layer.link,
2021 &shell->input_panel_layer.link);
2022 wl_list_insert(&shell->input_panel_layer.link,
2023 &ws->layer.link);
2024 } else {
2025 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2026 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002027
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002028 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002029
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002030 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002031 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002033 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002034}
2035
2036static void
2037desktop_shell_unlock(struct wl_client *client,
2038 struct wl_resource *resource)
2039{
Tiago Vignattibe143262012-04-16 17:31:41 +03002040 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002041
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002042 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002043
2044 if (shell->locked)
2045 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002046}
2047
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002049desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002050 struct wl_resource *resource,
2051 struct wl_resource *surface_resource)
2052{
2053 struct desktop_shell *shell = resource->data;
2054
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002055 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056}
2057
Kristian Høgsberg75840622011-09-06 13:48:16 -04002058static const struct desktop_shell_interface desktop_shell_implementation = {
2059 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002060 desktop_shell_set_panel,
2061 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002063 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002064};
2065
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002066static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002067get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002068{
2069 struct shell_surface *shsurf;
2070
2071 shsurf = get_shell_surface(surface);
2072 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002073 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002074 return shsurf->type;
2075}
2076
Kristian Høgsberg75840622011-09-06 13:48:16 -04002077static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002078move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002079{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002080 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002081 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002082 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002083
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002084 if (surface == NULL)
2085 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002086
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002087 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002088 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002089 return;
2090
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002091 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002092}
2093
2094static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002095resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002096{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002097 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002098 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002099 uint32_t edges = 0;
2100 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002101 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002102
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002103 if (surface == NULL)
2104 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002105
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002106 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002107 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002108 return;
2109
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002110 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002111 wl_fixed_to_int(seat->pointer->grab_x),
2112 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002113 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002114
Pekka Paalanen60921e52012-01-25 15:55:43 +02002115 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002116 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002117 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002118 edges |= 0;
2119 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002120 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002121
Pekka Paalanen60921e52012-01-25 15:55:43 +02002122 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002123 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002124 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002125 edges |= 0;
2126 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002127 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002128
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002129 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002130}
2131
2132static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002133surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002134 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002135{
Scott Moreau02709af2012-05-22 01:54:10 -06002136 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002137 struct shell_surface *shsurf;
2138 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002139 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002140
2141 if (surface == NULL)
2142 return;
2143
2144 shsurf = get_shell_surface(surface);
2145 if (!shsurf)
2146 return;
2147
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002148 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002149
Scott Moreau02709af2012-05-22 01:54:10 -06002150 if (surface->alpha > 1.0)
2151 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002152 if (surface->alpha < step)
2153 surface->alpha = step;
2154
2155 surface->geometry.dirty = 1;
2156 weston_surface_damage(surface);
2157}
2158
2159static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002160do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002161 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002162{
Daniel Stone37816df2012-05-16 18:45:18 +01002163 struct weston_seat *ws = (struct weston_seat *) seat;
2164 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002165 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002166 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002167
2168 wl_list_for_each(output, &compositor->output_list, link) {
2169 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002170 wl_fixed_to_double(seat->pointer->x),
2171 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002172 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002173 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002174 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002175 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002176 increment = -output->zoom.increment;
2177 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002178 increment = output->zoom.increment *
2179 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002180 else
2181 increment = 0;
2182
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002183 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002184
Scott Moreaue6603982012-06-11 13:07:51 -06002185 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002186 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002187 else if (output->zoom.level > output->zoom.max_level)
2188 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002189 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002190 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002191 output->disable_planes++;
2192 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002193
Scott Moreaue6603982012-06-11 13:07:51 -06002194 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002195
Scott Moreau8dacaab2012-06-17 18:10:58 -06002196 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002197 }
2198 }
2199}
2200
Scott Moreauccbf29d2012-02-22 14:21:41 -07002201static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002202zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002203 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002204{
2205 do_zoom(seat, time, 0, axis, value);
2206}
2207
2208static void
2209zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2210 void *data)
2211{
2212 do_zoom(seat, time, key, 0, 0);
2213}
2214
2215static void
2216terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2217 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002218{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002219 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002220
Daniel Stone325fc2d2012-05-30 16:31:58 +01002221 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002222}
2223
2224static void
Scott Moreau447013d2012-02-18 05:05:29 -07002225rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002226 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002227{
2228 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002229 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002230 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002231 struct shell_surface *shsurf = rotate->base.shsurf;
2232 struct weston_surface *surface;
2233 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2234
2235 if (!shsurf)
2236 return;
2237
2238 surface = shsurf->surface;
2239
2240 cx = 0.5f * surface->geometry.width;
2241 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002242
Daniel Stone37816df2012-05-16 18:45:18 +01002243 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2244 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002245 r = sqrtf(dx * dx + dy * dy);
2246
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002247 wl_list_remove(&shsurf->rotation.transform.link);
2248 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002249
2250 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002252 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002253
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002254 weston_matrix_init(&rotate->rotation);
2255 rotate->rotation.d[0] = dx / r;
2256 rotate->rotation.d[4] = -dy / r;
2257 rotate->rotation.d[1] = -rotate->rotation.d[4];
2258 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002259
2260 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002261 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002262 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002263 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002264 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002265
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002266 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002267 &shsurf->surface->geometry.transformation_list,
2268 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002269 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002270 wl_list_init(&shsurf->rotation.transform.link);
2271 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002272 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002274
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002275 /* We need to adjust the position of the surface
2276 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002277 cposx = surface->geometry.x + cx;
2278 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002279 dposx = rotate->center.x - cposx;
2280 dposy = rotate->center.y - cposy;
2281 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002282 weston_surface_set_position(surface,
2283 surface->geometry.x + dposx,
2284 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002285 }
2286
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002287 /* Repaint implies weston_surface_update_transform(), which
2288 * lazily applies the damage due to rotation update.
2289 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002290 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002291}
2292
2293static void
Scott Moreau447013d2012-02-18 05:05:29 -07002294rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002295 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002296{
2297 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002298 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002299 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002300 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002301 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302
Daniel Stone4dbadb12012-05-30 16:31:51 +01002303 if (pointer->button_count == 0 &&
2304 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002305 if (shsurf)
2306 weston_matrix_multiply(&shsurf->rotation.rotation,
2307 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002308 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002309 free(rotate);
2310 }
2311}
2312
Scott Moreau447013d2012-02-18 05:05:29 -07002313static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002314 noop_grab_focus,
2315 rotate_grab_motion,
2316 rotate_grab_button,
2317};
2318
2319static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002320rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002321 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002322{
2323 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002324 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002325 struct shell_surface *surface;
2326 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002327 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002328 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002329
2330 if (base_surface == NULL)
2331 return;
2332
2333 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002334 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002335 return;
2336
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337 rotate = malloc(sizeof *rotate);
2338 if (!rotate)
2339 return;
2340
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002341 weston_surface_to_global_float(surface->surface,
2342 surface->surface->geometry.width / 2,
2343 surface->surface->geometry.height / 2,
2344 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002345
Daniel Stone37816df2012-05-16 18:45:18 +01002346 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2347 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002348 r = sqrtf(dx * dx + dy * dy);
2349 if (r > 20.0f) {
2350 struct weston_matrix inverse;
2351
2352 weston_matrix_init(&inverse);
2353 inverse.d[0] = dx / r;
2354 inverse.d[4] = dy / r;
2355 inverse.d[1] = -inverse.d[4];
2356 inverse.d[5] = inverse.d[0];
2357 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002358
2359 weston_matrix_init(&rotate->rotation);
2360 rotate->rotation.d[0] = dx / r;
2361 rotate->rotation.d[4] = -dy / r;
2362 rotate->rotation.d[1] = -rotate->rotation.d[4];
2363 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002364 } else {
2365 weston_matrix_init(&surface->rotation.rotation);
2366 weston_matrix_init(&rotate->rotation);
2367 }
2368
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002369 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2370 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002371}
2372
2373static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002374lower_fullscreen_layer(struct desktop_shell *shell)
2375{
2376 struct workspace *ws;
2377 struct weston_surface *surface, *prev;
2378
2379 ws = get_current_workspace(shell);
2380 wl_list_for_each_reverse_safe(surface, prev,
2381 &shell->fullscreen_layer.surface_list,
2382 layer_link)
2383 weston_surface_restack(surface, &ws->layer.surface_list);
2384}
2385
2386static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002387activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002388 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002389{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002390 struct workspace *ws = get_current_workspace(shell);
2391 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392
Daniel Stone37816df2012-05-16 18:45:18 +01002393 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002394
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002395 wl_list_for_each(state, &ws->focus_list, link)
2396 if (state->seat == seat)
2397 break;
2398
2399 if (&state->link == &ws->focus_list) {
2400 state = focus_state_create(seat, ws);
2401 if (state == NULL)
2402 return;
2403 }
2404
2405 state->keyboard_focus = es;
2406 wl_list_remove(&state->surface_destroy_listener.link);
2407 wl_signal_add(&es->surface.resource.destroy_signal,
2408 &state->surface_destroy_listener);
2409
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002410 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002411 case SHELL_SURFACE_FULLSCREEN:
2412 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002413 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002414 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002415 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002416 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002417 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002418 lower_fullscreen_layer(shell);
2419 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002420 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002421 }
2422}
2423
Alex Wu21858432012-04-01 20:13:08 +08002424/* no-op func for checking black surface */
2425static void
2426black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2427{
2428}
2429
2430static bool
2431is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2432{
2433 if (es->configure == black_surface_configure) {
2434 if (fs_surface)
2435 *fs_surface = (struct weston_surface *)es->private;
2436 return true;
2437 }
2438 return false;
2439}
2440
Kristian Høgsberg75840622011-09-06 13:48:16 -04002441static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002442click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002443 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002444{
Daniel Stone37816df2012-05-16 18:45:18 +01002445 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002446 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002447 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002448 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002449
Daniel Stone37816df2012-05-16 18:45:18 +01002450 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002451 if (!focus)
2452 return;
2453
Alex Wu21858432012-04-01 20:13:08 +08002454 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002455 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002456
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002457 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2458 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002459
Daniel Stone325fc2d2012-05-30 16:31:58 +01002460 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002461 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002462}
2463
2464static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002465lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002466{
Tiago Vignattibe143262012-04-16 17:31:41 +03002467 struct desktop_shell *shell =
2468 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002469 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002470 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002471
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002472 if (shell->locked) {
2473 wl_list_for_each(output, &shell->compositor->output_list, link)
2474 /* TODO: find a way to jump to other DPMS levels */
2475 if (output->set_dpms)
2476 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002477 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002478 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479
2480 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002481
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002482 /* Hide all surfaces by removing the fullscreen, panel and
2483 * toplevel layers. This way nothing else can show or receive
2484 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002485
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002486 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002487 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002488 if (shell->showing_input_panels)
2489 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002490 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002491 wl_list_insert(&shell->compositor->cursor_layer.link,
2492 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002493
Pekka Paalanen77346a62011-11-30 16:26:35 +02002494 launch_screensaver(shell);
2495
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002496 /* TODO: disable bindings that should not work while locked. */
2497
2498 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002499}
2500
2501static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002502unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002503{
Tiago Vignattibe143262012-04-16 17:31:41 +03002504 struct desktop_shell *shell =
2505 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002506
Pekka Paalanend81c2162011-11-16 13:47:34 +02002507 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002508 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002509 return;
2510 }
2511
2512 /* If desktop-shell client has gone away, unlock immediately. */
2513 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002514 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002515 return;
2516 }
2517
2518 if (shell->prepare_event_sent)
2519 return;
2520
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002521 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002522 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002523}
2524
2525static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002526show_input_panels(struct wl_listener *listener, void *data)
2527{
2528 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002529 container_of(listener, struct desktop_shell,
2530 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002531 struct input_panel_surface *surface, *next;
2532 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002533
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002534 shell->showing_input_panels = true;
2535
2536 wl_list_insert(&shell->panel_layer.link,
2537 &shell->input_panel_layer.link);
2538
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002539 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002540 &shell->input_panel.surfaces, link) {
2541 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002542 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002543 &ws->layer_link);
2544 weston_surface_assign_output(ws);
2545 weston_surface_damage(ws);
2546 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002547 }
2548}
2549
2550static void
2551hide_input_panels(struct wl_listener *listener, void *data)
2552{
2553 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002554 container_of(listener, struct desktop_shell,
2555 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002556 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002557
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002558 shell->showing_input_panels = false;
2559
2560 wl_list_remove(&shell->input_panel_layer.link);
2561
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002562 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002563 &shell->input_panel_layer.surface_list, layer_link)
2564 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002565}
2566
2567static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002568center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002569{
Scott Moreau1bad5db2012-08-18 01:04:05 -06002570 GLfloat x = (output->width - surface->buffer->width) / 2;
2571 GLfloat y = (output->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002572
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002573 weston_surface_configure(surface, output->x + x, output->y + y,
2574 surface->buffer->width,
2575 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002576}
2577
2578static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002579weston_surface_set_initial_position (struct weston_surface *surface,
2580 struct desktop_shell *shell)
2581{
2582 struct weston_compositor *compositor = shell->compositor;
2583 int ix = 0, iy = 0;
2584 int range_x, range_y;
2585 int dx, dy, x, y, panel_height;
2586 struct weston_output *output, *target_output = NULL;
2587 struct weston_seat *seat;
2588
2589 /* As a heuristic place the new window on the same output as the
2590 * pointer. Falling back to the output containing 0, 0.
2591 *
2592 * TODO: Do something clever for touch too?
2593 */
2594 wl_list_for_each(seat, &compositor->seat_list, link) {
2595 if (seat->has_pointer) {
2596 ix = wl_fixed_to_int(seat->pointer.x);
2597 iy = wl_fixed_to_int(seat->pointer.y);
2598 break;
2599 }
2600 }
2601
2602 wl_list_for_each(output, &compositor->output_list, link) {
2603 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2604 target_output = output;
2605 break;
2606 }
2607 }
2608
2609 if (!target_output) {
2610 weston_surface_set_position(surface, 10 + random() % 400,
2611 10 + random() % 400);
2612 return;
2613 }
2614
2615 /* Valid range within output where the surface will still be onscreen.
2616 * If this is negative it means that the surface is bigger than
2617 * output.
2618 */
2619 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002620 range_x = target_output->width - surface->geometry.width;
2621 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002622 surface->geometry.height;
2623
Rob Bradford4cb88c72012-08-13 15:18:44 +01002624 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002625 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002626 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002627 dx = 0;
2628
2629 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002630 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002631 else
2632 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002633
2634 x = target_output->x + dx;
2635 y = target_output->y + dy;
2636
2637 weston_surface_set_position (surface, x, y);
2638}
2639
2640static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002641map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002642 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002643{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002644 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002645 struct shell_surface *shsurf = get_shell_surface(surface);
2646 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002647 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002648 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002649 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002650 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002651
Pekka Paalanen60921e52012-01-25 15:55:43 +02002652 surface->geometry.width = width;
2653 surface->geometry.height = height;
2654 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002655
2656 /* initial positioning, see also configure() */
2657 switch (surface_type) {
2658 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002659 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002660 break;
Alex Wu4539b082012-03-01 12:57:46 +08002661 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002662 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002663 shell_map_fullscreen(shsurf);
2664 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002665 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002666 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002667 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002668 weston_surface_set_position(surface, shsurf->output->x,
2669 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002670 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002671 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002672 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002673 case SHELL_SURFACE_NONE:
2674 weston_surface_set_position(surface,
2675 surface->geometry.x + sx,
2676 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002677 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002678 default:
2679 ;
2680 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002681
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002682 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002683 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002684 case SHELL_SURFACE_POPUP:
2685 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002686 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002687 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2688 break;
Alex Wu4539b082012-03-01 12:57:46 +08002689 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002690 case SHELL_SURFACE_NONE:
2691 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002692 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002693 ws = get_current_workspace(shell);
2694 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002695 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002696 }
2697
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002698 if (surface_type != SHELL_SURFACE_NONE) {
2699 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002700 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2701 surface->output = shsurf->output;
2702 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002703
Juan Zhao7bb92f02011-12-15 11:31:51 -05002704 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002705 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002706 if (shsurf->transient.flags ==
2707 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2708 break;
2709 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002710 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002711 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002712 if (!shell->locked) {
2713 wl_list_for_each(seat, &compositor->seat_list, link)
2714 activate(shell, surface, seat);
2715 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002716 break;
2717 default:
2718 break;
2719 }
2720
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002721 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002722 {
2723 switch (shell->win_animation_type) {
2724 case ANIMATION_FADE:
2725 weston_fade_run(surface, NULL, NULL);
2726 break;
2727 case ANIMATION_ZOOM:
2728 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2729 break;
2730 default:
2731 break;
2732 }
2733 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002734}
2735
2736static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002737configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002738 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002739{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002740 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2741 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002742
Pekka Paalanen77346a62011-11-30 16:26:35 +02002743 shsurf = get_shell_surface(surface);
2744 if (shsurf)
2745 surface_type = shsurf->type;
2746
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002747 surface->geometry.x = x;
2748 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002749 surface->geometry.width = width;
2750 surface->geometry.height = height;
2751 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002752
2753 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002754 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002755 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002756 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002757 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002758 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002759 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002760 surface->geometry.x = surface->output->x;
2761 surface->geometry.y = surface->output->y +
2762 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002763 break;
Alex Wu4539b082012-03-01 12:57:46 +08002764 case SHELL_SURFACE_TOPLEVEL:
2765 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002766 default:
2767 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002768 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002769
Alex Wu4539b082012-03-01 12:57:46 +08002770 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002771 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002772 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002773
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002774 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002775 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002776 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002777}
2778
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002779static void
2780shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2781{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002782 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002783 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002784 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002785
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002786 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002787 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002788 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002789 type_changed = 1;
2790 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002791
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002792 if (!weston_surface_is_mapped(es)) {
2793 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002794 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002795 es->geometry.width != es->buffer->width ||
2796 es->geometry.height != es->buffer->height) {
2797 GLfloat from_x, from_y;
2798 GLfloat to_x, to_y;
2799
2800 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2801 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2802 configure(shell, es,
2803 es->geometry.x + to_x - from_x,
2804 es->geometry.y + to_y - from_y,
2805 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002806 }
2807}
2808
Tiago Vignattibe143262012-04-16 17:31:41 +03002809static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002810
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002811static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002812desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002813{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002814 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002815 struct desktop_shell *shell =
2816 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002817
2818 shell->child.process.pid = 0;
2819 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002820
2821 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2822 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002823 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002824 shell->child.deathstamp = time;
2825 shell->child.deathcount = 0;
2826 }
2827
2828 shell->child.deathcount++;
2829 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002830 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002831 return;
2832 }
2833
Martin Minarik6d118362012-06-07 18:01:59 +02002834 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002835 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002836}
2837
2838static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002839launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002840{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002841 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002842
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002843 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002844 &shell->child.process,
2845 shell_exe,
2846 desktop_shell_sigchld);
2847
2848 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002849 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002850 return 0;
2851}
2852
2853static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002854bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2855{
Tiago Vignattibe143262012-04-16 17:31:41 +03002856 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002857
2858 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002859 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002860}
2861
Kristian Høgsberg75840622011-09-06 13:48:16 -04002862static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002863unbind_desktop_shell(struct wl_resource *resource)
2864{
Tiago Vignattibe143262012-04-16 17:31:41 +03002865 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002866
2867 if (shell->locked)
2868 resume_desktop(shell);
2869
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002870 shell->child.desktop_shell = NULL;
2871 shell->prepare_event_sent = false;
2872 free(resource);
2873}
2874
2875static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002876bind_desktop_shell(struct wl_client *client,
2877 void *data, uint32_t version, uint32_t id)
2878{
Tiago Vignattibe143262012-04-16 17:31:41 +03002879 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002880 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002881
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002882 resource = wl_client_add_object(client, &desktop_shell_interface,
2883 &desktop_shell_implementation,
2884 id, shell);
2885
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002886 if (client == shell->child.client) {
2887 resource->destroy = unbind_desktop_shell;
2888 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002889 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002890 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002891
2892 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2893 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002894 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002895}
2896
Pekka Paalanen6e168112011-11-24 11:34:05 +02002897static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002898screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2899{
2900 struct desktop_shell *shell = surface->private;
2901
2902 if (!shell->locked)
2903 return;
2904
2905 center_on_output(surface, surface->output);
2906
2907 if (wl_list_empty(&surface->layer_link)) {
2908 wl_list_insert(shell->lock_layer.surface_list.prev,
2909 &surface->layer_link);
2910 weston_surface_assign_output(surface);
2911 shell->compositor->idle_time = shell->screensaver.duration;
2912 weston_compositor_wake(shell->compositor);
2913 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2914 }
2915}
2916
2917static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002918screensaver_set_surface(struct wl_client *client,
2919 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002920 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002921 struct wl_resource *output_resource)
2922{
Tiago Vignattibe143262012-04-16 17:31:41 +03002923 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002924 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002925 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002926
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002927 surface->configure = screensaver_configure;
2928 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002929 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002930}
2931
2932static const struct screensaver_interface screensaver_implementation = {
2933 screensaver_set_surface
2934};
2935
2936static void
2937unbind_screensaver(struct wl_resource *resource)
2938{
Tiago Vignattibe143262012-04-16 17:31:41 +03002939 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002940
Pekka Paalanen77346a62011-11-30 16:26:35 +02002941 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002942 free(resource);
2943}
2944
2945static void
2946bind_screensaver(struct wl_client *client,
2947 void *data, uint32_t version, uint32_t id)
2948{
Tiago Vignattibe143262012-04-16 17:31:41 +03002949 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002950 struct wl_resource *resource;
2951
2952 resource = wl_client_add_object(client, &screensaver_interface,
2953 &screensaver_implementation,
2954 id, shell);
2955
Pekka Paalanen77346a62011-11-30 16:26:35 +02002956 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002957 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002958 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002959 return;
2960 }
2961
2962 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2963 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002964 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002965}
2966
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002967static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002968input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2969{
2970 struct weston_mode *mode = surface->output->current;
2971 GLfloat x = (mode->width - surface->buffer->width) / 2;
2972 GLfloat y = mode->height - surface->buffer->height;
2973
2974 /* Don't map the input panel here, wait for
2975 * show_input_panels signal. */
2976
2977 weston_surface_configure(surface,
2978 surface->output->x + x,
2979 surface->output->y + y,
2980 surface->buffer->width,
2981 surface->buffer->height);
2982}
2983
2984static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002985destroy_input_panel_surface(struct wl_listener *listener,
2986 void *data)
2987{
2988 struct input_panel_surface *input_panel_surface =
2989 container_of(listener, struct input_panel_surface, listener);
2990
2991 wl_list_remove(&listener->link);
2992 wl_list_remove(&input_panel_surface->link);
2993
2994 free(input_panel_surface);
2995}
2996
2997static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002998input_panel_set_surface(struct wl_client *client,
2999 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003000 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003001 struct wl_resource *output_resource)
3002{
3003 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003004 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003005 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003006 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003007
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003008 surface->configure = input_panel_configure;
3009 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003010 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003011
3012 input_panel_surface = malloc(sizeof *input_panel_surface);
3013 if (!input_panel_surface) {
3014 wl_resource_post_no_memory(resource);
3015 return;
3016 }
3017
3018 input_panel_surface->surface = surface;
3019 input_panel_surface->listener.notify = destroy_input_panel_surface;
3020
3021 wl_signal_add(&surface_resource->destroy_signal,
3022 &input_panel_surface->listener);
3023
3024 wl_list_insert(&shell->input_panel.surfaces,
3025 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003026}
3027
3028static const struct input_panel_interface input_panel_implementation = {
3029 input_panel_set_surface
3030};
3031
3032static void
3033unbind_input_panel(struct wl_resource *resource)
3034{
3035 struct desktop_shell *shell = resource->data;
3036
3037 shell->input_panel.binding = NULL;
3038 free(resource);
3039}
3040
3041static void
3042bind_input_panel(struct wl_client *client,
3043 void *data, uint32_t version, uint32_t id)
3044{
3045 struct desktop_shell *shell = data;
3046 struct wl_resource *resource;
3047
3048 resource = wl_client_add_object(client, &input_panel_interface,
3049 &input_panel_implementation,
3050 id, shell);
3051
3052 if (shell->input_panel.binding == NULL) {
3053 resource->destroy = unbind_input_panel;
3054 shell->input_panel.binding = resource;
3055 return;
3056 }
3057
3058 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3059 "interface object already bound");
3060 wl_resource_destroy(resource);
3061}
3062
Kristian Høgsberg07045392012-02-19 18:52:44 -05003063struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003064 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003065 struct weston_surface *current;
3066 struct wl_listener listener;
3067 struct wl_keyboard_grab grab;
3068};
3069
3070static void
3071switcher_next(struct switcher *switcher)
3072{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003073 struct weston_surface *surface;
3074 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003075 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003076 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003077
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003078 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003079 switch (get_shell_surface_type(surface)) {
3080 case SHELL_SURFACE_TOPLEVEL:
3081 case SHELL_SURFACE_FULLSCREEN:
3082 case SHELL_SURFACE_MAXIMIZED:
3083 if (first == NULL)
3084 first = surface;
3085 if (prev == switcher->current)
3086 next = surface;
3087 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003088 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003089 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003090 weston_surface_damage(surface);
3091 break;
3092 default:
3093 break;
3094 }
Alex Wu1659daa2012-04-01 20:13:09 +08003095
3096 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003097 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003098 surface->geometry.dirty = 1;
3099 weston_surface_damage(surface);
3100 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003101 }
3102
3103 if (next == NULL)
3104 next = first;
3105
Alex Wu07b26062012-03-12 16:06:01 +08003106 if (next == NULL)
3107 return;
3108
Kristian Høgsberg07045392012-02-19 18:52:44 -05003109 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003110 wl_signal_add(&next->surface.resource.destroy_signal,
3111 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003112
3113 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003114 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003115
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003116 shsurf = get_shell_surface(switcher->current);
3117 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003118 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003119}
3120
3121static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003122switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003123{
3124 struct switcher *switcher =
3125 container_of(listener, struct switcher, listener);
3126
3127 switcher_next(switcher);
3128}
3129
3130static void
Daniel Stone351eb612012-05-31 15:27:47 -04003131switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003132{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003133 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003134 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003135 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003136
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003137 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003138 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003139 weston_surface_damage(surface);
3140 }
3141
Alex Wu07b26062012-03-12 16:06:01 +08003142 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003143 activate(switcher->shell, switcher->current,
3144 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003145 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003146 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003147 free(switcher);
3148}
3149
3150static void
3151switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003152 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003153{
3154 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003155 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003156
Daniel Stonec9785ea2012-05-30 16:31:52 +01003157 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003158 switcher_next(switcher);
3159}
3160
3161static void
3162switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3163 uint32_t mods_depressed, uint32_t mods_latched,
3164 uint32_t mods_locked, uint32_t group)
3165{
3166 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003167 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003168
Daniel Stone351eb612012-05-31 15:27:47 -04003169 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3170 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003171}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003172
3173static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003174 switcher_key,
3175 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003176};
3177
3178static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003179switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3180 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003181{
Tiago Vignattibe143262012-04-16 17:31:41 +03003182 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003183 struct switcher *switcher;
3184
3185 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003186 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003187 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003188 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003189 wl_list_init(&switcher->listener.link);
3190
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003191 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003192 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003193 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3194 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003195 switcher_next(switcher);
3196}
3197
Pekka Paalanen3c647232011-12-22 13:43:43 +02003198static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003199backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3200 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003201{
3202 struct weston_compositor *compositor = data;
3203 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003204 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003205
3206 /* TODO: we're limiting to simple use cases, where we assume just
3207 * control on the primary display. We'd have to extend later if we
3208 * ever get support for setting backlights on random desktop LCD
3209 * panels though */
3210 output = get_default_output(compositor);
3211 if (!output)
3212 return;
3213
3214 if (!output->set_backlight)
3215 return;
3216
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003217 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3218 backlight_new = output->backlight_current - 25;
3219 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3220 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003221
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003222 if (backlight_new < 5)
3223 backlight_new = 5;
3224 if (backlight_new > 255)
3225 backlight_new = 255;
3226
3227 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003228 output->set_backlight(output, output->backlight_current);
3229}
3230
3231static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003232debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3233 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003234{
Tiago Vignattibe143262012-04-16 17:31:41 +03003235 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003236 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003237 struct weston_surface *surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003238 struct weston_plane plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003239
3240 if (shell->debug_repaint_surface) {
3241 weston_surface_destroy(shell->debug_repaint_surface);
3242 shell->debug_repaint_surface = NULL;
3243 } else {
3244 surface = weston_surface_create(compositor);
3245 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3246 weston_surface_configure(surface, 0, 0, 8192, 8192);
3247 wl_list_insert(&compositor->fade_layer.surface_list,
3248 &surface->layer_link);
3249 weston_surface_assign_output(surface);
3250 pixman_region32_init(&surface->input);
3251
3252 /* Here's the dirty little trick that makes the
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003253 * repaint debugging work: we move the surface to a
3254 * different plane and force an update_transform to
3255 * update dependent state and clear the
3256 * geometry.dirty bit. This way the call to
3257 * damage_below() in update_transform() does not
3258 * add damage to the primary plane. */
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003259
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003260 weston_plane_init(&plane, 0, 0);
3261 surface->plane = &plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003262 weston_surface_update_transform(surface);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003263 shell->debug_repaint_surface = surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003264 surface->plane = &compositor->primary_plane;
3265 weston_plane_release(&plane);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003266 }
3267}
3268
3269static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003270force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3271 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003272{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003273 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003274 struct wl_client *client;
3275 pid_t pid;
3276 uid_t uid;
3277 gid_t gid;
3278
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003279 focus_surface = seat->keyboard->focus;
3280 if (!focus_surface)
3281 return;
3282
3283 client = focus_surface->resource.client;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003284 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003285
Daniel Stone325fc2d2012-05-30 16:31:58 +01003286 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003287}
3288
3289static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003290workspace_up_binding(struct wl_seat *seat, uint32_t time,
3291 uint32_t key, void *data)
3292{
3293 struct desktop_shell *shell = data;
3294 unsigned int new_index = shell->workspaces.current;
3295
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003296 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003297 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003298 if (new_index != 0)
3299 new_index--;
3300
3301 change_workspace(shell, new_index);
3302}
3303
3304static void
3305workspace_down_binding(struct wl_seat *seat, uint32_t time,
3306 uint32_t key, void *data)
3307{
3308 struct desktop_shell *shell = data;
3309 unsigned int new_index = shell->workspaces.current;
3310
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003311 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003312 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003313 if (new_index < shell->workspaces.num - 1)
3314 new_index++;
3315
3316 change_workspace(shell, new_index);
3317}
3318
3319static void
3320workspace_f_binding(struct wl_seat *seat, uint32_t time,
3321 uint32_t key, void *data)
3322{
3323 struct desktop_shell *shell = data;
3324 unsigned int new_index;
3325
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003326 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003327 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003328 new_index = key - KEY_F1;
3329 if (new_index >= shell->workspaces.num)
3330 new_index = shell->workspaces.num - 1;
3331
3332 change_workspace(shell, new_index);
3333}
3334
3335
3336static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003337shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003338{
Tiago Vignattibe143262012-04-16 17:31:41 +03003339 struct desktop_shell *shell =
3340 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003341 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003342
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003343 if (shell->child.client)
3344 wl_client_destroy(shell->child.client);
3345
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003346 wl_list_remove(&shell->lock_listener.link);
3347 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003348 wl_list_remove(&shell->show_input_panel_listener.link);
3349 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003350
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003351 wl_array_for_each(ws, &shell->workspaces.array)
3352 workspace_destroy(*ws);
3353 wl_array_release(&shell->workspaces.array);
3354
Pekka Paalanen3c647232011-12-22 13:43:43 +02003355 free(shell->screensaver.path);
3356 free(shell);
3357}
3358
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003359static void
3360shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3361{
3362 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003363 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003364
3365 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003366 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3367 MODIFIER_CTRL | MODIFIER_ALT,
3368 terminate_binding, ec);
3369 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3370 click_to_activate_binding,
3371 shell);
3372 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3373 MODIFIER_SUPER | MODIFIER_ALT,
3374 surface_opacity_binding, NULL);
3375 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3376 MODIFIER_SUPER, zoom_axis_binding,
3377 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003378
3379 /* configurable bindings */
3380 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003381 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3382 zoom_key_binding, NULL);
3383 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3384 zoom_key_binding, NULL);
3385 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3386 shell);
3387 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3388 resize_binding, shell);
3389 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3390 rotate_binding, NULL);
3391 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3392 shell);
3393 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3394 ec);
3395 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3396 backlight_binding, ec);
3397 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3398 ec);
3399 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3400 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003401 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003402 debug_repaint_binding, shell);
3403 weston_compositor_add_key_binding(ec, KEY_K, mod,
3404 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003405 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3406 workspace_up_binding, shell);
3407 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3408 workspace_down_binding, shell);
3409
3410 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3411 if (shell->workspaces.num > 1) {
3412 num_workspace_bindings = shell->workspaces.num;
3413 if (num_workspace_bindings > 6)
3414 num_workspace_bindings = 6;
3415 for (i = 0; i < num_workspace_bindings; i++)
3416 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3417 workspace_f_binding,
3418 shell);
3419 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003420}
3421
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003422int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003423shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003424
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003425WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003426shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003427{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003428 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003429 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003430 struct workspace **pws;
3431 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003432
3433 shell = malloc(sizeof *shell);
3434 if (shell == NULL)
3435 return -1;
3436
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003437 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003438 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003439
3440 shell->destroy_listener.notify = shell_destroy;
3441 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3442 shell->lock_listener.notify = lock;
3443 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3444 shell->unlock_listener.notify = unlock;
3445 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003446 shell->show_input_panel_listener.notify = show_input_panels;
3447 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3448 shell->hide_input_panel_listener.notify = hide_input_panels;
3449 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003450 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003451 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003452 ec->shell_interface.create_shell_surface = create_shell_surface;
3453 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003454 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003455 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003456 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003457
Pekka Paalanen77346a62011-11-30 16:26:35 +02003458 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003459 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003460
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003461 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3462 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003463 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3464 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003465 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003466
3467 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003468
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003469 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003470
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003471 for (i = 0; i < shell->workspaces.num; i++) {
3472 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3473 if (pws == NULL)
3474 return -1;
3475
3476 *pws = workspace_create();
3477 if (*pws == NULL)
3478 return -1;
3479 }
3480 activate_workspace(shell, 0);
3481
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003482 wl_list_init(&shell->workspaces.animation.link);
3483 shell->workspaces.animation.frame = animate_workspace_change_frame;
3484
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003485 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3486 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003487 return -1;
3488
Kristian Høgsberg75840622011-09-06 13:48:16 -04003489 if (wl_display_add_global(ec->wl_display,
3490 &desktop_shell_interface,
3491 shell, bind_desktop_shell) == NULL)
3492 return -1;
3493
Pekka Paalanen6e168112011-11-24 11:34:05 +02003494 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3495 shell, bind_screensaver) == NULL)
3496 return -1;
3497
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003498 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3499 shell, bind_input_panel) == NULL)
3500 return -1;
3501
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003502 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003503 if (launch_desktop_shell_process(shell) != 0)
3504 return -1;
3505
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003506 wl_list_for_each(seat, &ec->seat_list, link)
3507 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003508
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003509 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003510
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003511 return 0;
3512}