blob: bfb2978c6e41efae65ce6dc25ce0ff666af6feaf [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Jonas Ådahle3cddce2012-06-13 00:01:22 +020060struct workspace {
61 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020062
63 struct wl_list focus_list;
64 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020065};
66
Tiago Vignattibe143262012-04-16 17:31:41 +030067struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040069
70 struct wl_listener lock_listener;
71 struct wl_listener unlock_listener;
72 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020073 struct wl_listener show_input_panel_listener;
74 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020075
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050076 struct weston_layer fullscreen_layer;
77 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050078 struct weston_layer background_layer;
79 struct weston_layer lock_layer;
80
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040081 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030082 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040083
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020084 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050085 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020086 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020087 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020088
89 unsigned deathcount;
90 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020091 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020092
93 bool locked;
94 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020095
Kristian Høgsberg730c94d2012-06-26 21:44:35 -040096 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050097 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010098
Pekka Paalanen77346a62011-11-30 16:26:35 +020099 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200100 struct wl_array array;
101 unsigned int current;
102 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200103
104 struct weston_animation animation;
105 int anim_dir;
106 uint32_t anim_timestamp;
107 double anim_current;
108 struct workspace *anim_from;
109 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200110 } workspaces;
111
112 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200113 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200114 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200115 struct wl_resource *binding;
116 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500117 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200118 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500119
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200120 struct {
121 struct wl_resource *binding;
122 struct wl_list surfaces;
123 } input_panel;
124
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300125 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800126 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500127 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400128};
129
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500130enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200131 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500132 SHELL_SURFACE_TOPLEVEL,
133 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800135 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200137};
138
Scott Moreauff1db4a2012-04-17 19:06:18 -0600139struct ping_timer {
140 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600141 uint32_t serial;
142};
143
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200144struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200145 struct wl_resource resource;
146
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500147 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200148 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400149 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300150 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200151
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400152 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400153 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500154 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800155 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800156 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600157 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100158
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500159 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200160 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500161 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200162 } rotation;
163
164 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700165 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500166 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200167 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500168 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100169 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400170 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500171 } popup;
172
Alex Wu4539b082012-03-01 12:57:46 +0800173 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300174 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400175 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300176 } transient;
177
178 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800179 enum wl_shell_surface_fullscreen_method type;
180 struct weston_transform transform; /* matrix from x, y */
181 uint32_t framerate;
182 struct weston_surface *black_surface;
183 } fullscreen;
184
Scott Moreauff1db4a2012-04-17 19:06:18 -0600185 struct ping_timer *ping_timer;
186
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200187 struct weston_transform workspace_transform;
188
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500189 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100191 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400192
193 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200194};
195
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300196struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700197 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198 struct shell_surface *shsurf;
199 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300200 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201};
202
203struct weston_move_grab {
204 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100205 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206};
207
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500210 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400212 GLfloat x;
213 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 } center;
215};
216
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400217static void
218activate(struct desktop_shell *shell, struct weston_surface *es,
219 struct weston_seat *seat);
220
221static struct workspace *
222get_current_workspace(struct desktop_shell *shell);
223
Alex Wubd3354b2012-04-17 17:20:49 +0800224static struct shell_surface *
225get_shell_surface(struct weston_surface *surface);
226
227static struct desktop_shell *
228shell_surface_get_shell(struct shell_surface *shsurf);
229
230static bool
231shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
232{
233 struct desktop_shell *shell;
234 struct weston_surface *top_fs_es;
235
236 shell = shell_surface_get_shell(shsurf);
237
238 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
239 return false;
240
241 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
242 struct weston_surface,
243 layer_link);
244 return (shsurf == get_shell_surface(top_fs_es));
245}
246
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500247static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400248destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300249{
250 struct shell_grab *grab;
251
252 grab = container_of(listener, struct shell_grab,
253 shsurf_destroy_listener);
254
255 grab->shsurf = NULL;
256}
257
258static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300259shell_grab_start(struct shell_grab *grab,
260 const struct wl_pointer_grab_interface *interface,
261 struct shell_surface *shsurf,
262 struct wl_pointer *pointer,
263 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300264{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300265 struct desktop_shell *shell = shsurf->shell;
266
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300267 grab->grab.interface = interface;
268 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400269 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
270 wl_signal_add(&shsurf->resource.destroy_signal,
271 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300273 grab->pointer = pointer;
274 grab->grab.focus = &shsurf->surface->surface;
275
276 wl_pointer_start_grab(pointer, &grab->grab);
277 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
278 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
279 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280}
281
282static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300283shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400285 if (grab->shsurf)
286 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300287
288 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289}
290
291static void
Alex Wu4539b082012-03-01 12:57:46 +0800292center_on_output(struct weston_surface *surface,
293 struct weston_output *output);
294
Daniel Stone496ca172012-05-30 16:31:42 +0100295static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300296get_modifier(char *modifier)
297{
298 if (!modifier)
299 return MODIFIER_SUPER;
300
301 if (!strcmp("ctrl", modifier))
302 return MODIFIER_CTRL;
303 else if (!strcmp("alt", modifier))
304 return MODIFIER_ALT;
305 else if (!strcmp("super", modifier))
306 return MODIFIER_SUPER;
307 else
308 return MODIFIER_SUPER;
309}
310
Juan Zhaoe10d2792012-04-25 19:09:52 +0800311static enum animation_type
312get_animation_type(char *animation)
313{
314 if (!animation)
315 return ANIMATION_NONE;
316
317 if (!strcmp("zoom", animation))
318 return ANIMATION_ZOOM;
319 else if (!strcmp("fade", animation))
320 return ANIMATION_FADE;
321 else
322 return ANIMATION_NONE;
323}
324
Alex Wu4539b082012-03-01 12:57:46 +0800325static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300326shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200327{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200328 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200329 char *path = NULL;
330 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200331 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300332 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800333 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400335 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300336 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800337 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200338 { "num-workspaces",
339 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200340 };
341
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 struct config_key saver_keys[] = {
343 { "path", CONFIG_KEY_STRING, &path },
344 { "duration", CONFIG_KEY_INTEGER, &duration },
345 };
346
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200347 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400348 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200349 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
350 };
351
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200352 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500353 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200354 free(config_file);
355
Pekka Paalanen7296e792011-12-07 16:22:00 +0200356 shell->screensaver.path = path;
357 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300358 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800359 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200360 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
361}
362
363static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200364focus_state_destroy(struct focus_state *state)
365{
366 wl_list_remove(&state->seat_destroy_listener.link);
367 wl_list_remove(&state->surface_destroy_listener.link);
368 free(state);
369}
370
371static void
372focus_state_seat_destroy(struct wl_listener *listener, void *data)
373{
374 struct focus_state *state = container_of(listener,
375 struct focus_state,
376 seat_destroy_listener);
377
378 wl_list_remove(&state->link);
379 focus_state_destroy(state);
380}
381
382static void
383focus_state_surface_destroy(struct wl_listener *listener, void *data)
384{
385 struct focus_state *state = container_of(listener,
386 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400387 surface_destroy_listener);
Jonas Ådahl04769742012-06-13 00:01:24 +0200388
389 wl_list_remove(&state->link);
390 focus_state_destroy(state);
391}
392
393static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400394focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200395{
Jonas Ådahl04769742012-06-13 00:01:24 +0200396 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200397
398 state = malloc(sizeof *state);
399 if (state == NULL)
400 return NULL;
401
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400402 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200403 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400404 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200405
406 state->seat_destroy_listener.notify = focus_state_seat_destroy;
407 state->surface_destroy_listener.notify = focus_state_surface_destroy;
408 wl_signal_add(&seat->seat.destroy_signal,
409 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400410 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200411
412 return state;
413}
414
415static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400416restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200417{
418 struct focus_state *state, *next;
419
420 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
421 if (state->keyboard_focus)
422 wl_keyboard_set_focus(state->seat->seat.keyboard,
423 &state->keyboard_focus->surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200424 }
425}
426
427static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200428workspace_destroy(struct workspace *ws)
429{
Jonas Ådahl04769742012-06-13 00:01:24 +0200430 struct focus_state *state, *next;
431
432 wl_list_for_each_safe(state, next, &ws->focus_list, link)
433 focus_state_destroy(state);
434
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200435 free(ws);
436}
437
Jonas Ådahl04769742012-06-13 00:01:24 +0200438static void
439seat_destroyed(struct wl_listener *listener, void *data)
440{
441 struct weston_seat *seat = data;
442 struct focus_state *state, *next;
443 struct workspace *ws = container_of(listener,
444 struct workspace,
445 seat_destroyed_listener);
446
447 wl_list_for_each_safe(state, next, &ws->focus_list, link)
448 if (state->seat == seat)
449 wl_list_remove(&state->link);
450}
451
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200452static struct workspace *
453workspace_create(void)
454{
455 struct workspace *ws = malloc(sizeof *ws);
456 if (ws == NULL)
457 return NULL;
458
459 weston_layer_init(&ws->layer, NULL);
460
Jonas Ådahl04769742012-06-13 00:01:24 +0200461 wl_list_init(&ws->focus_list);
462 wl_list_init(&ws->seat_destroyed_listener.link);
463 ws->seat_destroyed_listener.notify = seat_destroyed;
464
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200465 return ws;
466}
467
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200468static int
469workspace_is_empty(struct workspace *ws)
470{
471 return wl_list_empty(&ws->layer.surface_list);
472}
473
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200474static struct workspace *
475get_workspace(struct desktop_shell *shell, unsigned int index)
476{
477 struct workspace **pws = shell->workspaces.array.data;
478 pws += index;
479 return *pws;
480}
481
482static struct workspace *
483get_current_workspace(struct desktop_shell *shell)
484{
485 return get_workspace(shell, shell->workspaces.current);
486}
487
488static void
489activate_workspace(struct desktop_shell *shell, unsigned int index)
490{
491 struct workspace *ws;
492
493 ws = get_workspace(shell, index);
494 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
495
496 shell->workspaces.current = index;
497}
498
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200499static unsigned int
500get_output_height(struct weston_output *output)
501{
502 return abs(output->region.extents.y1 - output->region.extents.y2);
503}
504
505static void
506surface_translate(struct weston_surface *surface, double d)
507{
508 struct shell_surface *shsurf = get_shell_surface(surface);
509 struct weston_transform *transform;
510
511 transform = &shsurf->workspace_transform;
512 if (wl_list_empty(&transform->link))
513 wl_list_insert(surface->geometry.transformation_list.prev,
514 &shsurf->workspace_transform.link);
515
516 weston_matrix_init(&shsurf->workspace_transform.matrix);
517 weston_matrix_translate(&shsurf->workspace_transform.matrix,
518 0.0, d, 0.0);
519 surface->geometry.dirty = 1;
520}
521
522static void
523workspace_translate_out(struct workspace *ws, double fraction)
524{
525 struct weston_surface *surface;
526 unsigned int height;
527 double d;
528
529 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
530 height = get_output_height(surface->output);
531 d = height * fraction;
532
533 surface_translate(surface, d);
534 }
535}
536
537static void
538workspace_translate_in(struct workspace *ws, double fraction)
539{
540 struct weston_surface *surface;
541 unsigned int height;
542 double d;
543
544 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
545 height = get_output_height(surface->output);
546
547 if (fraction > 0)
548 d = -(height - height * fraction);
549 else
550 d = height + height * fraction;
551
552 surface_translate(surface, d);
553 }
554}
555
556static void
557workspace_damage_all_surfaces(struct workspace *ws)
558{
559 struct weston_surface *surface;
560
561 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
562 weston_surface_damage(surface);
563}
564
565static void
566reverse_workspace_change_animation(struct desktop_shell *shell,
567 unsigned int index,
568 struct workspace *from,
569 struct workspace *to)
570{
571 shell->workspaces.current = index;
572
573 shell->workspaces.anim_to = to;
574 shell->workspaces.anim_from = from;
575 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
576 shell->workspaces.anim_timestamp = 0;
577
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400578 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200579
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200580 workspace_damage_all_surfaces(from);
581 workspace_damage_all_surfaces(to);
582}
583
584static void
585workspace_deactivate_transforms(struct workspace *ws)
586{
587 struct weston_surface *surface;
588 struct shell_surface *shsurf;
589
590 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
591 shsurf = get_shell_surface(surface);
592 wl_list_remove(&shsurf->workspace_transform.link);
593 wl_list_init(&shsurf->workspace_transform.link);
594 shsurf->surface->geometry.dirty = 1;
595 }
596}
597
598static void
599finish_workspace_change_animation(struct desktop_shell *shell,
600 struct workspace *from,
601 struct workspace *to)
602{
603 workspace_damage_all_surfaces(from);
604 workspace_damage_all_surfaces(to);
605
606 wl_list_remove(&shell->workspaces.animation.link);
607 workspace_deactivate_transforms(from);
608 workspace_deactivate_transforms(to);
609 shell->workspaces.anim_to = NULL;
610
611 wl_list_remove(&shell->workspaces.anim_from->layer.link);
612}
613
614static void
615animate_workspace_change_frame(struct weston_animation *animation,
616 struct weston_output *output, uint32_t msecs)
617{
618 struct desktop_shell *shell =
619 container_of(animation, struct desktop_shell,
620 workspaces.animation);
621 struct workspace *from = shell->workspaces.anim_from;
622 struct workspace *to = shell->workspaces.anim_to;
623 uint32_t t;
624 double x, y;
625
626 if (workspace_is_empty(from) && workspace_is_empty(to)) {
627 finish_workspace_change_animation(shell, from, to);
628 return;
629 }
630
631 if (shell->workspaces.anim_timestamp == 0) {
632 if (shell->workspaces.anim_current == 0.0)
633 shell->workspaces.anim_timestamp = msecs;
634 else
635 shell->workspaces.anim_timestamp =
636 msecs -
637 /* Invers of movement function 'y' below. */
638 (asin(1.0 - shell->workspaces.anim_current) *
639 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
640 M_2_PI);
641 }
642
643 t = msecs - shell->workspaces.anim_timestamp;
644
645 /*
646 * x = [0, π/2]
647 * y(x) = sin(x)
648 */
649 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
650 y = sin(x);
651
652 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
653 workspace_damage_all_surfaces(from);
654 workspace_damage_all_surfaces(to);
655
656 workspace_translate_out(from, shell->workspaces.anim_dir * y);
657 workspace_translate_in(to, shell->workspaces.anim_dir * y);
658 shell->workspaces.anim_current = y;
659
660 workspace_damage_all_surfaces(from);
661 workspace_damage_all_surfaces(to);
662 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200663 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200664 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200665}
666
667static void
668animate_workspace_change(struct desktop_shell *shell,
669 unsigned int index,
670 struct workspace *from,
671 struct workspace *to)
672{
673 struct weston_output *output;
674
675 int dir;
676
677 if (index > shell->workspaces.current)
678 dir = -1;
679 else
680 dir = 1;
681
682 shell->workspaces.current = index;
683
684 shell->workspaces.anim_dir = dir;
685 shell->workspaces.anim_from = from;
686 shell->workspaces.anim_to = to;
687 shell->workspaces.anim_current = 0.0;
688 shell->workspaces.anim_timestamp = 0;
689
690 output = container_of(shell->compositor->output_list.next,
691 struct weston_output, link);
692 wl_list_insert(&output->animation_list,
693 &shell->workspaces.animation.link);
694
695 wl_list_insert(&from->layer.link, &to->layer.link);
696
697 workspace_translate_in(to, 0);
698
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400699 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200700
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200701 workspace_damage_all_surfaces(from);
702 workspace_damage_all_surfaces(to);
703}
704
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200705static void
706change_workspace(struct desktop_shell *shell, unsigned int index)
707{
708 struct workspace *from;
709 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200710
711 if (index == shell->workspaces.current)
712 return;
713
714 /* Don't change workspace when there is any fullscreen surfaces. */
715 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
716 return;
717
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200718 from = get_current_workspace(shell);
719 to = get_workspace(shell, index);
720
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200721 if (shell->workspaces.anim_from == to &&
722 shell->workspaces.anim_to == from) {
723 reverse_workspace_change_animation(shell, index, from, to);
724 return;
725 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200726
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200727 if (shell->workspaces.anim_to != NULL)
728 finish_workspace_change_animation(shell,
729 shell->workspaces.anim_from,
730 shell->workspaces.anim_to);
731
732 if (workspace_is_empty(to) && workspace_is_empty(from)) {
733 shell->workspaces.current = index;
734 wl_list_insert(&from->layer.link, &to->layer.link);
735 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200736
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400737 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200738 }
739 else
740 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200741}
742
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200743static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400744noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100745 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500746{
747 grab->focus = NULL;
748}
749
750static void
Scott Moreau447013d2012-02-18 05:05:29 -0700751move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100752 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500753{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500754 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100755 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300756 struct shell_surface *shsurf = move->base.shsurf;
757 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100758 int dx = wl_fixed_to_int(pointer->x + move->dx);
759 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300760
761 if (!shsurf)
762 return;
763
764 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500765
Daniel Stone103db7f2012-05-08 17:17:55 +0100766 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200767 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400768
769 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500770}
771
772static void
Scott Moreau447013d2012-02-18 05:05:29 -0700773move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100774 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500775{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300776 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
777 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100778 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100779 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500780
Daniel Stone4dbadb12012-05-30 16:31:51 +0100781 if (pointer->button_count == 0 &&
782 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300783 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500784 free(grab);
785 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500786}
787
Scott Moreau447013d2012-02-18 05:05:29 -0700788static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500789 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500790 move_grab_motion,
791 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500792};
793
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600794static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400795busy_cursor_grab_focus(struct wl_pointer_grab *base,
796 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600797{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400798 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600799
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400800 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300801 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400802 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600803 }
804}
805
806static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400807busy_cursor_grab_motion(struct wl_pointer_grab *grab,
808 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600809{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400810}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600811
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400812static void
813busy_cursor_grab_button(struct wl_pointer_grab *grab,
814 uint32_t time, uint32_t button, uint32_t state)
815{
816}
817
818static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
819 busy_cursor_grab_focus,
820 busy_cursor_grab_motion,
821 busy_cursor_grab_button,
822};
823
824static void
825set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
826{
827 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828
829 grab = malloc(sizeof *grab);
830 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600831 return;
832
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300833 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
834 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400835}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600836
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400837static void
838end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
839{
840 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
841
842 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300843 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400844 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600845 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600846}
847
Scott Moreau9521d5e2012-04-19 13:06:17 -0600848static void
849ping_timer_destroy(struct shell_surface *shsurf)
850{
851 if (!shsurf || !shsurf->ping_timer)
852 return;
853
854 if (shsurf->ping_timer->source)
855 wl_event_source_remove(shsurf->ping_timer->source);
856
857 free(shsurf->ping_timer);
858 shsurf->ping_timer = NULL;
859}
860
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400861static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600862ping_timeout_handler(void *data)
863{
864 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400865 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600866
Scott Moreau9521d5e2012-04-19 13:06:17 -0600867 /* Client is not responding */
868 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400869
870 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
871 if (seat->seat.pointer->focus == &shsurf->surface->surface)
872 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600873
874 return 1;
875}
876
877static void
878ping_handler(struct weston_surface *surface, uint32_t serial)
879{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400880 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600881 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400882 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600883
884 if (!shsurf)
885 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400886 if (!shsurf->resource.client)
887 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600888
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +0300889 if (shsurf->surface == shsurf->shell->grab_surface)
890 return;
891
Scott Moreauff1db4a2012-04-17 19:06:18 -0600892 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300893 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600894 if (!shsurf->ping_timer)
895 return;
896
897 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600898 loop = wl_display_get_event_loop(surface->compositor->wl_display);
899 shsurf->ping_timer->source =
900 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
901 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
902
903 wl_shell_surface_send_ping(&shsurf->resource, serial);
904 }
905}
906
907static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400908handle_pointer_focus(struct wl_listener *listener, void *data)
909{
910 struct wl_pointer *pointer = data;
911 struct weston_surface *surface =
912 (struct weston_surface *) pointer->focus;
913 struct weston_compositor *compositor;
914 struct shell_surface *shsurf;
915 uint32_t serial;
916
917 if (!surface)
918 return;
919
920 compositor = surface->compositor;
921 shsurf = get_shell_surface(surface);
922
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300923 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400924 set_busy_cursor(shsurf, pointer);
925 } else {
926 serial = wl_display_next_serial(compositor->wl_display);
927 ping_handler(surface, serial);
928 }
929}
930
931static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600932shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
933 uint32_t serial)
934{
935 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400936 struct desktop_shell *shell = shsurf->shell;
937 struct weston_seat *seat;
938 struct weston_compositor *ec = shsurf->surface->compositor;
939 struct wl_pointer *pointer;
940 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600941
Scott Moreauff1db4a2012-04-17 19:06:18 -0600942 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400943 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600944 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400945 if (was_unresponsive) {
946 /* Received pong from previously unresponsive client */
947 wl_list_for_each(seat, &ec->seat_list, link) {
948 pointer = seat->seat.pointer;
949 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300950 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400951 pointer->current ==
952 &shsurf->surface->surface)
953 end_busy_cursor(shsurf, pointer);
954 }
955 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600956 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600957 }
958}
959
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400960static void
961shell_surface_set_title(struct wl_client *client,
962 struct wl_resource *resource, const char *title)
963{
964 struct shell_surface *shsurf = resource->data;
965
966 free(shsurf->title);
967 shsurf->title = strdup(title);
968}
969
970static void
971shell_surface_set_class(struct wl_client *client,
972 struct wl_resource *resource, const char *class)
973{
974 struct shell_surface *shsurf = resource->data;
975
976 free(shsurf->class);
977 shsurf->class = strdup(class);
978}
979
Scott Moreauff1db4a2012-04-17 19:06:18 -0600980static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400981surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500983 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300984
985 if (!shsurf)
986 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500987
Ander Conselvan de Oliveira00d17bb2012-06-28 18:08:06 +0300988 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
989 return 0;
990
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500991 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400992 if (!move)
993 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500994
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400995 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100996 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400997 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100998 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500999
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001000 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1001 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001002
1003 return 0;
1004}
1005
1006static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001007shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001008 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001009{
Daniel Stone37816df2012-05-16 18:45:18 +01001010 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001011 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001012
Daniel Stone37816df2012-05-16 18:45:18 +01001013 if (ws->seat.pointer->button_count == 0 ||
1014 ws->seat.pointer->grab_serial != serial ||
1015 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001016 return;
1017
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001018 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001019 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020}
1021
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001022struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001023 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001024 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001025 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001026};
1027
1028static void
Scott Moreau447013d2012-02-18 05:05:29 -07001029resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001030 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001031{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001032 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001033 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001034 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001035 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001036 wl_fixed_t from_x, from_y;
1037 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001038
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001039 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001040 return;
1041
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001042 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001043 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001044 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001045 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001046 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001047
Daniel Stone103db7f2012-05-08 17:17:55 +01001048 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001049 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001050 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001051 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001052 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053 }
1054
Daniel Stone103db7f2012-05-08 17:17:55 +01001055 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001056 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001057 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001058 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001059 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060 }
1061
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001062 shsurf->client->send_configure(shsurf->surface,
1063 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064}
1065
1066static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001067send_configure(struct weston_surface *surface,
1068 uint32_t edges, int32_t width, int32_t height)
1069{
1070 struct shell_surface *shsurf = get_shell_surface(surface);
1071
1072 wl_shell_surface_send_configure(&shsurf->resource,
1073 edges, width, height);
1074}
1075
1076static const struct weston_shell_client shell_client = {
1077 send_configure
1078};
1079
1080static void
Scott Moreau447013d2012-02-18 05:05:29 -07001081resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001082 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001083{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001084 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001085 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001086 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001087
Daniel Stone4dbadb12012-05-30 16:31:51 +01001088 if (pointer->button_count == 0 &&
1089 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001090 shell_grab_end(&resize->base);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001091 free(grab);
1092 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001093}
1094
Scott Moreau447013d2012-02-18 05:05:29 -07001095static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001096 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001097 resize_grab_motion,
1098 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001099};
1100
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001101static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001102surface_resize(struct shell_surface *shsurf,
1103 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001104{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001105 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001106
Alex Wu4539b082012-03-01 12:57:46 +08001107 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1108 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001109
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001110 if (edges == 0 || edges > 15 ||
1111 (edges & 3) == 3 || (edges & 12) == 12)
1112 return 0;
1113
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001114 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001115 if (!resize)
1116 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001117
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001118 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001119 resize->width = shsurf->surface->geometry.width;
1120 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001121
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001122 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1123 ws->seat.pointer, edges);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001124
1125 return 0;
1126}
1127
1128static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001129shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001130 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001131 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001132{
Daniel Stone37816df2012-05-16 18:45:18 +01001133 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001134 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001135
Alex Wu4539b082012-03-01 12:57:46 +08001136 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1137 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001138
Daniel Stone37816df2012-05-16 18:45:18 +01001139 if (ws->seat.pointer->button_count == 0 ||
1140 ws->seat.pointer->grab_serial != serial ||
1141 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001142 return;
1143
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001144 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001145 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146}
1147
Juan Zhao96879df2012-02-07 08:45:41 +08001148static struct weston_output *
1149get_default_output(struct weston_compositor *compositor)
1150{
1151 return container_of(compositor->output_list.next,
1152 struct weston_output, link);
1153}
1154
Alex Wu4539b082012-03-01 12:57:46 +08001155static void
1156shell_unset_fullscreen(struct shell_surface *shsurf)
1157{
1158 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001159 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1160 shell_surface_is_top_fullscreen(shsurf)) {
1161 weston_output_switch_mode(shsurf->fullscreen_output,
1162 shsurf->fullscreen_output->origin);
1163 }
Alex Wu4539b082012-03-01 12:57:46 +08001164 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1165 shsurf->fullscreen.framerate = 0;
1166 wl_list_remove(&shsurf->fullscreen.transform.link);
1167 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001168 if (shsurf->fullscreen.black_surface)
1169 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001170 shsurf->fullscreen.black_surface = NULL;
1171 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001172 weston_surface_set_position(shsurf->surface,
1173 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001174 if (shsurf->saved_rotation_valid) {
1175 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1176 &shsurf->rotation.transform.link);
1177 shsurf->saved_rotation_valid = false;
1178 }
Alex Wu4539b082012-03-01 12:57:46 +08001179}
1180
Pekka Paalanen98262232011-12-01 10:42:22 +02001181static int
1182reset_shell_surface_type(struct shell_surface *surface)
1183{
1184 switch (surface->type) {
1185 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001186 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001187 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001188 case SHELL_SURFACE_MAXIMIZED:
1189 surface->output = get_default_output(surface->surface->compositor);
1190 weston_surface_set_position(surface->surface,
1191 surface->saved_x,
1192 surface->saved_y);
1193 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001194 case SHELL_SURFACE_NONE:
1195 case SHELL_SURFACE_TOPLEVEL:
1196 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001197 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001198 break;
1199 }
1200
1201 surface->type = SHELL_SURFACE_NONE;
1202 return 0;
1203}
1204
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001205static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001206set_surface_type(struct shell_surface *shsurf)
1207{
1208 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001209 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001210
1211 reset_shell_surface_type(shsurf);
1212
1213 shsurf->type = shsurf->next_type;
1214 shsurf->next_type = SHELL_SURFACE_NONE;
1215
1216 switch (shsurf->type) {
1217 case SHELL_SURFACE_TOPLEVEL:
1218 break;
1219 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001220 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001221 pes->geometry.x + shsurf->transient.x,
1222 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001223 break;
1224
1225 case SHELL_SURFACE_MAXIMIZED:
1226 shsurf->saved_x = surface->geometry.x;
1227 shsurf->saved_y = surface->geometry.y;
1228 shsurf->saved_position_valid = true;
1229 break;
1230
1231 case SHELL_SURFACE_FULLSCREEN:
1232 shsurf->saved_x = surface->geometry.x;
1233 shsurf->saved_y = surface->geometry.y;
1234 shsurf->saved_position_valid = true;
1235
1236 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1237 wl_list_remove(&shsurf->rotation.transform.link);
1238 wl_list_init(&shsurf->rotation.transform.link);
1239 shsurf->surface->geometry.dirty = 1;
1240 shsurf->saved_rotation_valid = true;
1241 }
1242 break;
1243
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001244 default:
1245 break;
1246 }
1247}
1248
1249static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001250set_toplevel(struct shell_surface *shsurf)
1251{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001253}
1254
1255static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001256shell_surface_set_toplevel(struct wl_client *client,
1257 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001258{
Pekka Paalanen98262232011-12-01 10:42:22 +02001259 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001260
Tiago Vignattibc052c92012-04-19 16:18:18 +03001261 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001262}
1263
1264static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001265set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001266 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001267{
1268 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001269 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001270 shsurf->transient.x = x;
1271 shsurf->transient.y = y;
1272 shsurf->transient.flags = flags;
1273 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1274}
1275
1276static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001277shell_surface_set_transient(struct wl_client *client,
1278 struct wl_resource *resource,
1279 struct wl_resource *parent_resource,
1280 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001281{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001282 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001283 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001284
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001285 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001286}
1287
Tiago Vignattibe143262012-04-16 17:31:41 +03001288static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001289shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001290{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001291 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001292}
1293
1294static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001295get_output_panel_height(struct desktop_shell *shell,
1296 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001297{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001298 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001299 int panel_height = 0;
1300
1301 if (!output)
1302 return 0;
1303
Juan Zhao4ab94682012-07-09 22:24:09 -07001304 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001305 if (surface->output == output) {
1306 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001307 break;
1308 }
1309 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001310
Juan Zhao96879df2012-02-07 08:45:41 +08001311 return panel_height;
1312}
1313
1314static void
1315shell_surface_set_maximized(struct wl_client *client,
1316 struct wl_resource *resource,
1317 struct wl_resource *output_resource )
1318{
1319 struct shell_surface *shsurf = resource->data;
1320 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001321 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001322 uint32_t edges = 0, panel_height = 0;
1323
1324 /* get the default output, if the client set it as NULL
1325 check whether the ouput is available */
1326 if (output_resource)
1327 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001328 else if (es->output)
1329 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001330 else
1331 shsurf->output = get_default_output(es->compositor);
1332
Tiago Vignattibe143262012-04-16 17:31:41 +03001333 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001334 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001335 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001336
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001337 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001338 shsurf->output->current->width,
1339 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001340
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001341 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001342}
1343
Alex Wu21858432012-04-01 20:13:08 +08001344static void
1345black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1346
Alex Wu4539b082012-03-01 12:57:46 +08001347static struct weston_surface *
1348create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001349 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001350 GLfloat x, GLfloat y, int w, int h)
1351{
1352 struct weston_surface *surface = NULL;
1353
1354 surface = weston_surface_create(ec);
1355 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001356 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001357 return NULL;
1358 }
1359
Alex Wu21858432012-04-01 20:13:08 +08001360 surface->configure = black_surface_configure;
1361 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001362 weston_surface_configure(surface, x, y, w, h);
1363 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1364 return surface;
1365}
1366
1367/* Create black surface and append it to the associated fullscreen surface.
1368 * Handle size dismatch and positioning according to the method. */
1369static void
1370shell_configure_fullscreen(struct shell_surface *shsurf)
1371{
1372 struct weston_output *output = shsurf->fullscreen_output;
1373 struct weston_surface *surface = shsurf->surface;
1374 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001375 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001376
Alex Wu4539b082012-03-01 12:57:46 +08001377 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001378 shsurf->fullscreen.black_surface =
1379 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001380 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001381 output->x, output->y,
1382 output->current->width,
1383 output->current->height);
1384
1385 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1386 wl_list_insert(&surface->layer_link,
1387 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001388 shsurf->fullscreen.black_surface->output = output;
1389
1390 switch (shsurf->fullscreen.type) {
1391 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001392 if (surface->buffer)
1393 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001394 break;
1395 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1396 matrix = &shsurf->fullscreen.transform.matrix;
1397 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001398
1399 output_aspect = (float) output->current->width /
1400 (float) output->current->height;
1401 surface_aspect = (float) surface->geometry.width /
1402 (float) surface->geometry.height;
1403 if (output_aspect < surface_aspect)
1404 scale = (float) output->current->width /
1405 (float) surface->geometry.width;
1406 else
1407 scale = (float) output->current->height /
1408 (float) surface->geometry.height;
1409
Alex Wu4539b082012-03-01 12:57:46 +08001410 weston_matrix_scale(matrix, scale, scale, 1);
1411 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001412 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001413 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001414 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1415 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1416 weston_surface_set_position(surface, x, y);
1417
Alex Wu4539b082012-03-01 12:57:46 +08001418 break;
1419 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001420 if (shell_surface_is_top_fullscreen(shsurf)) {
1421 struct weston_mode mode = {0,
1422 surface->geometry.width,
1423 surface->geometry.height,
1424 shsurf->fullscreen.framerate};
1425
1426 if (weston_output_switch_mode(output, &mode) == 0) {
1427 weston_surface_configure(shsurf->fullscreen.black_surface,
1428 output->x, output->y,
1429 output->current->width,
1430 output->current->height);
1431 weston_surface_set_position(surface, output->x, output->y);
1432 break;
1433 }
1434 }
Alex Wu4539b082012-03-01 12:57:46 +08001435 break;
1436 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1437 break;
1438 default:
1439 break;
1440 }
1441}
1442
1443/* make the fullscreen and black surface at the top */
1444static void
1445shell_stack_fullscreen(struct shell_surface *shsurf)
1446{
Alex Wubd3354b2012-04-17 17:20:49 +08001447 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001448 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001449 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001450
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001451 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001452 wl_list_insert(&shell->fullscreen_layer.surface_list,
1453 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001454 weston_surface_damage(surface);
1455
1456 if (!shsurf->fullscreen.black_surface)
1457 shsurf->fullscreen.black_surface =
1458 create_black_surface(surface->compositor,
1459 surface,
1460 output->x, output->y,
1461 output->current->width,
1462 output->current->height);
1463
1464 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001465 wl_list_insert(&surface->layer_link,
1466 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001467 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001468}
1469
1470static void
1471shell_map_fullscreen(struct shell_surface *shsurf)
1472{
Alex Wu4539b082012-03-01 12:57:46 +08001473 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001474 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001475}
1476
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001477static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001478shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001479 struct wl_resource *resource,
1480 uint32_t method,
1481 uint32_t framerate,
1482 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001483{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001484 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001486
1487 if (output_resource)
1488 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001489 else if (es->output)
1490 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001491 else
1492 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001493
Alex Wu4539b082012-03-01 12:57:46 +08001494 shsurf->fullscreen_output = shsurf->output;
1495 shsurf->fullscreen.type = method;
1496 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001497 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001498
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001499 shsurf->client->send_configure(shsurf->surface, 0,
1500 shsurf->output->current->width,
1501 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001502}
1503
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001504static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001505popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001506 struct wl_surface *surface,
1507 wl_fixed_t x,
1508 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001509{
Daniel Stone37816df2012-05-16 18:45:18 +01001510 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001511 struct shell_surface *priv =
1512 container_of(grab, struct shell_surface, popup.grab);
1513 struct wl_client *client = priv->surface->surface.resource.client;
1514
Pekka Paalanencb108432012-01-19 16:25:40 +02001515 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001516 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001517 grab->focus = surface;
1518 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001519 wl_pointer_set_focus(pointer, NULL,
1520 wl_fixed_from_int(0),
1521 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001522 grab->focus = NULL;
1523 }
1524}
1525
1526static void
Scott Moreau447013d2012-02-18 05:05:29 -07001527popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001528 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001529{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001530 struct wl_resource *resource;
1531
Daniel Stone37816df2012-05-16 18:45:18 +01001532 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001533 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001534 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001535}
1536
1537static void
Scott Moreau447013d2012-02-18 05:05:29 -07001538popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001539 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540{
1541 struct wl_resource *resource;
1542 struct shell_surface *shsurf =
1543 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001544 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001545 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001546 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001547
Daniel Stone37816df2012-05-16 18:45:18 +01001548 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001549 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001550 display = wl_client_get_display(resource->client);
1551 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001552 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001553 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001554 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001555 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001556 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001557 wl_pointer_end_grab(grab->pointer);
1558 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559 }
1560
Daniel Stone4dbadb12012-05-30 16:31:51 +01001561 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562 shsurf->popup.initial_up = 1;
1563}
1564
Scott Moreau447013d2012-02-18 05:05:29 -07001565static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001566 popup_grab_focus,
1567 popup_grab_motion,
1568 popup_grab_button,
1569};
1570
1571static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001572shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001573{
Daniel Stone37816df2012-05-16 18:45:18 +01001574 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001575 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001576 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577
1578 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001579 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001580
Pekka Paalanen938269a2012-02-07 14:19:01 +02001581 weston_surface_update_transform(parent);
1582 if (parent->transform.enabled) {
1583 shsurf->popup.parent_transform.matrix =
1584 parent->transform.matrix;
1585 } else {
1586 /* construct x, y translation matrix */
1587 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1588 shsurf->popup.parent_transform.matrix.d[12] =
1589 parent->geometry.x;
1590 shsurf->popup.parent_transform.matrix.d[13] =
1591 parent->geometry.y;
1592 }
1593 wl_list_insert(es->geometry.transformation_list.prev,
1594 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001595 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001596
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001597 shsurf->popup.initial_up = 0;
1598
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001599 /* We don't require the grab to still be active, but if another
1600 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001601 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1602 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001603 } else {
1604 wl_shell_surface_send_popup_done(&shsurf->resource);
1605 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606}
1607
1608static void
1609shell_surface_set_popup(struct wl_client *client,
1610 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001611 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001612 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001613 struct wl_resource *parent_resource,
1614 int32_t x, int32_t y, uint32_t flags)
1615{
1616 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001618 shsurf->type = SHELL_SURFACE_POPUP;
1619 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001620 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001621 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001622 shsurf->popup.x = x;
1623 shsurf->popup.y = y;
1624}
1625
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001626static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001627 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001628 shell_surface_move,
1629 shell_surface_resize,
1630 shell_surface_set_toplevel,
1631 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001632 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001633 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001634 shell_surface_set_maximized,
1635 shell_surface_set_title,
1636 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001637};
1638
1639static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001640destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001641{
Daniel Stone37816df2012-05-16 18:45:18 +01001642 if (shsurf->popup.grab.pointer)
1643 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001644
Alex Wubd3354b2012-04-17 17:20:49 +08001645 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1646 shell_surface_is_top_fullscreen(shsurf)) {
1647 weston_output_switch_mode(shsurf->fullscreen_output,
1648 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001649 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001650
Alex Wuaa08e2d2012-03-05 11:01:40 +08001651 if (shsurf->fullscreen.black_surface)
1652 weston_surface_destroy(shsurf->fullscreen.black_surface);
1653
Alex Wubd3354b2012-04-17 17:20:49 +08001654 /* As destroy_resource() use wl_list_for_each_safe(),
1655 * we can always remove the listener.
1656 */
1657 wl_list_remove(&shsurf->surface_destroy_listener.link);
1658 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001659 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001660
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661 wl_list_remove(&shsurf->link);
1662 free(shsurf);
1663}
1664
1665static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001666shell_destroy_shell_surface(struct wl_resource *resource)
1667{
1668 struct shell_surface *shsurf = resource->data;
1669
1670 destroy_shell_surface(shsurf);
1671}
1672
1673static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001674shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001675{
1676 struct shell_surface *shsurf = container_of(listener,
1677 struct shell_surface,
1678 surface_destroy_listener);
1679
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001680 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001681 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001682 } else {
1683 wl_signal_emit(&shsurf->resource.destroy_signal,
1684 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001685 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001686 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001687}
1688
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001689static void
1690shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1691
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001692static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001693get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001694{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001695 if (surface->configure == shell_surface_configure)
1696 return surface->private;
1697 else
1698 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001699}
1700
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001701static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001702create_shell_surface(void *shell, struct weston_surface *surface,
1703 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001704{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001705 struct shell_surface *shsurf;
1706
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001707 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001708 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001709 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001710 }
1711
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712 shsurf = calloc(1, sizeof *shsurf);
1713 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001714 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001715 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716 }
1717
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001718 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001719 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001720
Tiago Vignattibc052c92012-04-19 16:18:18 +03001721 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001722 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001723 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001724 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001725 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001726 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1727 shsurf->fullscreen.framerate = 0;
1728 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001729 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001730 wl_list_init(&shsurf->fullscreen.transform.link);
1731
Tiago Vignattibc052c92012-04-19 16:18:18 +03001732 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001733 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1734 wl_signal_add(&surface->surface.resource.destroy_signal,
1735 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001736
1737 /* init link so its safe to always remove it in destroy_shell_surface */
1738 wl_list_init(&shsurf->link);
1739
Pekka Paalanen460099f2012-01-20 16:48:25 +02001740 /* empty when not in use */
1741 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001742 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001743
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001744 wl_list_init(&shsurf->workspace_transform.link);
1745
Pekka Paalanen98262232011-12-01 10:42:22 +02001746 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001747 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001748
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001749 shsurf->client = client;
1750
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001751 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001752}
1753
1754static void
1755shell_get_shell_surface(struct wl_client *client,
1756 struct wl_resource *resource,
1757 uint32_t id,
1758 struct wl_resource *surface_resource)
1759{
1760 struct weston_surface *surface = surface_resource->data;
1761 struct desktop_shell *shell = resource->data;
1762 struct shell_surface *shsurf;
1763
1764 if (get_shell_surface(surface)) {
1765 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001766 WL_DISPLAY_ERROR_INVALID_OBJECT,
1767 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001768 return;
1769 }
1770
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001771 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001772 if (!shsurf) {
1773 wl_resource_post_error(surface_resource,
1774 WL_DISPLAY_ERROR_INVALID_OBJECT,
1775 "surface->configure already set");
1776 return;
1777 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001778
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001779 shsurf->resource.destroy = shell_destroy_shell_surface;
1780 shsurf->resource.object.id = id;
1781 shsurf->resource.object.interface = &wl_shell_surface_interface;
1782 shsurf->resource.object.implementation =
1783 (void (**)(void)) &shell_surface_implementation;
1784 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001785
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001786 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001787}
1788
1789static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001790 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001791};
1792
Kristian Høgsberg07937562011-04-12 17:25:42 -04001793static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001794handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001795{
1796 proc->pid = 0;
1797}
1798
1799static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001800launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001801{
1802 if (shell->screensaver.binding)
1803 return;
1804
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001805 if (!shell->screensaver.path)
1806 return;
1807
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001808 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001809 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001810 return;
1811 }
1812
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001814 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001815 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001816 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001817}
1818
1819static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001820terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001821{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001822 if (shell->screensaver.process.pid == 0)
1823 return;
1824
1825 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001826}
1827
1828static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001829configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001830{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001831 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001832
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001833 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1834 if (s->output == es->output && s != es) {
1835 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001836 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001837 }
1838 }
1839
1840 weston_surface_configure(es, es->output->x, es->output->y,
1841 es->buffer->width, es->buffer->height);
1842
1843 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001844 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001845 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001846 }
1847}
1848
1849static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001850background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1851{
1852 struct desktop_shell *shell = es->private;
1853
1854 configure_static_surface(es, &shell->background_layer);
1855}
1856
1857static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001858desktop_shell_set_background(struct wl_client *client,
1859 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001860 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001861 struct wl_resource *surface_resource)
1862{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001863 struct desktop_shell *shell = resource->data;
1864 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001865
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001866 if (surface->configure) {
1867 wl_resource_post_error(surface_resource,
1868 WL_DISPLAY_ERROR_INVALID_OBJECT,
1869 "surface role already assigned");
1870 return;
1871 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001872
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001873 surface->configure = background_configure;
1874 surface->private = shell;
1875 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001876 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001877 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878 surface->output->current->width,
1879 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001880}
1881
1882static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001883panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1884{
1885 struct desktop_shell *shell = es->private;
1886
1887 configure_static_surface(es, &shell->panel_layer);
1888}
1889
1890static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001891desktop_shell_set_panel(struct wl_client *client,
1892 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001893 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001894 struct wl_resource *surface_resource)
1895{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001896 struct desktop_shell *shell = resource->data;
1897 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001898
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001899 if (surface->configure) {
1900 wl_resource_post_error(surface_resource,
1901 WL_DISPLAY_ERROR_INVALID_OBJECT,
1902 "surface role already assigned");
1903 return;
1904 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001905
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001906 surface->configure = panel_configure;
1907 surface->private = shell;
1908 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001909 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001910 surface_resource,
1911 surface->output->current->width,
1912 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001913}
1914
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001915static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001916lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1917{
1918 struct desktop_shell *shell = surface->private;
1919
1920 center_on_output(surface, get_default_output(shell->compositor));
1921
1922 if (!weston_surface_is_mapped(surface)) {
1923 wl_list_insert(&shell->lock_layer.surface_list,
1924 &surface->layer_link);
1925 weston_surface_assign_output(surface);
1926 weston_compositor_wake(shell->compositor);
1927 }
1928}
1929
1930static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001931handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001932{
Tiago Vignattibe143262012-04-16 17:31:41 +03001933 struct desktop_shell *shell =
1934 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001935
Martin Minarik6d118362012-06-07 18:01:59 +02001936 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001937 shell->lock_surface = NULL;
1938}
1939
1940static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001941desktop_shell_set_lock_surface(struct wl_client *client,
1942 struct wl_resource *resource,
1943 struct wl_resource *surface_resource)
1944{
Tiago Vignattibe143262012-04-16 17:31:41 +03001945 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001946 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001947
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001948 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001949
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001950 if (!shell->locked)
1951 return;
1952
Pekka Paalanen98262232011-12-01 10:42:22 +02001953 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001954
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001955 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1956 wl_signal_add(&surface_resource->destroy_signal,
1957 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001958
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001959 surface->configure = lock_surface_configure;
1960 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001961}
1962
1963static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001964resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001965{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001966 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001967 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001968
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001969 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1970 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001971
1972 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001973
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001974 wl_list_remove(&shell->lock_layer.link);
1975 wl_list_insert(&shell->compositor->cursor_layer.link,
1976 &shell->fullscreen_layer.link);
1977 wl_list_insert(&shell->fullscreen_layer.link,
1978 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001979 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001980
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001981 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02001982
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001983 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001984 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001985 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001986 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001987}
1988
1989static void
1990desktop_shell_unlock(struct wl_client *client,
1991 struct wl_resource *resource)
1992{
Tiago Vignattibe143262012-04-16 17:31:41 +03001993 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001994
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001995 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001996
1997 if (shell->locked)
1998 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001999}
2000
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002001static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002002desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002003 struct wl_resource *resource,
2004 struct wl_resource *surface_resource)
2005{
2006 struct desktop_shell *shell = resource->data;
2007
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002008 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002009}
2010
Kristian Høgsberg75840622011-09-06 13:48:16 -04002011static const struct desktop_shell_interface desktop_shell_implementation = {
2012 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002013 desktop_shell_set_panel,
2014 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002015 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002016 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002017};
2018
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002019static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002020get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002021{
2022 struct shell_surface *shsurf;
2023
2024 shsurf = get_shell_surface(surface);
2025 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002026 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002027 return shsurf->type;
2028}
2029
Kristian Høgsberg75840622011-09-06 13:48:16 -04002030static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002031move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002032{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002033 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002034 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002035 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002036
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002037 if (surface == NULL)
2038 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002039
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002040 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002041 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002042 return;
2043
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002044 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002045}
2046
2047static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002048resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002051 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002052 uint32_t edges = 0;
2053 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002054 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002055
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002056 if (surface == NULL)
2057 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002058
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002059 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002060 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002061 return;
2062
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002063 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002064 wl_fixed_to_int(seat->pointer->grab_x),
2065 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002066 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002067
Pekka Paalanen60921e52012-01-25 15:55:43 +02002068 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002070 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002071 edges |= 0;
2072 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002073 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002074
Pekka Paalanen60921e52012-01-25 15:55:43 +02002075 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002076 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002077 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002078 edges |= 0;
2079 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002080 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002081
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002082 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002083}
2084
2085static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002086surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002087 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002088{
Scott Moreau02709af2012-05-22 01:54:10 -06002089 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002090 struct shell_surface *shsurf;
2091 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002092 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002093
2094 if (surface == NULL)
2095 return;
2096
2097 shsurf = get_shell_surface(surface);
2098 if (!shsurf)
2099 return;
2100
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002101 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002102
Scott Moreau02709af2012-05-22 01:54:10 -06002103 if (surface->alpha > 1.0)
2104 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002105 if (surface->alpha < step)
2106 surface->alpha = step;
2107
2108 surface->geometry.dirty = 1;
2109 weston_surface_damage(surface);
2110}
2111
2112static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002113do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002114 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002115{
Daniel Stone37816df2012-05-16 18:45:18 +01002116 struct weston_seat *ws = (struct weston_seat *) seat;
2117 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002118 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002119 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002120
2121 wl_list_for_each(output, &compositor->output_list, link) {
2122 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002123 wl_fixed_to_double(seat->pointer->x),
2124 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002125 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002126 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002127 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002128 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002129 increment = -output->zoom.increment;
2130 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002131 increment = output->zoom.increment *
2132 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002133 else
2134 increment = 0;
2135
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002136 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002137
Scott Moreaue6603982012-06-11 13:07:51 -06002138 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002139 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002140 else if (output->zoom.level > output->zoom.max_level)
2141 output->zoom.level = output->zoom.max_level;
2142 else
2143 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002144
Scott Moreaue6603982012-06-11 13:07:51 -06002145 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002146
Scott Moreau8dacaab2012-06-17 18:10:58 -06002147 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002148 }
2149 }
2150}
2151
Scott Moreauccbf29d2012-02-22 14:21:41 -07002152static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002153zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002154 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002155{
2156 do_zoom(seat, time, 0, axis, value);
2157}
2158
2159static void
2160zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2161 void *data)
2162{
2163 do_zoom(seat, time, key, 0, 0);
2164}
2165
2166static void
2167terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2168 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002169{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002170 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002171
Daniel Stone325fc2d2012-05-30 16:31:58 +01002172 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002173}
2174
2175static void
Scott Moreau447013d2012-02-18 05:05:29 -07002176rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002177 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002178{
2179 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002180 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002181 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002182 struct shell_surface *shsurf = rotate->base.shsurf;
2183 struct weston_surface *surface;
2184 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2185
2186 if (!shsurf)
2187 return;
2188
2189 surface = shsurf->surface;
2190
2191 cx = 0.5f * surface->geometry.width;
2192 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002193
Daniel Stone37816df2012-05-16 18:45:18 +01002194 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2195 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002196 r = sqrtf(dx * dx + dy * dy);
2197
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002198 wl_list_remove(&shsurf->rotation.transform.link);
2199 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002200
2201 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002202 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002203 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002204
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002205 weston_matrix_init(&rotate->rotation);
2206 rotate->rotation.d[0] = dx / r;
2207 rotate->rotation.d[4] = -dy / r;
2208 rotate->rotation.d[1] = -rotate->rotation.d[4];
2209 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002210
2211 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002212 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002213 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002214 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002215 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002216
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002217 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002218 &shsurf->surface->geometry.transformation_list,
2219 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002220 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002221 wl_list_init(&shsurf->rotation.transform.link);
2222 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002223 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002224 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002225
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002226 /* We need to adjust the position of the surface
2227 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002228 cposx = surface->geometry.x + cx;
2229 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002230 dposx = rotate->center.x - cposx;
2231 dposy = rotate->center.y - cposy;
2232 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002233 weston_surface_set_position(surface,
2234 surface->geometry.x + dposx,
2235 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002236 }
2237
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002238 /* Repaint implies weston_surface_update_transform(), which
2239 * lazily applies the damage due to rotation update.
2240 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002241 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002242}
2243
2244static void
Scott Moreau447013d2012-02-18 05:05:29 -07002245rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002246 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002247{
2248 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002249 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002250 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002251 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002252 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002253
Daniel Stone4dbadb12012-05-30 16:31:51 +01002254 if (pointer->button_count == 0 &&
2255 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002256 if (shsurf)
2257 weston_matrix_multiply(&shsurf->rotation.rotation,
2258 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002259 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002260 free(rotate);
2261 }
2262}
2263
Scott Moreau447013d2012-02-18 05:05:29 -07002264static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002265 noop_grab_focus,
2266 rotate_grab_motion,
2267 rotate_grab_button,
2268};
2269
2270static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002271rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002272 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273{
2274 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002275 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002276 struct shell_surface *surface;
2277 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002278 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002279 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002280
2281 if (base_surface == NULL)
2282 return;
2283
2284 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002285 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002286 return;
2287
Pekka Paalanen460099f2012-01-20 16:48:25 +02002288 rotate = malloc(sizeof *rotate);
2289 if (!rotate)
2290 return;
2291
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002292 weston_surface_to_global_float(surface->surface,
2293 surface->surface->geometry.width / 2,
2294 surface->surface->geometry.height / 2,
2295 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002296
Daniel Stone37816df2012-05-16 18:45:18 +01002297 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2298 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002299 r = sqrtf(dx * dx + dy * dy);
2300 if (r > 20.0f) {
2301 struct weston_matrix inverse;
2302
2303 weston_matrix_init(&inverse);
2304 inverse.d[0] = dx / r;
2305 inverse.d[4] = dy / r;
2306 inverse.d[1] = -inverse.d[4];
2307 inverse.d[5] = inverse.d[0];
2308 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002309
2310 weston_matrix_init(&rotate->rotation);
2311 rotate->rotation.d[0] = dx / r;
2312 rotate->rotation.d[4] = -dy / r;
2313 rotate->rotation.d[1] = -rotate->rotation.d[4];
2314 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002315 } else {
2316 weston_matrix_init(&surface->rotation.rotation);
2317 weston_matrix_init(&rotate->rotation);
2318 }
2319
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002320 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2321 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002322}
2323
2324static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002325lower_fullscreen_layer(struct desktop_shell *shell)
2326{
2327 struct workspace *ws;
2328 struct weston_surface *surface, *prev;
2329
2330 ws = get_current_workspace(shell);
2331 wl_list_for_each_reverse_safe(surface, prev,
2332 &shell->fullscreen_layer.surface_list,
2333 layer_link)
2334 weston_surface_restack(surface, &ws->layer.surface_list);
2335}
2336
2337static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002338activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002339 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002340{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002341 struct workspace *ws = get_current_workspace(shell);
2342 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002343
Daniel Stone37816df2012-05-16 18:45:18 +01002344 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002345
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002346 wl_list_for_each(state, &ws->focus_list, link)
2347 if (state->seat == seat)
2348 break;
2349
2350 if (&state->link == &ws->focus_list) {
2351 state = focus_state_create(seat, ws);
2352 if (state == NULL)
2353 return;
2354 }
2355
2356 state->keyboard_focus = es;
2357 wl_list_remove(&state->surface_destroy_listener.link);
2358 wl_signal_add(&es->surface.resource.destroy_signal,
2359 &state->surface_destroy_listener);
2360
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002361 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002362 case SHELL_SURFACE_FULLSCREEN:
2363 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002364 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002365 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002366 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002367 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002368 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002369 lower_fullscreen_layer(shell);
2370 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002371 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002372 }
2373}
2374
Alex Wu21858432012-04-01 20:13:08 +08002375/* no-op func for checking black surface */
2376static void
2377black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2378{
2379}
2380
2381static bool
2382is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2383{
2384 if (es->configure == black_surface_configure) {
2385 if (fs_surface)
2386 *fs_surface = (struct weston_surface *)es->private;
2387 return true;
2388 }
2389 return false;
2390}
2391
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002393click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002394 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002395{
Daniel Stone37816df2012-05-16 18:45:18 +01002396 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002397 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002398 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002399 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002400
Daniel Stone37816df2012-05-16 18:45:18 +01002401 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002402 if (!focus)
2403 return;
2404
Alex Wu21858432012-04-01 20:13:08 +08002405 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002406 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002407
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002408 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2409 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002410
Daniel Stone325fc2d2012-05-30 16:31:58 +01002411 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002412 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002413}
2414
2415static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002416lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002417{
Tiago Vignattibe143262012-04-16 17:31:41 +03002418 struct desktop_shell *shell =
2419 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002420 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002421 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002422
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002423 if (shell->locked) {
2424 wl_list_for_each(output, &shell->compositor->output_list, link)
2425 /* TODO: find a way to jump to other DPMS levels */
2426 if (output->set_dpms)
2427 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002428 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002429 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002430
2431 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002432
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002433 /* Hide all surfaces by removing the fullscreen, panel and
2434 * toplevel layers. This way nothing else can show or receive
2435 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002436
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002437 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002438 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002439 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002440 wl_list_insert(&shell->compositor->cursor_layer.link,
2441 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002442
Pekka Paalanen77346a62011-11-30 16:26:35 +02002443 launch_screensaver(shell);
2444
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002445 /* TODO: disable bindings that should not work while locked. */
2446
2447 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002448}
2449
2450static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002451unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002452{
Tiago Vignattibe143262012-04-16 17:31:41 +03002453 struct desktop_shell *shell =
2454 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002455
Pekka Paalanend81c2162011-11-16 13:47:34 +02002456 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002457 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002458 return;
2459 }
2460
2461 /* If desktop-shell client has gone away, unlock immediately. */
2462 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002463 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002464 return;
2465 }
2466
2467 if (shell->prepare_event_sent)
2468 return;
2469
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002470 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002471 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002472}
2473
2474static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002475show_input_panels(struct wl_listener *listener, void *data)
2476{
2477 struct desktop_shell *shell =
2478 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002479 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002480
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002481 wl_list_for_each_safe(surface, next,
2482 &shell->input_panel.surfaces, layer_link) {
2483 wl_list_remove(&surface->layer_link);
2484 wl_list_insert(&shell->panel_layer.surface_list,
2485 &surface->layer_link);
2486 weston_surface_assign_output(surface);
2487 weston_surface_damage(surface);
2488 weston_slide_run(surface,
2489 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002490 }
2491}
2492
2493static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002494input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2495
2496static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002497hide_input_panels(struct wl_listener *listener, void *data)
2498{
2499 struct desktop_shell *shell =
2500 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002501 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002502
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002503 wl_list_for_each_safe(surface, next,
2504 &shell->panel_layer.surface_list, layer_link)
2505 if (surface->configure == input_panel_configure) {
2506 weston_surface_unmap(surface);
2507 wl_list_insert(&shell->input_panel.surfaces,
2508 &surface->layer_link);
2509 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002510}
2511
2512static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002513center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002514{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002515 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002516 GLfloat x = (mode->width - surface->buffer->width) / 2;
2517 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002518
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002519 weston_surface_configure(surface, output->x + x, output->y + y,
2520 surface->buffer->width,
2521 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002522}
2523
2524static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002525map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002526 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002529 struct shell_surface *shsurf = get_shell_surface(surface);
2530 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002531 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002532 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002533 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002534 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002535
Pekka Paalanen60921e52012-01-25 15:55:43 +02002536 surface->geometry.width = width;
2537 surface->geometry.height = height;
2538 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002539
2540 /* initial positioning, see also configure() */
2541 switch (surface_type) {
2542 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002543 weston_surface_set_position(surface, 10 + random() % 400,
2544 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002545 break;
Alex Wu4539b082012-03-01 12:57:46 +08002546 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002547 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002548 shell_map_fullscreen(shsurf);
2549 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002550 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002551 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002552 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002553 weston_surface_set_position(surface, shsurf->output->x,
2554 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002555 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002556 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002557 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002558 case SHELL_SURFACE_NONE:
2559 weston_surface_set_position(surface,
2560 surface->geometry.x + sx,
2561 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002562 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002563 default:
2564 ;
2565 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002566
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002567 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002568 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002569 case SHELL_SURFACE_POPUP:
2570 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002571 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002572 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2573 break;
Alex Wu4539b082012-03-01 12:57:46 +08002574 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002575 case SHELL_SURFACE_NONE:
2576 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002577 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002578 ws = get_current_workspace(shell);
2579 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002580 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002581 }
2582
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002583 if (surface_type != SHELL_SURFACE_NONE) {
2584 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002585 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2586 surface->output = shsurf->output;
2587 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002588
Juan Zhao7bb92f02011-12-15 11:31:51 -05002589 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002590 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002591 if (shsurf->transient.flags ==
2592 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2593 break;
2594 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002595 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002596 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002597 if (!shell->locked) {
2598 wl_list_for_each(seat, &compositor->seat_list, link)
2599 activate(shell, surface, seat);
2600 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002601 break;
2602 default:
2603 break;
2604 }
2605
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002606 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002607 {
2608 switch (shell->win_animation_type) {
2609 case ANIMATION_FADE:
2610 weston_fade_run(surface, NULL, NULL);
2611 break;
2612 case ANIMATION_ZOOM:
2613 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2614 break;
2615 default:
2616 break;
2617 }
2618 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002619}
2620
2621static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002622configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002623 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002624{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002625 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2626 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002627
Pekka Paalanen77346a62011-11-30 16:26:35 +02002628 shsurf = get_shell_surface(surface);
2629 if (shsurf)
2630 surface_type = shsurf->type;
2631
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002632 surface->geometry.x = x;
2633 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002634 surface->geometry.width = width;
2635 surface->geometry.height = height;
2636 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002637
2638 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002639 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002640 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002641 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002642 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002643 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002644 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002645 surface->geometry.x = surface->output->x;
2646 surface->geometry.y = surface->output->y +
2647 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002648 break;
Alex Wu4539b082012-03-01 12:57:46 +08002649 case SHELL_SURFACE_TOPLEVEL:
2650 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002651 default:
2652 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002653 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002654
Alex Wu4539b082012-03-01 12:57:46 +08002655 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002656 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002657 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002658
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002659 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002660 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002661 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002662}
2663
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002664static void
2665shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2666{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002667 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002668 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002669 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002670
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002671 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002672 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002673 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002674 type_changed = 1;
2675 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002676
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002677 if (!weston_surface_is_mapped(es)) {
2678 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002679 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002680 es->geometry.width != es->buffer->width ||
2681 es->geometry.height != es->buffer->height) {
2682 GLfloat from_x, from_y;
2683 GLfloat to_x, to_y;
2684
2685 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2686 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2687 configure(shell, es,
2688 es->geometry.x + to_x - from_x,
2689 es->geometry.y + to_y - from_y,
2690 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002691 }
2692}
2693
Tiago Vignattibe143262012-04-16 17:31:41 +03002694static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002695
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002696static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002697desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002698{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002699 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002700 struct desktop_shell *shell =
2701 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002702
2703 shell->child.process.pid = 0;
2704 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002705
2706 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2707 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002708 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002709 shell->child.deathstamp = time;
2710 shell->child.deathcount = 0;
2711 }
2712
2713 shell->child.deathcount++;
2714 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002715 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002716 return;
2717 }
2718
Martin Minarik6d118362012-06-07 18:01:59 +02002719 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002720 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002721}
2722
2723static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002724launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002725{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002726 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002727
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002728 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002729 &shell->child.process,
2730 shell_exe,
2731 desktop_shell_sigchld);
2732
2733 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002734 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002735 return 0;
2736}
2737
2738static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002739bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2740{
Tiago Vignattibe143262012-04-16 17:31:41 +03002741 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002742
2743 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002744 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002745}
2746
Kristian Høgsberg75840622011-09-06 13:48:16 -04002747static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002748unbind_desktop_shell(struct wl_resource *resource)
2749{
Tiago Vignattibe143262012-04-16 17:31:41 +03002750 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002751
2752 if (shell->locked)
2753 resume_desktop(shell);
2754
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002755 shell->child.desktop_shell = NULL;
2756 shell->prepare_event_sent = false;
2757 free(resource);
2758}
2759
2760static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002761bind_desktop_shell(struct wl_client *client,
2762 void *data, uint32_t version, uint32_t id)
2763{
Tiago Vignattibe143262012-04-16 17:31:41 +03002764 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002765 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002766
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002767 resource = wl_client_add_object(client, &desktop_shell_interface,
2768 &desktop_shell_implementation,
2769 id, shell);
2770
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002771 if (client == shell->child.client) {
2772 resource->destroy = unbind_desktop_shell;
2773 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002774 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002775 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002776
2777 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2778 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002779 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002780}
2781
Pekka Paalanen6e168112011-11-24 11:34:05 +02002782static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002783screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2784{
2785 struct desktop_shell *shell = surface->private;
2786
2787 if (!shell->locked)
2788 return;
2789
2790 center_on_output(surface, surface->output);
2791
2792 if (wl_list_empty(&surface->layer_link)) {
2793 wl_list_insert(shell->lock_layer.surface_list.prev,
2794 &surface->layer_link);
2795 weston_surface_assign_output(surface);
2796 shell->compositor->idle_time = shell->screensaver.duration;
2797 weston_compositor_wake(shell->compositor);
2798 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2799 }
2800}
2801
2802static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002803screensaver_set_surface(struct wl_client *client,
2804 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002805 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002806 struct wl_resource *output_resource)
2807{
Tiago Vignattibe143262012-04-16 17:31:41 +03002808 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002809 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002810 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002811
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002812 surface->configure = screensaver_configure;
2813 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002814 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002815}
2816
2817static const struct screensaver_interface screensaver_implementation = {
2818 screensaver_set_surface
2819};
2820
2821static void
2822unbind_screensaver(struct wl_resource *resource)
2823{
Tiago Vignattibe143262012-04-16 17:31:41 +03002824 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002825
Pekka Paalanen77346a62011-11-30 16:26:35 +02002826 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002827 free(resource);
2828}
2829
2830static void
2831bind_screensaver(struct wl_client *client,
2832 void *data, uint32_t version, uint32_t id)
2833{
Tiago Vignattibe143262012-04-16 17:31:41 +03002834 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002835 struct wl_resource *resource;
2836
2837 resource = wl_client_add_object(client, &screensaver_interface,
2838 &screensaver_implementation,
2839 id, shell);
2840
Pekka Paalanen77346a62011-11-30 16:26:35 +02002841 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002842 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002843 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002844 return;
2845 }
2846
2847 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2848 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002849 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002850}
2851
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002852static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002853input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2854{
2855 struct weston_mode *mode = surface->output->current;
2856 GLfloat x = (mode->width - surface->buffer->width) / 2;
2857 GLfloat y = mode->height - surface->buffer->height;
2858
2859 /* Don't map the input panel here, wait for
2860 * show_input_panels signal. */
2861
2862 weston_surface_configure(surface,
2863 surface->output->x + x,
2864 surface->output->y + y,
2865 surface->buffer->width,
2866 surface->buffer->height);
2867}
2868
2869static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002870input_panel_set_surface(struct wl_client *client,
2871 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002872 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002873 struct wl_resource *output_resource)
2874{
2875 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002876 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002877 struct weston_output *output = output_resource->data;
2878
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002879 surface->configure = input_panel_configure;
2880 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002881 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002882 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002883}
2884
2885static const struct input_panel_interface input_panel_implementation = {
2886 input_panel_set_surface
2887};
2888
2889static void
2890unbind_input_panel(struct wl_resource *resource)
2891{
2892 struct desktop_shell *shell = resource->data;
2893
2894 shell->input_panel.binding = NULL;
2895 free(resource);
2896}
2897
2898static void
2899bind_input_panel(struct wl_client *client,
2900 void *data, uint32_t version, uint32_t id)
2901{
2902 struct desktop_shell *shell = data;
2903 struct wl_resource *resource;
2904
2905 resource = wl_client_add_object(client, &input_panel_interface,
2906 &input_panel_implementation,
2907 id, shell);
2908
2909 if (shell->input_panel.binding == NULL) {
2910 resource->destroy = unbind_input_panel;
2911 shell->input_panel.binding = resource;
2912 return;
2913 }
2914
2915 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2916 "interface object already bound");
2917 wl_resource_destroy(resource);
2918}
2919
Kristian Høgsberg07045392012-02-19 18:52:44 -05002920struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002921 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002922 struct weston_surface *current;
2923 struct wl_listener listener;
2924 struct wl_keyboard_grab grab;
2925};
2926
2927static void
2928switcher_next(struct switcher *switcher)
2929{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002930 struct weston_surface *surface;
2931 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002932 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002933 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002934
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002935 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002936 switch (get_shell_surface_type(surface)) {
2937 case SHELL_SURFACE_TOPLEVEL:
2938 case SHELL_SURFACE_FULLSCREEN:
2939 case SHELL_SURFACE_MAXIMIZED:
2940 if (first == NULL)
2941 first = surface;
2942 if (prev == switcher->current)
2943 next = surface;
2944 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002945 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002946 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002947 weston_surface_damage(surface);
2948 break;
2949 default:
2950 break;
2951 }
Alex Wu1659daa2012-04-01 20:13:09 +08002952
2953 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002954 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002955 surface->geometry.dirty = 1;
2956 weston_surface_damage(surface);
2957 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002958 }
2959
2960 if (next == NULL)
2961 next = first;
2962
Alex Wu07b26062012-03-12 16:06:01 +08002963 if (next == NULL)
2964 return;
2965
Kristian Høgsberg07045392012-02-19 18:52:44 -05002966 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002967 wl_signal_add(&next->surface.resource.destroy_signal,
2968 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002969
2970 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002971 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002972
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002973 shsurf = get_shell_surface(switcher->current);
2974 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002975 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002976}
2977
2978static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002979switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002980{
2981 struct switcher *switcher =
2982 container_of(listener, struct switcher, listener);
2983
2984 switcher_next(switcher);
2985}
2986
2987static void
Daniel Stone351eb612012-05-31 15:27:47 -04002988switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002989{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002990 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002991 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002992 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002993
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002994 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002995 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002996 weston_surface_damage(surface);
2997 }
2998
Alex Wu07b26062012-03-12 16:06:01 +08002999 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003000 activate(switcher->shell, switcher->current,
3001 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003002 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003003 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003004 free(switcher);
3005}
3006
3007static void
3008switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003009 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003010{
3011 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003012 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003013
Daniel Stonec9785ea2012-05-30 16:31:52 +01003014 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003015 switcher_next(switcher);
3016}
3017
3018static void
3019switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3020 uint32_t mods_depressed, uint32_t mods_latched,
3021 uint32_t mods_locked, uint32_t group)
3022{
3023 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003024 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003025
Daniel Stone351eb612012-05-31 15:27:47 -04003026 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3027 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003028}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003029
3030static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003031 switcher_key,
3032 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033};
3034
3035static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003036switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3037 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003038{
Tiago Vignattibe143262012-04-16 17:31:41 +03003039 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003040 struct switcher *switcher;
3041
3042 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003043 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003045 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003046 wl_list_init(&switcher->listener.link);
3047
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003048 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003049 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003050 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3051 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003052 switcher_next(switcher);
3053}
3054
Pekka Paalanen3c647232011-12-22 13:43:43 +02003055static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003056backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3057 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003058{
3059 struct weston_compositor *compositor = data;
3060 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003061 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003062
3063 /* TODO: we're limiting to simple use cases, where we assume just
3064 * control on the primary display. We'd have to extend later if we
3065 * ever get support for setting backlights on random desktop LCD
3066 * panels though */
3067 output = get_default_output(compositor);
3068 if (!output)
3069 return;
3070
3071 if (!output->set_backlight)
3072 return;
3073
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003074 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3075 backlight_new = output->backlight_current - 25;
3076 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3077 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003078
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003079 if (backlight_new < 5)
3080 backlight_new = 5;
3081 if (backlight_new > 255)
3082 backlight_new = 255;
3083
3084 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003085 output->set_backlight(output, output->backlight_current);
3086}
3087
3088static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003089debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3090 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003091{
Tiago Vignattibe143262012-04-16 17:31:41 +03003092 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003093 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003094 struct weston_surface *surface;
3095
3096 if (shell->debug_repaint_surface) {
3097 weston_surface_destroy(shell->debug_repaint_surface);
3098 shell->debug_repaint_surface = NULL;
3099 } else {
3100 surface = weston_surface_create(compositor);
3101 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3102 weston_surface_configure(surface, 0, 0, 8192, 8192);
3103 wl_list_insert(&compositor->fade_layer.surface_list,
3104 &surface->layer_link);
3105 weston_surface_assign_output(surface);
3106 pixman_region32_init(&surface->input);
3107
3108 /* Here's the dirty little trick that makes the
3109 * repaint debugging work: we force an
3110 * update_transform first to update dependent state
3111 * and clear the geometry.dirty bit. Then we clear
3112 * the surface damage so it only gets repainted
3113 * piecewise as we repaint other things. */
3114
3115 weston_surface_update_transform(surface);
3116 pixman_region32_fini(&surface->damage);
3117 pixman_region32_init(&surface->damage);
3118 shell->debug_repaint_surface = surface;
3119 }
3120}
3121
3122static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003123force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3124 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003125{
3126 struct wl_client *client;
3127 pid_t pid;
3128 uid_t uid;
3129 gid_t gid;
3130
Daniel Stone325fc2d2012-05-30 16:31:58 +01003131 client = seat->keyboard->focus->resource.client;
3132 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003133
Daniel Stone325fc2d2012-05-30 16:31:58 +01003134 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003135}
3136
3137static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003138workspace_up_binding(struct wl_seat *seat, uint32_t time,
3139 uint32_t key, void *data)
3140{
3141 struct desktop_shell *shell = data;
3142 unsigned int new_index = shell->workspaces.current;
3143
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003144 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003145 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003146 if (new_index != 0)
3147 new_index--;
3148
3149 change_workspace(shell, new_index);
3150}
3151
3152static void
3153workspace_down_binding(struct wl_seat *seat, uint32_t time,
3154 uint32_t key, void *data)
3155{
3156 struct desktop_shell *shell = data;
3157 unsigned int new_index = shell->workspaces.current;
3158
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003159 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003160 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003161 if (new_index < shell->workspaces.num - 1)
3162 new_index++;
3163
3164 change_workspace(shell, new_index);
3165}
3166
3167static void
3168workspace_f_binding(struct wl_seat *seat, uint32_t time,
3169 uint32_t key, void *data)
3170{
3171 struct desktop_shell *shell = data;
3172 unsigned int new_index;
3173
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003174 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003175 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003176 new_index = key - KEY_F1;
3177 if (new_index >= shell->workspaces.num)
3178 new_index = shell->workspaces.num - 1;
3179
3180 change_workspace(shell, new_index);
3181}
3182
3183
3184static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003185shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003186{
Tiago Vignattibe143262012-04-16 17:31:41 +03003187 struct desktop_shell *shell =
3188 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003189 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003190
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003191 if (shell->child.client)
3192 wl_client_destroy(shell->child.client);
3193
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003194 wl_list_remove(&shell->lock_listener.link);
3195 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003196 wl_list_remove(&shell->show_input_panel_listener.link);
3197 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003198
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003199 wl_array_for_each(ws, &shell->workspaces.array)
3200 workspace_destroy(*ws);
3201 wl_array_release(&shell->workspaces.array);
3202
Pekka Paalanen3c647232011-12-22 13:43:43 +02003203 free(shell->screensaver.path);
3204 free(shell);
3205}
3206
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003207static void
3208shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3209{
3210 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003211 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003212
3213 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003214 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3215 MODIFIER_CTRL | MODIFIER_ALT,
3216 terminate_binding, ec);
3217 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3218 click_to_activate_binding,
3219 shell);
3220 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3221 MODIFIER_SUPER | MODIFIER_ALT,
3222 surface_opacity_binding, NULL);
3223 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3224 MODIFIER_SUPER, zoom_axis_binding,
3225 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003226
3227 /* configurable bindings */
3228 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003229 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3230 zoom_key_binding, NULL);
3231 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3232 zoom_key_binding, NULL);
3233 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3234 shell);
3235 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3236 resize_binding, shell);
3237 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3238 rotate_binding, NULL);
3239 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3240 shell);
3241 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3242 ec);
3243 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3244 backlight_binding, ec);
3245 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3246 ec);
3247 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3248 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003249 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003250 debug_repaint_binding, shell);
3251 weston_compositor_add_key_binding(ec, KEY_K, mod,
3252 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003253 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3254 workspace_up_binding, shell);
3255 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3256 workspace_down_binding, shell);
3257
3258 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3259 if (shell->workspaces.num > 1) {
3260 num_workspace_bindings = shell->workspaces.num;
3261 if (num_workspace_bindings > 6)
3262 num_workspace_bindings = 6;
3263 for (i = 0; i < num_workspace_bindings; i++)
3264 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3265 workspace_f_binding,
3266 shell);
3267 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003268}
3269
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003270int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003271shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003272
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003273WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003274shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003275{
Tiago Vignattibe143262012-04-16 17:31:41 +03003276 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003277 struct workspace **pws;
3278 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003279
3280 shell = malloc(sizeof *shell);
3281 if (shell == NULL)
3282 return -1;
3283
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003284 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003285 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003286
3287 shell->destroy_listener.notify = shell_destroy;
3288 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3289 shell->lock_listener.notify = lock;
3290 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3291 shell->unlock_listener.notify = unlock;
3292 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003293 shell->show_input_panel_listener.notify = show_input_panels;
3294 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3295 shell->hide_input_panel_listener.notify = hide_input_panels;
3296 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003297 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003298 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003299 ec->shell_interface.create_shell_surface = create_shell_surface;
3300 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003301 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003302 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003303 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003304
Pekka Paalanen77346a62011-11-30 16:26:35 +02003305 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003306 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003307
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003308 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3309 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003310 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3311 weston_layer_init(&shell->lock_layer, NULL);
3312
3313 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003314
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003315 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003316
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003317 for (i = 0; i < shell->workspaces.num; i++) {
3318 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3319 if (pws == NULL)
3320 return -1;
3321
3322 *pws = workspace_create();
3323 if (*pws == NULL)
3324 return -1;
3325 }
3326 activate_workspace(shell, 0);
3327
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003328 wl_list_init(&shell->workspaces.animation.link);
3329 shell->workspaces.animation.frame = animate_workspace_change_frame;
3330
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003331 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3332 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003333 return -1;
3334
Kristian Høgsberg75840622011-09-06 13:48:16 -04003335 if (wl_display_add_global(ec->wl_display,
3336 &desktop_shell_interface,
3337 shell, bind_desktop_shell) == NULL)
3338 return -1;
3339
Pekka Paalanen6e168112011-11-24 11:34:05 +02003340 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3341 shell, bind_screensaver) == NULL)
3342 return -1;
3343
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003344 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3345 shell, bind_input_panel) == NULL)
3346 return -1;
3347
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003348 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003349 if (launch_desktop_shell_process(shell) != 0)
3350 return -1;
3351
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003352 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003353 if (ec->seat->seat.pointer)
3354 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3355 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003356
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003357 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003358
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003359 return 0;
3360}