blob: 40cef14f70fc1008602de0dd4f4c6165c8f4ea95 [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;
53 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Tiago Vignattibe143262012-04-16 17:31:41 +030066struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040068
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020072 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050077 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
79
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040080 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030081 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040082
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020085 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020086 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020087
88 unsigned deathcount;
89 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020091
92 bool locked;
93 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020094
Kristian Høgsberg730c94d2012-06-26 21:44:35 -040095 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050096 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Pekka Paalanen77346a62011-11-30 16:26:35 +020098 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020099 struct wl_array array;
100 unsigned int current;
101 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200102
103 struct weston_animation animation;
104 int anim_dir;
105 uint32_t anim_timestamp;
106 double anim_current;
107 struct workspace *anim_from;
108 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200109 } workspaces;
110
111 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200112 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200113 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200114 struct wl_resource *binding;
115 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500118
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200119 struct {
120 struct wl_resource *binding;
121 struct wl_list surfaces;
122 } input_panel;
123
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300124 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800125 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400127};
128
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500129enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200130 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 SHELL_SURFACE_TOPLEVEL,
132 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800134 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200136};
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138struct ping_timer {
139 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600140 uint32_t serial;
141};
142
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200143struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200144 struct wl_resource resource;
145
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200147 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400148 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300149 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200150
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400151 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400152 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500153 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800154 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800155 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600156 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100157
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200159 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500160 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200161 } rotation;
162
163 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700164 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500165 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200166 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100168 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400169 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 } popup;
171
Alex Wu4539b082012-03-01 12:57:46 +0800172 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300173 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400174 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300175 } transient;
176
177 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800178 enum wl_shell_surface_fullscreen_method type;
179 struct weston_transform transform; /* matrix from x, y */
180 uint32_t framerate;
181 struct weston_surface *black_surface;
182 } fullscreen;
183
Scott Moreauff1db4a2012-04-17 19:06:18 -0600184 struct ping_timer *ping_timer;
185
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200186 struct weston_transform workspace_transform;
187
Jonas Ådahl04769742012-06-13 00:01:24 +0200188 struct focus_state *focus_state;
189
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500190 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100192 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400193
194 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700198 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199 struct shell_surface *shsurf;
200 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300201 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300202};
203
204struct weston_move_grab {
205 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100206 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207};
208
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500211 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400213 GLfloat x;
214 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } center;
216};
217
Alex Wubd3354b2012-04-17 17:20:49 +0800218static struct shell_surface *
219get_shell_surface(struct weston_surface *surface);
220
221static struct desktop_shell *
222shell_surface_get_shell(struct shell_surface *shsurf);
223
224static bool
225shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
226{
227 struct desktop_shell *shell;
228 struct weston_surface *top_fs_es;
229
230 shell = shell_surface_get_shell(shsurf);
231
232 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
233 return false;
234
235 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
236 struct weston_surface,
237 layer_link);
238 return (shsurf == get_shell_surface(top_fs_es));
239}
240
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500241static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400242destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300243{
244 struct shell_grab *grab;
245
246 grab = container_of(listener, struct shell_grab,
247 shsurf_destroy_listener);
248
249 grab->shsurf = NULL;
250}
251
252static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300253shell_grab_start(struct shell_grab *grab,
254 const struct wl_pointer_grab_interface *interface,
255 struct shell_surface *shsurf,
256 struct wl_pointer *pointer,
257 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300259 struct desktop_shell *shell = shsurf->shell;
260
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261 grab->grab.interface = interface;
262 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400263 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
264 wl_signal_add(&shsurf->resource.destroy_signal,
265 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267 grab->pointer = pointer;
268 grab->grab.focus = &shsurf->surface->surface;
269
270 wl_pointer_start_grab(pointer, &grab->grab);
271 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
272 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
273 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274}
275
276static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300277shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400279 if (grab->shsurf)
280 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300281
282 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283}
284
285static void
Alex Wu4539b082012-03-01 12:57:46 +0800286center_on_output(struct weston_surface *surface,
287 struct weston_output *output);
288
Daniel Stone496ca172012-05-30 16:31:42 +0100289static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300290get_modifier(char *modifier)
291{
292 if (!modifier)
293 return MODIFIER_SUPER;
294
295 if (!strcmp("ctrl", modifier))
296 return MODIFIER_CTRL;
297 else if (!strcmp("alt", modifier))
298 return MODIFIER_ALT;
299 else if (!strcmp("super", modifier))
300 return MODIFIER_SUPER;
301 else
302 return MODIFIER_SUPER;
303}
304
Juan Zhaoe10d2792012-04-25 19:09:52 +0800305static enum animation_type
306get_animation_type(char *animation)
307{
308 if (!animation)
309 return ANIMATION_NONE;
310
311 if (!strcmp("zoom", animation))
312 return ANIMATION_ZOOM;
313 else if (!strcmp("fade", animation))
314 return ANIMATION_FADE;
315 else
316 return ANIMATION_NONE;
317}
318
Alex Wu4539b082012-03-01 12:57:46 +0800319static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300320shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200321{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200322 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200323 char *path = NULL;
324 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200325 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300326 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800327 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200328
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400329 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300330 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800331 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200332 { "num-workspaces",
333 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334 };
335
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400336 struct config_key saver_keys[] = {
337 { "path", CONFIG_KEY_STRING, &path },
338 { "duration", CONFIG_KEY_INTEGER, &duration },
339 };
340
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200343 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
344 };
345
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200346 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500347 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200348 free(config_file);
349
Pekka Paalanen7296e792011-12-07 16:22:00 +0200350 shell->screensaver.path = path;
351 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300352 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800353 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200354 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
355}
356
357static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200358focus_state_destroy(struct focus_state *state)
359{
360 wl_list_remove(&state->seat_destroy_listener.link);
361 wl_list_remove(&state->surface_destroy_listener.link);
362 free(state);
363}
364
365static void
366focus_state_seat_destroy(struct wl_listener *listener, void *data)
367{
368 struct focus_state *state = container_of(listener,
369 struct focus_state,
370 seat_destroy_listener);
371
372 wl_list_remove(&state->link);
373 focus_state_destroy(state);
374}
375
376static void
377focus_state_surface_destroy(struct wl_listener *listener, void *data)
378{
379 struct focus_state *state = container_of(listener,
380 struct focus_state,
381 seat_destroy_listener);
382
383 wl_list_remove(&state->link);
384 focus_state_destroy(state);
385}
386
387static struct focus_state *
388focus_state_create(struct weston_seat *seat)
389{
390 struct wl_keyboard *keyboard = seat->seat.keyboard;
391 struct focus_state *state;
392 struct wl_surface *surface;
393 struct shell_surface *shsurf;
394
395 state = malloc(sizeof *state);
396 if (state == NULL)
397 return NULL;
398
399 surface = keyboard->focus;
400 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
401 shsurf->focus_state = state;
402
403 state->seat = seat;
404 state->keyboard_focus = shsurf->surface;
405 wl_list_init(&state->link);
406
407 state->seat_destroy_listener.notify = focus_state_seat_destroy;
408 state->surface_destroy_listener.notify = focus_state_surface_destroy;
409 wl_signal_add(&seat->seat.destroy_signal,
410 &state->seat_destroy_listener);
411 wl_signal_add(&surface->resource.destroy_signal,
412 &state->surface_destroy_listener);
413
414 return state;
415}
416
417static void
418pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
419{
420 struct focus_state *state, *next;
421
422 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
423 if (state->keyboard_focus)
424 wl_keyboard_set_focus(state->seat->seat.keyboard,
425 &state->keyboard_focus->surface);
426
427 focus_state_destroy(state);
428 }
429 wl_list_init(&ws->focus_list);
430}
431
432static void
433push_focus_state(struct desktop_shell *shell, struct workspace *ws)
434{
435 struct weston_seat *seat;
436 struct focus_state *state;
437 struct wl_keyboard *keyboard;
438
439 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
440 keyboard = seat->seat.keyboard;
441 if (keyboard && keyboard->focus) {
442 state = focus_state_create(seat);
443 if (state == NULL)
444 return;
445
446 wl_list_insert(&ws->focus_list, &state->link);
447
448 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
449 }
450 }
451}
452
453static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200454workspace_destroy(struct workspace *ws)
455{
Jonas Ådahl04769742012-06-13 00:01:24 +0200456 struct focus_state *state, *next;
457
458 wl_list_for_each_safe(state, next, &ws->focus_list, link)
459 focus_state_destroy(state);
460
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200461 free(ws);
462}
463
Jonas Ådahl04769742012-06-13 00:01:24 +0200464static void
465seat_destroyed(struct wl_listener *listener, void *data)
466{
467 struct weston_seat *seat = data;
468 struct focus_state *state, *next;
469 struct workspace *ws = container_of(listener,
470 struct workspace,
471 seat_destroyed_listener);
472
473 wl_list_for_each_safe(state, next, &ws->focus_list, link)
474 if (state->seat == seat)
475 wl_list_remove(&state->link);
476}
477
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200478static struct workspace *
479workspace_create(void)
480{
481 struct workspace *ws = malloc(sizeof *ws);
482 if (ws == NULL)
483 return NULL;
484
485 weston_layer_init(&ws->layer, NULL);
486
Jonas Ådahl04769742012-06-13 00:01:24 +0200487 wl_list_init(&ws->focus_list);
488 wl_list_init(&ws->seat_destroyed_listener.link);
489 ws->seat_destroyed_listener.notify = seat_destroyed;
490
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200491 return ws;
492}
493
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200494static int
495workspace_is_empty(struct workspace *ws)
496{
497 return wl_list_empty(&ws->layer.surface_list);
498}
499
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200500static struct workspace *
501get_workspace(struct desktop_shell *shell, unsigned int index)
502{
503 struct workspace **pws = shell->workspaces.array.data;
504 pws += index;
505 return *pws;
506}
507
508static struct workspace *
509get_current_workspace(struct desktop_shell *shell)
510{
511 return get_workspace(shell, shell->workspaces.current);
512}
513
514static void
515activate_workspace(struct desktop_shell *shell, unsigned int index)
516{
517 struct workspace *ws;
518
519 ws = get_workspace(shell, index);
520 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
521
522 shell->workspaces.current = index;
523}
524
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200525static unsigned int
526get_output_height(struct weston_output *output)
527{
528 return abs(output->region.extents.y1 - output->region.extents.y2);
529}
530
531static void
532surface_translate(struct weston_surface *surface, double d)
533{
534 struct shell_surface *shsurf = get_shell_surface(surface);
535 struct weston_transform *transform;
536
537 transform = &shsurf->workspace_transform;
538 if (wl_list_empty(&transform->link))
539 wl_list_insert(surface->geometry.transformation_list.prev,
540 &shsurf->workspace_transform.link);
541
542 weston_matrix_init(&shsurf->workspace_transform.matrix);
543 weston_matrix_translate(&shsurf->workspace_transform.matrix,
544 0.0, d, 0.0);
545 surface->geometry.dirty = 1;
546}
547
548static void
549workspace_translate_out(struct workspace *ws, double fraction)
550{
551 struct weston_surface *surface;
552 unsigned int height;
553 double d;
554
555 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
556 height = get_output_height(surface->output);
557 d = height * fraction;
558
559 surface_translate(surface, d);
560 }
561}
562
563static void
564workspace_translate_in(struct workspace *ws, double fraction)
565{
566 struct weston_surface *surface;
567 unsigned int height;
568 double d;
569
570 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
571 height = get_output_height(surface->output);
572
573 if (fraction > 0)
574 d = -(height - height * fraction);
575 else
576 d = height + height * fraction;
577
578 surface_translate(surface, d);
579 }
580}
581
582static void
583workspace_damage_all_surfaces(struct workspace *ws)
584{
585 struct weston_surface *surface;
586
587 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
588 weston_surface_damage(surface);
589}
590
591static void
592reverse_workspace_change_animation(struct desktop_shell *shell,
593 unsigned int index,
594 struct workspace *from,
595 struct workspace *to)
596{
597 shell->workspaces.current = index;
598
599 shell->workspaces.anim_to = to;
600 shell->workspaces.anim_from = from;
601 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
602 shell->workspaces.anim_timestamp = 0;
603
Jonas Ådahl04769742012-06-13 00:01:24 +0200604 push_focus_state(shell, from);
605 pop_focus_state(shell, to);
606
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200607 workspace_damage_all_surfaces(from);
608 workspace_damage_all_surfaces(to);
609}
610
611static void
612workspace_deactivate_transforms(struct workspace *ws)
613{
614 struct weston_surface *surface;
615 struct shell_surface *shsurf;
616
617 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
618 shsurf = get_shell_surface(surface);
619 wl_list_remove(&shsurf->workspace_transform.link);
620 wl_list_init(&shsurf->workspace_transform.link);
621 shsurf->surface->geometry.dirty = 1;
622 }
623}
624
625static void
626finish_workspace_change_animation(struct desktop_shell *shell,
627 struct workspace *from,
628 struct workspace *to)
629{
630 workspace_damage_all_surfaces(from);
631 workspace_damage_all_surfaces(to);
632
633 wl_list_remove(&shell->workspaces.animation.link);
634 workspace_deactivate_transforms(from);
635 workspace_deactivate_transforms(to);
636 shell->workspaces.anim_to = NULL;
637
638 wl_list_remove(&shell->workspaces.anim_from->layer.link);
639}
640
641static void
642animate_workspace_change_frame(struct weston_animation *animation,
643 struct weston_output *output, uint32_t msecs)
644{
645 struct desktop_shell *shell =
646 container_of(animation, struct desktop_shell,
647 workspaces.animation);
648 struct workspace *from = shell->workspaces.anim_from;
649 struct workspace *to = shell->workspaces.anim_to;
650 uint32_t t;
651 double x, y;
652
653 if (workspace_is_empty(from) && workspace_is_empty(to)) {
654 finish_workspace_change_animation(shell, from, to);
655 return;
656 }
657
658 if (shell->workspaces.anim_timestamp == 0) {
659 if (shell->workspaces.anim_current == 0.0)
660 shell->workspaces.anim_timestamp = msecs;
661 else
662 shell->workspaces.anim_timestamp =
663 msecs -
664 /* Invers of movement function 'y' below. */
665 (asin(1.0 - shell->workspaces.anim_current) *
666 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
667 M_2_PI);
668 }
669
670 t = msecs - shell->workspaces.anim_timestamp;
671
672 /*
673 * x = [0, π/2]
674 * y(x) = sin(x)
675 */
676 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
677 y = sin(x);
678
679 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
680 workspace_damage_all_surfaces(from);
681 workspace_damage_all_surfaces(to);
682
683 workspace_translate_out(from, shell->workspaces.anim_dir * y);
684 workspace_translate_in(to, shell->workspaces.anim_dir * y);
685 shell->workspaces.anim_current = y;
686
687 workspace_damage_all_surfaces(from);
688 workspace_damage_all_surfaces(to);
689 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200690 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200691 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200692}
693
694static void
695animate_workspace_change(struct desktop_shell *shell,
696 unsigned int index,
697 struct workspace *from,
698 struct workspace *to)
699{
700 struct weston_output *output;
701
702 int dir;
703
704 if (index > shell->workspaces.current)
705 dir = -1;
706 else
707 dir = 1;
708
709 shell->workspaces.current = index;
710
711 shell->workspaces.anim_dir = dir;
712 shell->workspaces.anim_from = from;
713 shell->workspaces.anim_to = to;
714 shell->workspaces.anim_current = 0.0;
715 shell->workspaces.anim_timestamp = 0;
716
717 output = container_of(shell->compositor->output_list.next,
718 struct weston_output, link);
719 wl_list_insert(&output->animation_list,
720 &shell->workspaces.animation.link);
721
722 wl_list_insert(&from->layer.link, &to->layer.link);
723
724 workspace_translate_in(to, 0);
725
Jonas Ådahl04769742012-06-13 00:01:24 +0200726 push_focus_state(shell, from);
727 pop_focus_state(shell, to);
728
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200729 workspace_damage_all_surfaces(from);
730 workspace_damage_all_surfaces(to);
731}
732
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200733static void
734change_workspace(struct desktop_shell *shell, unsigned int index)
735{
736 struct workspace *from;
737 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200738
739 if (index == shell->workspaces.current)
740 return;
741
742 /* Don't change workspace when there is any fullscreen surfaces. */
743 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
744 return;
745
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200746 from = get_current_workspace(shell);
747 to = get_workspace(shell, index);
748
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749 if (shell->workspaces.anim_from == to &&
750 shell->workspaces.anim_to == from) {
751 reverse_workspace_change_animation(shell, index, from, to);
752 return;
753 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200754
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200755 if (shell->workspaces.anim_to != NULL)
756 finish_workspace_change_animation(shell,
757 shell->workspaces.anim_from,
758 shell->workspaces.anim_to);
759
760 if (workspace_is_empty(to) && workspace_is_empty(from)) {
761 shell->workspaces.current = index;
762 wl_list_insert(&from->layer.link, &to->layer.link);
763 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200764
765 push_focus_state(shell, from);
766 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200767 }
768 else
769 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200770}
771
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200772static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400773noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100774 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500775{
776 grab->focus = NULL;
777}
778
779static void
Scott Moreau447013d2012-02-18 05:05:29 -0700780move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100781 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500783 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100784 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300785 struct shell_surface *shsurf = move->base.shsurf;
786 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100787 int dx = wl_fixed_to_int(pointer->x + move->dx);
788 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300789
790 if (!shsurf)
791 return;
792
793 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500794
Daniel Stone103db7f2012-05-08 17:17:55 +0100795 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200796 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400797
798 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500799}
800
801static void
Scott Moreau447013d2012-02-18 05:05:29 -0700802move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100803 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500804{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300805 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
806 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100807 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100808 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500809
Daniel Stone4dbadb12012-05-30 16:31:51 +0100810 if (pointer->button_count == 0 &&
811 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300812 shell_grab_end(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100813 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500814 free(grab);
815 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500816}
817
Scott Moreau447013d2012-02-18 05:05:29 -0700818static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500819 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500820 move_grab_motion,
821 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500822};
823
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600824static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825busy_cursor_grab_focus(struct wl_pointer_grab *base,
826 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600827{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828 struct shell_grab *grab = (struct shell_grab *) base;
829 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600830
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400831 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300832 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400833 wl_pointer_end_grab(pointer);
834 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600835 }
836}
837
838static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400839busy_cursor_grab_motion(struct wl_pointer_grab *grab,
840 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600841{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400842}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600843
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400844static void
845busy_cursor_grab_button(struct wl_pointer_grab *grab,
846 uint32_t time, uint32_t button, uint32_t state)
847{
848}
849
850static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
851 busy_cursor_grab_focus,
852 busy_cursor_grab_motion,
853 busy_cursor_grab_button,
854};
855
856static void
857set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
858{
859 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400860
861 grab = malloc(sizeof *grab);
862 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600863 return;
864
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300865 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
866 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400867}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600868
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400869static void
870end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
871{
872 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
873
874 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300875 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400876 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600877 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600878}
879
Scott Moreau9521d5e2012-04-19 13:06:17 -0600880static void
881ping_timer_destroy(struct shell_surface *shsurf)
882{
883 if (!shsurf || !shsurf->ping_timer)
884 return;
885
886 if (shsurf->ping_timer->source)
887 wl_event_source_remove(shsurf->ping_timer->source);
888
889 free(shsurf->ping_timer);
890 shsurf->ping_timer = NULL;
891}
892
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400893static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600894ping_timeout_handler(void *data)
895{
896 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400897 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600898
Scott Moreau9521d5e2012-04-19 13:06:17 -0600899 /* Client is not responding */
900 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400901
902 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
903 if (seat->seat.pointer->focus == &shsurf->surface->surface)
904 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600905
906 return 1;
907}
908
909static void
910ping_handler(struct weston_surface *surface, uint32_t serial)
911{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400912 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400914 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600915
916 if (!shsurf)
917 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400918 if (!shsurf->resource.client)
919 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600920
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +0300921 if (shsurf->surface == shsurf->shell->grab_surface)
922 return;
923
Scott Moreauff1db4a2012-04-17 19:06:18 -0600924 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300925 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600926 if (!shsurf->ping_timer)
927 return;
928
929 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600930 loop = wl_display_get_event_loop(surface->compositor->wl_display);
931 shsurf->ping_timer->source =
932 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
933 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
934
935 wl_shell_surface_send_ping(&shsurf->resource, serial);
936 }
937}
938
939static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400940handle_pointer_focus(struct wl_listener *listener, void *data)
941{
942 struct wl_pointer *pointer = data;
943 struct weston_surface *surface =
944 (struct weston_surface *) pointer->focus;
945 struct weston_compositor *compositor;
946 struct shell_surface *shsurf;
947 uint32_t serial;
948
949 if (!surface)
950 return;
951
952 compositor = surface->compositor;
953 shsurf = get_shell_surface(surface);
954
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300955 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400956 set_busy_cursor(shsurf, pointer);
957 } else {
958 serial = wl_display_next_serial(compositor->wl_display);
959 ping_handler(surface, serial);
960 }
961}
962
963static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600964shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
965 uint32_t serial)
966{
967 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400968 struct desktop_shell *shell = shsurf->shell;
969 struct weston_seat *seat;
970 struct weston_compositor *ec = shsurf->surface->compositor;
971 struct wl_pointer *pointer;
972 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600973
Scott Moreauff1db4a2012-04-17 19:06:18 -0600974 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400975 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600976 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400977 if (was_unresponsive) {
978 /* Received pong from previously unresponsive client */
979 wl_list_for_each(seat, &ec->seat_list, link) {
980 pointer = seat->seat.pointer;
981 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300982 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400983 pointer->current ==
984 &shsurf->surface->surface)
985 end_busy_cursor(shsurf, pointer);
986 }
987 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600988 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600989 }
990}
991
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400992static void
993shell_surface_set_title(struct wl_client *client,
994 struct wl_resource *resource, const char *title)
995{
996 struct shell_surface *shsurf = resource->data;
997
998 free(shsurf->title);
999 shsurf->title = strdup(title);
1000}
1001
1002static void
1003shell_surface_set_class(struct wl_client *client,
1004 struct wl_resource *resource, const char *class)
1005{
1006 struct shell_surface *shsurf = resource->data;
1007
1008 free(shsurf->class);
1009 shsurf->class = strdup(class);
1010}
1011
Scott Moreauff1db4a2012-04-17 19:06:18 -06001012static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001013surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001014{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001015 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001016
1017 if (!shsurf)
1018 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001019
Ander Conselvan de Oliveira00d17bb2012-06-28 18:08:06 +03001020 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1021 return 0;
1022
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001024 if (!move)
1025 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001026
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001027 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001028 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001029 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001030 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001031
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001032 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1033 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001034
1035 return 0;
1036}
1037
1038static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001039shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001040 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001041{
Daniel Stone37816df2012-05-16 18:45:18 +01001042 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001043 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001044
Daniel Stone37816df2012-05-16 18:45:18 +01001045 if (ws->seat.pointer->button_count == 0 ||
1046 ws->seat.pointer->grab_serial != serial ||
1047 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001048 return;
1049
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001050 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001051 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001052}
1053
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001055 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001057 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001058};
1059
1060static void
Scott Moreau447013d2012-02-18 05:05:29 -07001061resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001062 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001063{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001064 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001065 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001066 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001067 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001068 wl_fixed_t from_x, from_y;
1069 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001070
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001071 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001072 return;
1073
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001074 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001075 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001077 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001078 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001079
Daniel Stone103db7f2012-05-08 17:17:55 +01001080 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001081 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001082 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001083 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001084 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001085 }
1086
Daniel Stone103db7f2012-05-08 17:17:55 +01001087 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001088 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001089 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001090 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001091 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001092 }
1093
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001094 shsurf->client->send_configure(shsurf->surface,
1095 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001096}
1097
1098static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001099send_configure(struct weston_surface *surface,
1100 uint32_t edges, int32_t width, int32_t height)
1101{
1102 struct shell_surface *shsurf = get_shell_surface(surface);
1103
1104 wl_shell_surface_send_configure(&shsurf->resource,
1105 edges, width, height);
1106}
1107
1108static const struct weston_shell_client shell_client = {
1109 send_configure
1110};
1111
1112static void
Scott Moreau447013d2012-02-18 05:05:29 -07001113resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001114 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001115{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001116 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001117 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001118 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001119
Daniel Stone4dbadb12012-05-30 16:31:51 +01001120 if (pointer->button_count == 0 &&
1121 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001122 shell_grab_end(&resize->base);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001123 free(grab);
1124 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001125}
1126
Scott Moreau447013d2012-02-18 05:05:29 -07001127static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001128 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001129 resize_grab_motion,
1130 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001131};
1132
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001133static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001134surface_resize(struct shell_surface *shsurf,
1135 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001136{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001137 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001138
Alex Wu4539b082012-03-01 12:57:46 +08001139 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1140 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001141
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001142 if (edges == 0 || edges > 15 ||
1143 (edges & 3) == 3 || (edges & 12) == 12)
1144 return 0;
1145
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001147 if (!resize)
1148 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001149
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001150 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001151 resize->width = shsurf->surface->geometry.width;
1152 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001153
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001154 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1155 ws->seat.pointer, edges);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001156
1157 return 0;
1158}
1159
1160static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001162 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164{
Daniel Stone37816df2012-05-16 18:45:18 +01001165 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001166 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001167
Alex Wu4539b082012-03-01 12:57:46 +08001168 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1169 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001170
Daniel Stone37816df2012-05-16 18:45:18 +01001171 if (ws->seat.pointer->button_count == 0 ||
1172 ws->seat.pointer->grab_serial != serial ||
1173 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001174 return;
1175
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001176 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001177 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001178}
1179
Juan Zhao96879df2012-02-07 08:45:41 +08001180static struct weston_output *
1181get_default_output(struct weston_compositor *compositor)
1182{
1183 return container_of(compositor->output_list.next,
1184 struct weston_output, link);
1185}
1186
Alex Wu4539b082012-03-01 12:57:46 +08001187static void
1188shell_unset_fullscreen(struct shell_surface *shsurf)
1189{
1190 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001191 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1192 shell_surface_is_top_fullscreen(shsurf)) {
1193 weston_output_switch_mode(shsurf->fullscreen_output,
1194 shsurf->fullscreen_output->origin);
1195 }
Alex Wu4539b082012-03-01 12:57:46 +08001196 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1197 shsurf->fullscreen.framerate = 0;
1198 wl_list_remove(&shsurf->fullscreen.transform.link);
1199 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001200 if (shsurf->fullscreen.black_surface)
1201 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001202 shsurf->fullscreen.black_surface = NULL;
1203 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001204 weston_surface_set_position(shsurf->surface,
1205 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001206 if (shsurf->saved_rotation_valid) {
1207 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1208 &shsurf->rotation.transform.link);
1209 shsurf->saved_rotation_valid = false;
1210 }
Alex Wu4539b082012-03-01 12:57:46 +08001211}
1212
Pekka Paalanen98262232011-12-01 10:42:22 +02001213static int
1214reset_shell_surface_type(struct shell_surface *surface)
1215{
1216 switch (surface->type) {
1217 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001218 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001219 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001220 case SHELL_SURFACE_MAXIMIZED:
1221 surface->output = get_default_output(surface->surface->compositor);
1222 weston_surface_set_position(surface->surface,
1223 surface->saved_x,
1224 surface->saved_y);
1225 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001226 case SHELL_SURFACE_NONE:
1227 case SHELL_SURFACE_TOPLEVEL:
1228 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001229 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001230 break;
1231 }
1232
1233 surface->type = SHELL_SURFACE_NONE;
1234 return 0;
1235}
1236
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001237static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001238set_surface_type(struct shell_surface *shsurf)
1239{
1240 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001241 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001242
1243 reset_shell_surface_type(shsurf);
1244
1245 shsurf->type = shsurf->next_type;
1246 shsurf->next_type = SHELL_SURFACE_NONE;
1247
1248 switch (shsurf->type) {
1249 case SHELL_SURFACE_TOPLEVEL:
1250 break;
1251 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001253 pes->geometry.x + shsurf->transient.x,
1254 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001255 break;
1256
1257 case SHELL_SURFACE_MAXIMIZED:
1258 shsurf->saved_x = surface->geometry.x;
1259 shsurf->saved_y = surface->geometry.y;
1260 shsurf->saved_position_valid = true;
1261 break;
1262
1263 case SHELL_SURFACE_FULLSCREEN:
1264 shsurf->saved_x = surface->geometry.x;
1265 shsurf->saved_y = surface->geometry.y;
1266 shsurf->saved_position_valid = true;
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04001267 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001268
1269 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1270 wl_list_remove(&shsurf->rotation.transform.link);
1271 wl_list_init(&shsurf->rotation.transform.link);
1272 shsurf->surface->geometry.dirty = 1;
1273 shsurf->saved_rotation_valid = true;
1274 }
1275 break;
1276
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001277 default:
1278 break;
1279 }
1280}
1281
1282static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001283set_toplevel(struct shell_surface *shsurf)
1284{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001285 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001286}
1287
1288static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001289shell_surface_set_toplevel(struct wl_client *client,
1290 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001291{
Pekka Paalanen98262232011-12-01 10:42:22 +02001292 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001293
Tiago Vignattibc052c92012-04-19 16:18:18 +03001294 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001295}
1296
1297static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001298set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001299 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001300{
1301 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001302 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001303 shsurf->transient.x = x;
1304 shsurf->transient.y = y;
1305 shsurf->transient.flags = flags;
1306 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1307}
1308
1309static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001310shell_surface_set_transient(struct wl_client *client,
1311 struct wl_resource *resource,
1312 struct wl_resource *parent_resource,
1313 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001314{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001315 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001316 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001317
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001318 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001319}
1320
Tiago Vignattibe143262012-04-16 17:31:41 +03001321static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001322shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001323{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001324 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001325}
1326
1327static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001328get_output_panel_height(struct desktop_shell *shell,
1329 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001330{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001331 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001332 int panel_height = 0;
1333
1334 if (!output)
1335 return 0;
1336
Juan Zhao4ab94682012-07-09 22:24:09 -07001337 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001338 if (surface->output == output) {
1339 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001340 break;
1341 }
1342 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001343
Juan Zhao96879df2012-02-07 08:45:41 +08001344 return panel_height;
1345}
1346
1347static void
1348shell_surface_set_maximized(struct wl_client *client,
1349 struct wl_resource *resource,
1350 struct wl_resource *output_resource )
1351{
1352 struct shell_surface *shsurf = resource->data;
1353 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001354 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001355 uint32_t edges = 0, panel_height = 0;
1356
1357 /* get the default output, if the client set it as NULL
1358 check whether the ouput is available */
1359 if (output_resource)
1360 shsurf->output = output_resource->data;
1361 else
1362 shsurf->output = get_default_output(es->compositor);
1363
Tiago Vignattibe143262012-04-16 17:31:41 +03001364 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001365 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001366 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001367
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001368 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001369 shsurf->output->current->width,
1370 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001371
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001372 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001373}
1374
Alex Wu21858432012-04-01 20:13:08 +08001375static void
1376black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1377
Alex Wu4539b082012-03-01 12:57:46 +08001378static struct weston_surface *
1379create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001380 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001381 GLfloat x, GLfloat y, int w, int h)
1382{
1383 struct weston_surface *surface = NULL;
1384
1385 surface = weston_surface_create(ec);
1386 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001387 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001388 return NULL;
1389 }
1390
Alex Wu21858432012-04-01 20:13:08 +08001391 surface->configure = black_surface_configure;
1392 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001393 weston_surface_configure(surface, x, y, w, h);
1394 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1395 return surface;
1396}
1397
1398/* Create black surface and append it to the associated fullscreen surface.
1399 * Handle size dismatch and positioning according to the method. */
1400static void
1401shell_configure_fullscreen(struct shell_surface *shsurf)
1402{
1403 struct weston_output *output = shsurf->fullscreen_output;
1404 struct weston_surface *surface = shsurf->surface;
1405 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001406 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001407
Alex Wu4539b082012-03-01 12:57:46 +08001408 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001409 shsurf->fullscreen.black_surface =
1410 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001411 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001412 output->x, output->y,
1413 output->current->width,
1414 output->current->height);
1415
1416 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1417 wl_list_insert(&surface->layer_link,
1418 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001419 shsurf->fullscreen.black_surface->output = output;
1420
1421 switch (shsurf->fullscreen.type) {
1422 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1423 break;
1424 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1425 matrix = &shsurf->fullscreen.transform.matrix;
1426 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001427
1428 output_aspect = (float) output->current->width /
1429 (float) output->current->height;
1430 surface_aspect = (float) surface->geometry.width /
1431 (float) surface->geometry.height;
1432 if (output_aspect < surface_aspect)
1433 scale = (float) output->current->width /
1434 (float) surface->geometry.width;
1435 else
1436 scale = (float) output->current->height /
1437 (float) surface->geometry.height;
1438
Alex Wu4539b082012-03-01 12:57:46 +08001439 weston_matrix_scale(matrix, scale, scale, 1);
1440 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001441 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001442 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001443 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1444 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1445 weston_surface_set_position(surface, x, y);
1446
Alex Wu4539b082012-03-01 12:57:46 +08001447 break;
1448 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001449 if (shell_surface_is_top_fullscreen(shsurf)) {
1450 struct weston_mode mode = {0,
1451 surface->geometry.width,
1452 surface->geometry.height,
1453 shsurf->fullscreen.framerate};
1454
1455 if (weston_output_switch_mode(output, &mode) == 0) {
1456 weston_surface_configure(shsurf->fullscreen.black_surface,
1457 output->x, output->y,
1458 output->current->width,
1459 output->current->height);
1460 weston_surface_set_position(surface, output->x, output->y);
1461 break;
1462 }
1463 }
Alex Wu4539b082012-03-01 12:57:46 +08001464 break;
1465 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1466 break;
1467 default:
1468 break;
1469 }
1470}
1471
1472/* make the fullscreen and black surface at the top */
1473static void
1474shell_stack_fullscreen(struct shell_surface *shsurf)
1475{
Alex Wubd3354b2012-04-17 17:20:49 +08001476 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001477 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001478 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001479
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001480 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001481 wl_list_insert(&shell->fullscreen_layer.surface_list,
1482 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001483 weston_surface_damage(surface);
1484
1485 if (!shsurf->fullscreen.black_surface)
1486 shsurf->fullscreen.black_surface =
1487 create_black_surface(surface->compositor,
1488 surface,
1489 output->x, output->y,
1490 output->current->width,
1491 output->current->height);
1492
1493 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001494 wl_list_insert(&surface->layer_link,
1495 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001496 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001497}
1498
1499static void
1500shell_map_fullscreen(struct shell_surface *shsurf)
1501{
Alex Wu4539b082012-03-01 12:57:46 +08001502 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001503 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001504}
1505
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001506static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001507shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001508 struct wl_resource *resource,
1509 uint32_t method,
1510 uint32_t framerate,
1511 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001512{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001513 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001515
1516 if (output_resource)
1517 shsurf->output = output_resource->data;
1518 else
1519 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001520
Alex Wu4539b082012-03-01 12:57:46 +08001521 shsurf->fullscreen_output = shsurf->output;
1522 shsurf->fullscreen.type = method;
1523 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001524 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001525
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001526 shsurf->client->send_configure(shsurf->surface, 0,
1527 shsurf->output->current->width,
1528 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001529}
1530
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001531static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001532popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001533 struct wl_surface *surface,
1534 wl_fixed_t x,
1535 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001536{
Daniel Stone37816df2012-05-16 18:45:18 +01001537 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001538 struct shell_surface *priv =
1539 container_of(grab, struct shell_surface, popup.grab);
1540 struct wl_client *client = priv->surface->surface.resource.client;
1541
Pekka Paalanencb108432012-01-19 16:25:40 +02001542 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001543 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544 grab->focus = surface;
1545 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001546 wl_pointer_set_focus(pointer, NULL,
1547 wl_fixed_from_int(0),
1548 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001549 grab->focus = NULL;
1550 }
1551}
1552
1553static void
Scott Moreau447013d2012-02-18 05:05:29 -07001554popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001555 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001556{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001557 struct wl_resource *resource;
1558
Daniel Stone37816df2012-05-16 18:45:18 +01001559 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001560 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001561 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001562}
1563
1564static void
Scott Moreau447013d2012-02-18 05:05:29 -07001565popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001566 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001567{
1568 struct wl_resource *resource;
1569 struct shell_surface *shsurf =
1570 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001571 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001572 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001573 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001574
Daniel Stone37816df2012-05-16 18:45:18 +01001575 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001576 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001577 display = wl_client_get_display(resource->client);
1578 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001579 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001580 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001581 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001582 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001583 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001584 wl_pointer_end_grab(grab->pointer);
1585 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586 }
1587
Daniel Stone4dbadb12012-05-30 16:31:51 +01001588 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589 shsurf->popup.initial_up = 1;
1590}
1591
Scott Moreau447013d2012-02-18 05:05:29 -07001592static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001593 popup_grab_focus,
1594 popup_grab_motion,
1595 popup_grab_button,
1596};
1597
1598static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001599shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001600{
Daniel Stone37816df2012-05-16 18:45:18 +01001601 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001602 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001603 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604
1605 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001607
Pekka Paalanen938269a2012-02-07 14:19:01 +02001608 weston_surface_update_transform(parent);
1609 if (parent->transform.enabled) {
1610 shsurf->popup.parent_transform.matrix =
1611 parent->transform.matrix;
1612 } else {
1613 /* construct x, y translation matrix */
1614 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1615 shsurf->popup.parent_transform.matrix.d[12] =
1616 parent->geometry.x;
1617 shsurf->popup.parent_transform.matrix.d[13] =
1618 parent->geometry.y;
1619 }
1620 wl_list_insert(es->geometry.transformation_list.prev,
1621 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001622 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001623
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001624 shsurf->popup.initial_up = 0;
1625
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001626 /* We don't require the grab to still be active, but if another
1627 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001628 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1629 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001630 } else {
1631 wl_shell_surface_send_popup_done(&shsurf->resource);
1632 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633}
1634
1635static void
1636shell_surface_set_popup(struct wl_client *client,
1637 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001638 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001639 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001640 struct wl_resource *parent_resource,
1641 int32_t x, int32_t y, uint32_t flags)
1642{
1643 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001644
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001645 shsurf->type = SHELL_SURFACE_POPUP;
1646 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001647 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001648 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001649 shsurf->popup.x = x;
1650 shsurf->popup.y = y;
1651}
1652
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001653static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001654 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655 shell_surface_move,
1656 shell_surface_resize,
1657 shell_surface_set_toplevel,
1658 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001659 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001660 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001661 shell_surface_set_maximized,
1662 shell_surface_set_title,
1663 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001664};
1665
1666static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001667destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668{
Daniel Stone37816df2012-05-16 18:45:18 +01001669 if (shsurf->popup.grab.pointer)
1670 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001671
Alex Wubd3354b2012-04-17 17:20:49 +08001672 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1673 shell_surface_is_top_fullscreen(shsurf)) {
1674 weston_output_switch_mode(shsurf->fullscreen_output,
1675 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001676 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001677
Alex Wuaa08e2d2012-03-05 11:01:40 +08001678 if (shsurf->fullscreen.black_surface)
1679 weston_surface_destroy(shsurf->fullscreen.black_surface);
1680
Alex Wubd3354b2012-04-17 17:20:49 +08001681 /* As destroy_resource() use wl_list_for_each_safe(),
1682 * we can always remove the listener.
1683 */
1684 wl_list_remove(&shsurf->surface_destroy_listener.link);
1685 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001686 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001687
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001688 wl_list_remove(&shsurf->link);
1689 free(shsurf);
1690}
1691
1692static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001693shell_destroy_shell_surface(struct wl_resource *resource)
1694{
1695 struct shell_surface *shsurf = resource->data;
1696
1697 destroy_shell_surface(shsurf);
1698}
1699
1700static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001701shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001702{
1703 struct shell_surface *shsurf = container_of(listener,
1704 struct shell_surface,
1705 surface_destroy_listener);
1706
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001707 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001708 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001709 } else {
1710 wl_signal_emit(&shsurf->resource.destroy_signal,
1711 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001712 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001713 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001714}
1715
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001716static void
1717shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1718
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001719static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001720get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001721{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001722 if (surface->configure == shell_surface_configure)
1723 return surface->private;
1724 else
1725 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001726}
1727
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001728static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001729create_shell_surface(void *shell, struct weston_surface *surface,
1730 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001731{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001732 struct shell_surface *shsurf;
1733
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001734 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001735 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001736 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001737 }
1738
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001739 shsurf = calloc(1, sizeof *shsurf);
1740 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001741 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001742 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001743 }
1744
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001745 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001746 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001747 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001748
Tiago Vignattibc052c92012-04-19 16:18:18 +03001749 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001750 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001751 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001752 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001753 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001754 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1755 shsurf->fullscreen.framerate = 0;
1756 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001757 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001758 wl_list_init(&shsurf->fullscreen.transform.link);
1759
Tiago Vignattibc052c92012-04-19 16:18:18 +03001760 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001761 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1762 wl_signal_add(&surface->surface.resource.destroy_signal,
1763 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001764
1765 /* init link so its safe to always remove it in destroy_shell_surface */
1766 wl_list_init(&shsurf->link);
1767
Pekka Paalanen460099f2012-01-20 16:48:25 +02001768 /* empty when not in use */
1769 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001770 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001771
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001772 wl_list_init(&shsurf->workspace_transform.link);
1773
Pekka Paalanen98262232011-12-01 10:42:22 +02001774 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001775 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001776
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001777 shsurf->client = client;
1778
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001779 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001780}
1781
1782static void
1783shell_get_shell_surface(struct wl_client *client,
1784 struct wl_resource *resource,
1785 uint32_t id,
1786 struct wl_resource *surface_resource)
1787{
1788 struct weston_surface *surface = surface_resource->data;
1789 struct desktop_shell *shell = resource->data;
1790 struct shell_surface *shsurf;
1791
1792 if (get_shell_surface(surface)) {
1793 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001794 WL_DISPLAY_ERROR_INVALID_OBJECT,
1795 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001796 return;
1797 }
1798
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001799 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001800 if (!shsurf) {
1801 wl_resource_post_error(surface_resource,
1802 WL_DISPLAY_ERROR_INVALID_OBJECT,
1803 "surface->configure already set");
1804 return;
1805 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001806
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001807 shsurf->resource.destroy = shell_destroy_shell_surface;
1808 shsurf->resource.object.id = id;
1809 shsurf->resource.object.interface = &wl_shell_surface_interface;
1810 shsurf->resource.object.implementation =
1811 (void (**)(void)) &shell_surface_implementation;
1812 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001813
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001814 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001815}
1816
1817static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001818 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001819};
1820
Kristian Høgsberg07937562011-04-12 17:25:42 -04001821static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001822handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001823{
1824 proc->pid = 0;
1825}
1826
1827static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001828launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001829{
1830 if (shell->screensaver.binding)
1831 return;
1832
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001833 if (!shell->screensaver.path)
1834 return;
1835
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001836 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001837 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001838 return;
1839 }
1840
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001841 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001842 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001843 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001844 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001845}
1846
1847static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001848terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001850 if (shell->screensaver.process.pid == 0)
1851 return;
1852
1853 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001854}
1855
1856static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001857configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001858{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001859 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001860
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001861 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1862 if (s->output == es->output && s != es) {
1863 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001864 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001865 }
1866 }
1867
1868 weston_surface_configure(es, es->output->x, es->output->y,
1869 es->buffer->width, es->buffer->height);
1870
1871 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001872 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001873 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001874 }
1875}
1876
1877static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001878background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1879{
1880 struct desktop_shell *shell = es->private;
1881
1882 configure_static_surface(es, &shell->background_layer);
1883}
1884
1885static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001886desktop_shell_set_background(struct wl_client *client,
1887 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001888 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001889 struct wl_resource *surface_resource)
1890{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001891 struct desktop_shell *shell = resource->data;
1892 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001893
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001894 if (surface->configure) {
1895 wl_resource_post_error(surface_resource,
1896 WL_DISPLAY_ERROR_INVALID_OBJECT,
1897 "surface role already assigned");
1898 return;
1899 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001900
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001901 surface->configure = background_configure;
1902 surface->private = shell;
1903 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001904 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001905 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001906 surface->output->current->width,
1907 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001908}
1909
1910static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001911panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1912{
1913 struct desktop_shell *shell = es->private;
1914
1915 configure_static_surface(es, &shell->panel_layer);
1916}
1917
1918static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001919desktop_shell_set_panel(struct wl_client *client,
1920 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001921 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001922 struct wl_resource *surface_resource)
1923{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001924 struct desktop_shell *shell = resource->data;
1925 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001926
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001927 if (surface->configure) {
1928 wl_resource_post_error(surface_resource,
1929 WL_DISPLAY_ERROR_INVALID_OBJECT,
1930 "surface role already assigned");
1931 return;
1932 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001933
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001934 surface->configure = panel_configure;
1935 surface->private = shell;
1936 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001937 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001938 surface_resource,
1939 surface->output->current->width,
1940 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001941}
1942
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001943static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001944lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1945{
1946 struct desktop_shell *shell = surface->private;
1947
1948 center_on_output(surface, get_default_output(shell->compositor));
1949
1950 if (!weston_surface_is_mapped(surface)) {
1951 wl_list_insert(&shell->lock_layer.surface_list,
1952 &surface->layer_link);
1953 weston_surface_assign_output(surface);
1954 weston_compositor_wake(shell->compositor);
1955 }
1956}
1957
1958static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001959handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001960{
Tiago Vignattibe143262012-04-16 17:31:41 +03001961 struct desktop_shell *shell =
1962 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001963
Martin Minarik6d118362012-06-07 18:01:59 +02001964 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001965 shell->lock_surface = NULL;
1966}
1967
1968static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001969desktop_shell_set_lock_surface(struct wl_client *client,
1970 struct wl_resource *resource,
1971 struct wl_resource *surface_resource)
1972{
Tiago Vignattibe143262012-04-16 17:31:41 +03001973 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001974 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001975
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001976 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001977
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001978 if (!shell->locked)
1979 return;
1980
Pekka Paalanen98262232011-12-01 10:42:22 +02001981 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001982
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001983 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1984 wl_signal_add(&surface_resource->destroy_signal,
1985 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001986
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001987 surface->configure = lock_surface_configure;
1988 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001989}
1990
1991static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001992resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001993{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001994 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001995 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001996
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001997 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1998 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001999
2000 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002001
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002002 wl_list_remove(&shell->lock_layer.link);
2003 wl_list_insert(&shell->compositor->cursor_layer.link,
2004 &shell->fullscreen_layer.link);
2005 wl_list_insert(&shell->fullscreen_layer.link,
2006 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002007 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002008
Jonas Ådahl04769742012-06-13 00:01:24 +02002009 pop_focus_state(shell, get_current_workspace(shell));
2010
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002011 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002012 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002013 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002014 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002015}
2016
2017static void
2018desktop_shell_unlock(struct wl_client *client,
2019 struct wl_resource *resource)
2020{
Tiago Vignattibe143262012-04-16 17:31:41 +03002021 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002022
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002023 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002024
2025 if (shell->locked)
2026 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002027}
2028
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002029static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002030desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002031 struct wl_resource *resource,
2032 struct wl_resource *surface_resource)
2033{
2034 struct desktop_shell *shell = resource->data;
2035
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002036 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037}
2038
Kristian Høgsberg75840622011-09-06 13:48:16 -04002039static const struct desktop_shell_interface desktop_shell_implementation = {
2040 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002041 desktop_shell_set_panel,
2042 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002044 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002045};
2046
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002047static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002048get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002049{
2050 struct shell_surface *shsurf;
2051
2052 shsurf = get_shell_surface(surface);
2053 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002054 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002055 return shsurf->type;
2056}
2057
Kristian Høgsberg75840622011-09-06 13:48:16 -04002058static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002059move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002060{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002061 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002062 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002063 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002064
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002065 if (surface == NULL)
2066 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002067
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002068 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002069 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002070 return;
2071
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002072 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002073}
2074
2075static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002076resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002077{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002078 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002079 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002080 uint32_t edges = 0;
2081 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002083
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002084 if (surface == NULL)
2085 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002088 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002089 return;
2090
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002091 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002092 wl_fixed_to_int(seat->pointer->grab_x),
2093 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002094 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002095
Pekka Paalanen60921e52012-01-25 15:55:43 +02002096 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002097 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002098 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002099 edges |= 0;
2100 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002101 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002102
Pekka Paalanen60921e52012-01-25 15:55:43 +02002103 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002104 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002105 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002106 edges |= 0;
2107 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002109
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002110 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002111}
2112
2113static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002114surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002115 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002116{
Scott Moreau02709af2012-05-22 01:54:10 -06002117 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002118 struct shell_surface *shsurf;
2119 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002120 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002121
2122 if (surface == NULL)
2123 return;
2124
2125 shsurf = get_shell_surface(surface);
2126 if (!shsurf)
2127 return;
2128
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002129 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002130
Scott Moreau02709af2012-05-22 01:54:10 -06002131 if (surface->alpha > 1.0)
2132 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002133 if (surface->alpha < step)
2134 surface->alpha = step;
2135
2136 surface->geometry.dirty = 1;
2137 weston_surface_damage(surface);
2138}
2139
2140static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002141do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002142 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002143{
Daniel Stone37816df2012-05-16 18:45:18 +01002144 struct weston_seat *ws = (struct weston_seat *) seat;
2145 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002146 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002147 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002148
2149 wl_list_for_each(output, &compositor->output_list, link) {
2150 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002151 wl_fixed_to_double(seat->pointer->x),
2152 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002153 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002154 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002155 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002156 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002157 increment = -output->zoom.increment;
2158 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002159 increment = output->zoom.increment *
2160 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002161 else
2162 increment = 0;
2163
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002164 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002165
Scott Moreaue6603982012-06-11 13:07:51 -06002166 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002167 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002168 else if (output->zoom.level > output->zoom.max_level)
2169 output->zoom.level = output->zoom.max_level;
2170 else
2171 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002172
Scott Moreaue6603982012-06-11 13:07:51 -06002173 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002174
Scott Moreau8dacaab2012-06-17 18:10:58 -06002175 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002176 }
2177 }
2178}
2179
Scott Moreauccbf29d2012-02-22 14:21:41 -07002180static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002181zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002182 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002183{
2184 do_zoom(seat, time, 0, axis, value);
2185}
2186
2187static void
2188zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2189 void *data)
2190{
2191 do_zoom(seat, time, key, 0, 0);
2192}
2193
2194static void
2195terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2196 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002197{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002198 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002199
Daniel Stone325fc2d2012-05-30 16:31:58 +01002200 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002201}
2202
2203static void
Scott Moreau447013d2012-02-18 05:05:29 -07002204rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002205 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002206{
2207 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002208 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002209 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002210 struct shell_surface *shsurf = rotate->base.shsurf;
2211 struct weston_surface *surface;
2212 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2213
2214 if (!shsurf)
2215 return;
2216
2217 surface = shsurf->surface;
2218
2219 cx = 0.5f * surface->geometry.width;
2220 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002221
Daniel Stone37816df2012-05-16 18:45:18 +01002222 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2223 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002224 r = sqrtf(dx * dx + dy * dy);
2225
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002226 wl_list_remove(&shsurf->rotation.transform.link);
2227 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002228
2229 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002230 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002231 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002232
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002233 weston_matrix_init(&rotate->rotation);
2234 rotate->rotation.d[0] = dx / r;
2235 rotate->rotation.d[4] = -dy / r;
2236 rotate->rotation.d[1] = -rotate->rotation.d[4];
2237 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002238
2239 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002240 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002241 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002242 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002243 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002244
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002245 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002246 &shsurf->surface->geometry.transformation_list,
2247 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002248 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002249 wl_list_init(&shsurf->rotation.transform.link);
2250 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002251 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002252 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002253
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002254 /* We need to adjust the position of the surface
2255 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002256 cposx = surface->geometry.x + cx;
2257 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002258 dposx = rotate->center.x - cposx;
2259 dposy = rotate->center.y - cposy;
2260 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002261 weston_surface_set_position(surface,
2262 surface->geometry.x + dposx,
2263 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002264 }
2265
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002266 /* Repaint implies weston_surface_update_transform(), which
2267 * lazily applies the damage due to rotation update.
2268 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002269 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002270}
2271
2272static void
Scott Moreau447013d2012-02-18 05:05:29 -07002273rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002274 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002275{
2276 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002277 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002278 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002279 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002280 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002281
Daniel Stone4dbadb12012-05-30 16:31:51 +01002282 if (pointer->button_count == 0 &&
2283 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002284 if (shsurf)
2285 weston_matrix_multiply(&shsurf->rotation.rotation,
2286 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002287 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002288 free(rotate);
2289 }
2290}
2291
Scott Moreau447013d2012-02-18 05:05:29 -07002292static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293 noop_grab_focus,
2294 rotate_grab_motion,
2295 rotate_grab_button,
2296};
2297
2298static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002299rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002300 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301{
2302 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002303 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002304 struct shell_surface *surface;
2305 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002306 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002307 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002308
2309 if (base_surface == NULL)
2310 return;
2311
2312 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002313 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002314 return;
2315
Pekka Paalanen460099f2012-01-20 16:48:25 +02002316 rotate = malloc(sizeof *rotate);
2317 if (!rotate)
2318 return;
2319
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002320 weston_surface_to_global_float(surface->surface,
2321 surface->surface->geometry.width / 2,
2322 surface->surface->geometry.height / 2,
2323 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002324
Daniel Stone37816df2012-05-16 18:45:18 +01002325 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2326 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002327 r = sqrtf(dx * dx + dy * dy);
2328 if (r > 20.0f) {
2329 struct weston_matrix inverse;
2330
2331 weston_matrix_init(&inverse);
2332 inverse.d[0] = dx / r;
2333 inverse.d[4] = dy / r;
2334 inverse.d[1] = -inverse.d[4];
2335 inverse.d[5] = inverse.d[0];
2336 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002337
2338 weston_matrix_init(&rotate->rotation);
2339 rotate->rotation.d[0] = dx / r;
2340 rotate->rotation.d[4] = -dy / r;
2341 rotate->rotation.d[1] = -rotate->rotation.d[4];
2342 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002343 } else {
2344 weston_matrix_init(&surface->rotation.rotation);
2345 weston_matrix_init(&rotate->rotation);
2346 }
2347
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002348 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2349 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002350}
2351
2352static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002353lower_fullscreen_layer(struct desktop_shell *shell)
2354{
2355 struct workspace *ws;
2356 struct weston_surface *surface, *prev;
2357
2358 ws = get_current_workspace(shell);
2359 wl_list_for_each_reverse_safe(surface, prev,
2360 &shell->fullscreen_layer.surface_list,
2361 layer_link)
2362 weston_surface_restack(surface, &ws->layer.surface_list);
2363}
2364
2365static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002366activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002367 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002368{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002369 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002370
Daniel Stone37816df2012-05-16 18:45:18 +01002371 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002372
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002373 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002374 case SHELL_SURFACE_FULLSCREEN:
2375 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002376 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002377 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002378 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002379 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002380 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002381 lower_fullscreen_layer(shell);
2382 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002383 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002384 }
2385}
2386
Alex Wu21858432012-04-01 20:13:08 +08002387/* no-op func for checking black surface */
2388static void
2389black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2390{
2391}
2392
2393static bool
2394is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2395{
2396 if (es->configure == black_surface_configure) {
2397 if (fs_surface)
2398 *fs_surface = (struct weston_surface *)es->private;
2399 return true;
2400 }
2401 return false;
2402}
2403
Kristian Høgsberg75840622011-09-06 13:48:16 -04002404static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002405click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002406 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002407{
Daniel Stone37816df2012-05-16 18:45:18 +01002408 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002409 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002410 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002411 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002412
Daniel Stone37816df2012-05-16 18:45:18 +01002413 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002414 if (!focus)
2415 return;
2416
Alex Wu21858432012-04-01 20:13:08 +08002417 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002418 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002419
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002420 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2421 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002422
Daniel Stone325fc2d2012-05-30 16:31:58 +01002423 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002424 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002425}
2426
2427static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002428lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002429{
Tiago Vignattibe143262012-04-16 17:31:41 +03002430 struct desktop_shell *shell =
2431 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002432 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002433 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002434
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002435 if (shell->locked) {
2436 wl_list_for_each(output, &shell->compositor->output_list, link)
2437 /* TODO: find a way to jump to other DPMS levels */
2438 if (output->set_dpms)
2439 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002440 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002441 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002442
2443 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002444
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002445 /* Hide all surfaces by removing the fullscreen, panel and
2446 * toplevel layers. This way nothing else can show or receive
2447 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002448
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002449 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002450 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002451 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002452 wl_list_insert(&shell->compositor->cursor_layer.link,
2453 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002454
Pekka Paalanen77346a62011-11-30 16:26:35 +02002455 launch_screensaver(shell);
2456
Jonas Ådahl04769742012-06-13 00:01:24 +02002457 /* stash keyboard foci in current workspace */
2458 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459
2460 /* TODO: disable bindings that should not work while locked. */
2461
2462 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002463}
2464
2465static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002466unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002467{
Tiago Vignattibe143262012-04-16 17:31:41 +03002468 struct desktop_shell *shell =
2469 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002470
Pekka Paalanend81c2162011-11-16 13:47:34 +02002471 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002472 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002473 return;
2474 }
2475
2476 /* If desktop-shell client has gone away, unlock immediately. */
2477 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002478 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479 return;
2480 }
2481
2482 if (shell->prepare_event_sent)
2483 return;
2484
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002485 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002486 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002487}
2488
2489static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002490show_input_panels(struct wl_listener *listener, void *data)
2491{
2492 struct desktop_shell *shell =
2493 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002494 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002495
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002496 wl_list_for_each_safe(surface, next,
2497 &shell->input_panel.surfaces, layer_link) {
2498 wl_list_remove(&surface->layer_link);
2499 wl_list_insert(&shell->panel_layer.surface_list,
2500 &surface->layer_link);
2501 weston_surface_assign_output(surface);
2502 weston_surface_damage(surface);
2503 weston_slide_run(surface,
2504 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002505 }
2506}
2507
2508static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002509input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2510
2511static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002512hide_input_panels(struct wl_listener *listener, void *data)
2513{
2514 struct desktop_shell *shell =
2515 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002516 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002517
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002518 wl_list_for_each_safe(surface, next,
2519 &shell->panel_layer.surface_list, layer_link)
2520 if (surface->configure == input_panel_configure) {
2521 weston_surface_unmap(surface);
2522 wl_list_insert(&shell->input_panel.surfaces,
2523 &surface->layer_link);
2524 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002525}
2526
2527static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002529{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002530 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002531 GLfloat x = (mode->width - surface->buffer->width) / 2;
2532 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002533
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002534 weston_surface_configure(surface, output->x + x, output->y + y,
2535 surface->buffer->width,
2536 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002537}
2538
2539static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002540map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002541 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002542{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002543 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002544 struct shell_surface *shsurf = get_shell_surface(surface);
2545 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002546 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002547 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002548 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002549 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002550
Pekka Paalanen60921e52012-01-25 15:55:43 +02002551 surface->geometry.width = width;
2552 surface->geometry.height = height;
2553 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002554
2555 /* initial positioning, see also configure() */
2556 switch (surface_type) {
2557 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002558 weston_surface_set_position(surface, 10 + random() % 400,
2559 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002560 break;
Alex Wu4539b082012-03-01 12:57:46 +08002561 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002562 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002563 shell_map_fullscreen(shsurf);
2564 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002565 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002566 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002567 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002568 weston_surface_set_position(surface, shsurf->output->x,
2569 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002570 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002571 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002572 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002573 case SHELL_SURFACE_NONE:
2574 weston_surface_set_position(surface,
2575 surface->geometry.x + sx,
2576 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002577 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002578 default:
2579 ;
2580 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002581
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002582 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002583 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002584 case SHELL_SURFACE_POPUP:
2585 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002586 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002587 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2588 break;
Alex Wu4539b082012-03-01 12:57:46 +08002589 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002590 case SHELL_SURFACE_NONE:
2591 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002592 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002593 ws = get_current_workspace(shell);
2594 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002595 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002596 }
2597
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002598 if (surface_type != SHELL_SURFACE_NONE) {
2599 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002600 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2601 surface->output = shsurf->output;
2602 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002603
Juan Zhao7bb92f02011-12-15 11:31:51 -05002604 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002605 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002606 if (shsurf->transient.flags ==
2607 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2608 break;
2609 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002610 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002611 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002612 if (!shell->locked) {
2613 wl_list_for_each(seat, &compositor->seat_list, link)
2614 activate(shell, surface, seat);
2615 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002616 break;
2617 default:
2618 break;
2619 }
2620
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002621 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002622 {
2623 switch (shell->win_animation_type) {
2624 case ANIMATION_FADE:
2625 weston_fade_run(surface, NULL, NULL);
2626 break;
2627 case ANIMATION_ZOOM:
2628 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2629 break;
2630 default:
2631 break;
2632 }
2633 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002634}
2635
2636static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002637configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002638 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002639{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002640 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2641 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002642
Pekka Paalanen77346a62011-11-30 16:26:35 +02002643 shsurf = get_shell_surface(surface);
2644 if (shsurf)
2645 surface_type = shsurf->type;
2646
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002647 surface->geometry.x = x;
2648 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002649 surface->geometry.width = width;
2650 surface->geometry.height = height;
2651 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002652
2653 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002654 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002655 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002656 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002657 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002658 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002659 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002660 surface->geometry.x = surface->output->x;
2661 surface->geometry.y = surface->output->y +
2662 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002663 break;
Alex Wu4539b082012-03-01 12:57:46 +08002664 case SHELL_SURFACE_TOPLEVEL:
2665 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002666 default:
2667 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002668 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002669
Alex Wu4539b082012-03-01 12:57:46 +08002670 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002671 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002672 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002673
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002674 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002675 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002676 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002677}
2678
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002679static void
2680shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2681{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002682 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002683 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002684 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002685
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002686 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002687 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002688 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002689 type_changed = 1;
2690 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002691
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002692 if (!weston_surface_is_mapped(es)) {
2693 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002694 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002695 es->geometry.width != es->buffer->width ||
2696 es->geometry.height != es->buffer->height) {
2697 GLfloat from_x, from_y;
2698 GLfloat to_x, to_y;
2699
2700 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2701 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2702 configure(shell, es,
2703 es->geometry.x + to_x - from_x,
2704 es->geometry.y + to_y - from_y,
2705 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002706 }
2707}
2708
Tiago Vignattibe143262012-04-16 17:31:41 +03002709static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002710
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002711static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002712desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002713{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002714 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002715 struct desktop_shell *shell =
2716 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002717
2718 shell->child.process.pid = 0;
2719 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002720
2721 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2722 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002723 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002724 shell->child.deathstamp = time;
2725 shell->child.deathcount = 0;
2726 }
2727
2728 shell->child.deathcount++;
2729 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002730 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002731 return;
2732 }
2733
Martin Minarik6d118362012-06-07 18:01:59 +02002734 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002735 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002736}
2737
2738static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002739launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002740{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002741 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002742
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002743 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002744 &shell->child.process,
2745 shell_exe,
2746 desktop_shell_sigchld);
2747
2748 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002749 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002750 return 0;
2751}
2752
2753static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002754bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2755{
Tiago Vignattibe143262012-04-16 17:31:41 +03002756 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002757
2758 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002759 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002760}
2761
Kristian Høgsberg75840622011-09-06 13:48:16 -04002762static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002763unbind_desktop_shell(struct wl_resource *resource)
2764{
Tiago Vignattibe143262012-04-16 17:31:41 +03002765 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002766
2767 if (shell->locked)
2768 resume_desktop(shell);
2769
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002770 shell->child.desktop_shell = NULL;
2771 shell->prepare_event_sent = false;
2772 free(resource);
2773}
2774
2775static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002776bind_desktop_shell(struct wl_client *client,
2777 void *data, uint32_t version, uint32_t id)
2778{
Tiago Vignattibe143262012-04-16 17:31:41 +03002779 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002780 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002781
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002782 resource = wl_client_add_object(client, &desktop_shell_interface,
2783 &desktop_shell_implementation,
2784 id, shell);
2785
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002786 if (client == shell->child.client) {
2787 resource->destroy = unbind_desktop_shell;
2788 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002789 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002790 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002791
2792 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2793 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002794 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002795}
2796
Pekka Paalanen6e168112011-11-24 11:34:05 +02002797static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002798screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2799{
2800 struct desktop_shell *shell = surface->private;
2801
2802 if (!shell->locked)
2803 return;
2804
2805 center_on_output(surface, surface->output);
2806
2807 if (wl_list_empty(&surface->layer_link)) {
2808 wl_list_insert(shell->lock_layer.surface_list.prev,
2809 &surface->layer_link);
2810 weston_surface_assign_output(surface);
2811 shell->compositor->idle_time = shell->screensaver.duration;
2812 weston_compositor_wake(shell->compositor);
2813 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2814 }
2815}
2816
2817static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002818screensaver_set_surface(struct wl_client *client,
2819 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002820 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002821 struct wl_resource *output_resource)
2822{
Tiago Vignattibe143262012-04-16 17:31:41 +03002823 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002824 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002825 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002826
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002827 surface->configure = screensaver_configure;
2828 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002829 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002830}
2831
2832static const struct screensaver_interface screensaver_implementation = {
2833 screensaver_set_surface
2834};
2835
2836static void
2837unbind_screensaver(struct wl_resource *resource)
2838{
Tiago Vignattibe143262012-04-16 17:31:41 +03002839 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002840
Pekka Paalanen77346a62011-11-30 16:26:35 +02002841 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002842 free(resource);
2843}
2844
2845static void
2846bind_screensaver(struct wl_client *client,
2847 void *data, uint32_t version, uint32_t id)
2848{
Tiago Vignattibe143262012-04-16 17:31:41 +03002849 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002850 struct wl_resource *resource;
2851
2852 resource = wl_client_add_object(client, &screensaver_interface,
2853 &screensaver_implementation,
2854 id, shell);
2855
Pekka Paalanen77346a62011-11-30 16:26:35 +02002856 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002857 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002858 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002859 return;
2860 }
2861
2862 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2863 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002864 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002865}
2866
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002867static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002868input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2869{
2870 struct weston_mode *mode = surface->output->current;
2871 GLfloat x = (mode->width - surface->buffer->width) / 2;
2872 GLfloat y = mode->height - surface->buffer->height;
2873
2874 /* Don't map the input panel here, wait for
2875 * show_input_panels signal. */
2876
2877 weston_surface_configure(surface,
2878 surface->output->x + x,
2879 surface->output->y + y,
2880 surface->buffer->width,
2881 surface->buffer->height);
2882}
2883
2884static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002885input_panel_set_surface(struct wl_client *client,
2886 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002887 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002888 struct wl_resource *output_resource)
2889{
2890 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002891 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002892 struct weston_output *output = output_resource->data;
2893
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002894 surface->configure = input_panel_configure;
2895 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002896 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002897 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002898}
2899
2900static const struct input_panel_interface input_panel_implementation = {
2901 input_panel_set_surface
2902};
2903
2904static void
2905unbind_input_panel(struct wl_resource *resource)
2906{
2907 struct desktop_shell *shell = resource->data;
2908
2909 shell->input_panel.binding = NULL;
2910 free(resource);
2911}
2912
2913static void
2914bind_input_panel(struct wl_client *client,
2915 void *data, uint32_t version, uint32_t id)
2916{
2917 struct desktop_shell *shell = data;
2918 struct wl_resource *resource;
2919
2920 resource = wl_client_add_object(client, &input_panel_interface,
2921 &input_panel_implementation,
2922 id, shell);
2923
2924 if (shell->input_panel.binding == NULL) {
2925 resource->destroy = unbind_input_panel;
2926 shell->input_panel.binding = resource;
2927 return;
2928 }
2929
2930 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2931 "interface object already bound");
2932 wl_resource_destroy(resource);
2933}
2934
Kristian Høgsberg07045392012-02-19 18:52:44 -05002935struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002936 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002937 struct weston_surface *current;
2938 struct wl_listener listener;
2939 struct wl_keyboard_grab grab;
2940};
2941
2942static void
2943switcher_next(struct switcher *switcher)
2944{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002945 struct weston_surface *surface;
2946 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002947 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002948 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002949
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002950 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002951 switch (get_shell_surface_type(surface)) {
2952 case SHELL_SURFACE_TOPLEVEL:
2953 case SHELL_SURFACE_FULLSCREEN:
2954 case SHELL_SURFACE_MAXIMIZED:
2955 if (first == NULL)
2956 first = surface;
2957 if (prev == switcher->current)
2958 next = surface;
2959 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002960 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002961 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002962 weston_surface_damage(surface);
2963 break;
2964 default:
2965 break;
2966 }
Alex Wu1659daa2012-04-01 20:13:09 +08002967
2968 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002969 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002970 surface->geometry.dirty = 1;
2971 weston_surface_damage(surface);
2972 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002973 }
2974
2975 if (next == NULL)
2976 next = first;
2977
Alex Wu07b26062012-03-12 16:06:01 +08002978 if (next == NULL)
2979 return;
2980
Kristian Høgsberg07045392012-02-19 18:52:44 -05002981 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002982 wl_signal_add(&next->surface.resource.destroy_signal,
2983 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002984
2985 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002986 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002987
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002988 shsurf = get_shell_surface(switcher->current);
2989 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002990 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002991}
2992
2993static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002994switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002995{
2996 struct switcher *switcher =
2997 container_of(listener, struct switcher, listener);
2998
2999 switcher_next(switcher);
3000}
3001
3002static void
Daniel Stone351eb612012-05-31 15:27:47 -04003003switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003004{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003005 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003006 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003007 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003008
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003009 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003010 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003011 weston_surface_damage(surface);
3012 }
3013
Alex Wu07b26062012-03-12 16:06:01 +08003014 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003015 activate(switcher->shell, switcher->current,
3016 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003017 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003018 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003019 free(switcher);
3020}
3021
3022static void
3023switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003024 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003025{
3026 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003027 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003028
Daniel Stonec9785ea2012-05-30 16:31:52 +01003029 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003030 switcher_next(switcher);
3031}
3032
3033static void
3034switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3035 uint32_t mods_depressed, uint32_t mods_latched,
3036 uint32_t mods_locked, uint32_t group)
3037{
3038 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003039 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003040
Daniel Stone351eb612012-05-31 15:27:47 -04003041 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3042 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003043}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044
3045static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003046 switcher_key,
3047 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003048};
3049
3050static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003051switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3052 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003053{
Tiago Vignattibe143262012-04-16 17:31:41 +03003054 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003055 struct switcher *switcher;
3056
3057 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003058 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003059 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003060 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003061 wl_list_init(&switcher->listener.link);
3062
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003063 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003064 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003065 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3066 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003067 switcher_next(switcher);
3068}
3069
Pekka Paalanen3c647232011-12-22 13:43:43 +02003070static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003071backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3072 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003073{
3074 struct weston_compositor *compositor = data;
3075 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003076 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003077
3078 /* TODO: we're limiting to simple use cases, where we assume just
3079 * control on the primary display. We'd have to extend later if we
3080 * ever get support for setting backlights on random desktop LCD
3081 * panels though */
3082 output = get_default_output(compositor);
3083 if (!output)
3084 return;
3085
3086 if (!output->set_backlight)
3087 return;
3088
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003089 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3090 backlight_new = output->backlight_current - 25;
3091 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3092 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003093
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003094 if (backlight_new < 5)
3095 backlight_new = 5;
3096 if (backlight_new > 255)
3097 backlight_new = 255;
3098
3099 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003100 output->set_backlight(output, output->backlight_current);
3101}
3102
3103static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003104debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3105 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003106{
Tiago Vignattibe143262012-04-16 17:31:41 +03003107 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003108 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003109 struct weston_surface *surface;
3110
3111 if (shell->debug_repaint_surface) {
3112 weston_surface_destroy(shell->debug_repaint_surface);
3113 shell->debug_repaint_surface = NULL;
3114 } else {
3115 surface = weston_surface_create(compositor);
3116 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3117 weston_surface_configure(surface, 0, 0, 8192, 8192);
3118 wl_list_insert(&compositor->fade_layer.surface_list,
3119 &surface->layer_link);
3120 weston_surface_assign_output(surface);
3121 pixman_region32_init(&surface->input);
3122
3123 /* Here's the dirty little trick that makes the
3124 * repaint debugging work: we force an
3125 * update_transform first to update dependent state
3126 * and clear the geometry.dirty bit. Then we clear
3127 * the surface damage so it only gets repainted
3128 * piecewise as we repaint other things. */
3129
3130 weston_surface_update_transform(surface);
3131 pixman_region32_fini(&surface->damage);
3132 pixman_region32_init(&surface->damage);
3133 shell->debug_repaint_surface = surface;
3134 }
3135}
3136
3137static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003138force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3139 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003140{
3141 struct wl_client *client;
3142 pid_t pid;
3143 uid_t uid;
3144 gid_t gid;
3145
Daniel Stone325fc2d2012-05-30 16:31:58 +01003146 client = seat->keyboard->focus->resource.client;
3147 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003148
Daniel Stone325fc2d2012-05-30 16:31:58 +01003149 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003150}
3151
3152static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003153workspace_up_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 != 0)
3162 new_index--;
3163
3164 change_workspace(shell, new_index);
3165}
3166
3167static void
3168workspace_down_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 = shell->workspaces.current;
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 if (new_index < shell->workspaces.num - 1)
3177 new_index++;
3178
3179 change_workspace(shell, new_index);
3180}
3181
3182static void
3183workspace_f_binding(struct wl_seat *seat, uint32_t time,
3184 uint32_t key, void *data)
3185{
3186 struct desktop_shell *shell = data;
3187 unsigned int new_index;
3188
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003189 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003190 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003191 new_index = key - KEY_F1;
3192 if (new_index >= shell->workspaces.num)
3193 new_index = shell->workspaces.num - 1;
3194
3195 change_workspace(shell, new_index);
3196}
3197
3198
3199static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003200shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003201{
Tiago Vignattibe143262012-04-16 17:31:41 +03003202 struct desktop_shell *shell =
3203 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003204 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003205
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003206 if (shell->child.client)
3207 wl_client_destroy(shell->child.client);
3208
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003209 wl_list_remove(&shell->lock_listener.link);
3210 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003211 wl_list_remove(&shell->show_input_panel_listener.link);
3212 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003213
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003214 wl_array_for_each(ws, &shell->workspaces.array)
3215 workspace_destroy(*ws);
3216 wl_array_release(&shell->workspaces.array);
3217
Pekka Paalanen3c647232011-12-22 13:43:43 +02003218 free(shell->screensaver.path);
3219 free(shell);
3220}
3221
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003222static void
3223shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3224{
3225 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003226 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003227
3228 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003229 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3230 MODIFIER_CTRL | MODIFIER_ALT,
3231 terminate_binding, ec);
3232 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3233 click_to_activate_binding,
3234 shell);
3235 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3236 MODIFIER_SUPER | MODIFIER_ALT,
3237 surface_opacity_binding, NULL);
3238 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3239 MODIFIER_SUPER, zoom_axis_binding,
3240 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003241
3242 /* configurable bindings */
3243 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003244 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3245 zoom_key_binding, NULL);
3246 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3247 zoom_key_binding, NULL);
3248 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3249 shell);
3250 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3251 resize_binding, shell);
3252 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3253 rotate_binding, NULL);
3254 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3255 shell);
3256 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3257 ec);
3258 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3259 backlight_binding, ec);
3260 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3261 ec);
3262 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3263 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003264 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003265 debug_repaint_binding, shell);
3266 weston_compositor_add_key_binding(ec, KEY_K, mod,
3267 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003268 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3269 workspace_up_binding, shell);
3270 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3271 workspace_down_binding, shell);
3272
3273 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3274 if (shell->workspaces.num > 1) {
3275 num_workspace_bindings = shell->workspaces.num;
3276 if (num_workspace_bindings > 6)
3277 num_workspace_bindings = 6;
3278 for (i = 0; i < num_workspace_bindings; i++)
3279 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3280 workspace_f_binding,
3281 shell);
3282 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003283}
3284
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003285int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003286shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003287
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003288WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003289shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003290{
Tiago Vignattibe143262012-04-16 17:31:41 +03003291 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003292 struct workspace **pws;
3293 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003294
3295 shell = malloc(sizeof *shell);
3296 if (shell == NULL)
3297 return -1;
3298
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003299 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003300 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003301
3302 shell->destroy_listener.notify = shell_destroy;
3303 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3304 shell->lock_listener.notify = lock;
3305 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3306 shell->unlock_listener.notify = unlock;
3307 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003308 shell->show_input_panel_listener.notify = show_input_panels;
3309 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3310 shell->hide_input_panel_listener.notify = hide_input_panels;
3311 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003312 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003313 ec->shell_interface.create_shell_surface = create_shell_surface;
3314 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003315 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003316 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003317 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003318
Pekka Paalanen77346a62011-11-30 16:26:35 +02003319 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003320 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003321
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003322 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3323 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003324 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3325 weston_layer_init(&shell->lock_layer, NULL);
3326
3327 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003328
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003329 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003330
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003331 for (i = 0; i < shell->workspaces.num; i++) {
3332 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3333 if (pws == NULL)
3334 return -1;
3335
3336 *pws = workspace_create();
3337 if (*pws == NULL)
3338 return -1;
3339 }
3340 activate_workspace(shell, 0);
3341
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003342 wl_list_init(&shell->workspaces.animation.link);
3343 shell->workspaces.animation.frame = animate_workspace_change_frame;
3344
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003345 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3346 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003347 return -1;
3348
Kristian Høgsberg75840622011-09-06 13:48:16 -04003349 if (wl_display_add_global(ec->wl_display,
3350 &desktop_shell_interface,
3351 shell, bind_desktop_shell) == NULL)
3352 return -1;
3353
Pekka Paalanen6e168112011-11-24 11:34:05 +02003354 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3355 shell, bind_screensaver) == NULL)
3356 return -1;
3357
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003358 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3359 shell, bind_input_panel) == NULL)
3360 return -1;
3361
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003362 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003363 if (launch_desktop_shell_process(shell) != 0)
3364 return -1;
3365
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003366 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003367 if (ec->seat->seat.pointer)
3368 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3369 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003370
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003371 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003372
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003373 return 0;
3374}