blob: cdbb75674ca5e01c04f368024b4886aeb301decc [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"
Jonas Ådahle9d22502012-08-29 22:13:01 +020038#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020039#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Jonas Ådahle3cddce2012-06-13 00:01:22 +020060struct workspace {
61 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020062
63 struct wl_list focus_list;
64 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020065};
66
Philipp Brüschweiler88013572012-08-06 13:44:42 +020067struct input_panel_surface {
68 struct wl_list link;
69 struct weston_surface *surface;
70 struct wl_listener listener;
71};
72
Tiago Vignattibe143262012-04-16 17:31:41 +030073struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040075
76 struct wl_listener lock_listener;
77 struct wl_listener unlock_listener;
78 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020079 struct wl_listener show_input_panel_listener;
80 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020081
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050082 struct weston_layer fullscreen_layer;
83 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050084 struct weston_layer background_layer;
85 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020086 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050087
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040088 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030089 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040090
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020091 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020093 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020094 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020095
96 unsigned deathcount;
97 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020099
100 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200101 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200102 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200103
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400104 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500105 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100106
Pekka Paalanen77346a62011-11-30 16:26:35 +0200107 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200108 struct wl_array array;
109 unsigned int current;
110 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200111
Jonas Ådahle9d22502012-08-29 22:13:01 +0200112 struct wl_list client_list;
113
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200114 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200115 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200116 int anim_dir;
117 uint32_t anim_timestamp;
118 double anim_current;
119 struct workspace *anim_from;
120 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200121 } workspaces;
122
123 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200124 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200125 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200126 struct wl_resource *binding;
127 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200129 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500130
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200131 struct {
132 struct wl_resource *binding;
133 struct wl_list surfaces;
134 } input_panel;
135
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300136 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800137 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400138};
139
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500140enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200141 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500142 SHELL_SURFACE_TOPLEVEL,
143 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500144 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800145 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500146 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200147};
148
Scott Moreauff1db4a2012-04-17 19:06:18 -0600149struct ping_timer {
150 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600151 uint32_t serial;
152};
153
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200154struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200155 struct wl_resource resource;
156
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500157 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200158 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400159 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300160 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200161
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400162 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400163 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500164 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800165 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800166 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600167 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100168
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500169 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200170 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500171 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200172 } rotation;
173
174 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700175 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500176 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200177 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500178 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100179 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400180 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500181 } popup;
182
Alex Wu4539b082012-03-01 12:57:46 +0800183 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300184 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400185 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300186 } transient;
187
188 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800189 enum wl_shell_surface_fullscreen_method type;
190 struct weston_transform transform; /* matrix from x, y */
191 uint32_t framerate;
192 struct weston_surface *black_surface;
193 } fullscreen;
194
Scott Moreauff1db4a2012-04-17 19:06:18 -0600195 struct ping_timer *ping_timer;
196
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200197 struct weston_transform workspace_transform;
198
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500199 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500200 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100201 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400202
203 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200204};
205
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700207 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300208 struct shell_surface *shsurf;
209 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300210 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300211};
212
213struct weston_move_grab {
214 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100215 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216};
217
Pekka Paalanen460099f2012-01-20 16:48:25 +0200218struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300219 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500220 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200221 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200222 float x;
223 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200224 } center;
225};
226
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400227static void
228activate(struct desktop_shell *shell, struct weston_surface *es,
229 struct weston_seat *seat);
230
231static struct workspace *
232get_current_workspace(struct desktop_shell *shell);
233
Alex Wubd3354b2012-04-17 17:20:49 +0800234static struct shell_surface *
235get_shell_surface(struct weston_surface *surface);
236
237static struct desktop_shell *
238shell_surface_get_shell(struct shell_surface *shsurf);
239
240static bool
241shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
242{
243 struct desktop_shell *shell;
244 struct weston_surface *top_fs_es;
245
246 shell = shell_surface_get_shell(shsurf);
247
248 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
249 return false;
250
251 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
252 struct weston_surface,
253 layer_link);
254 return (shsurf == get_shell_surface(top_fs_es));
255}
256
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500257static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400258destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300259{
260 struct shell_grab *grab;
261
262 grab = container_of(listener, struct shell_grab,
263 shsurf_destroy_listener);
264
265 grab->shsurf = NULL;
266}
267
268static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400269popup_grab_end(struct wl_pointer *pointer);
270
271static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300272shell_grab_start(struct shell_grab *grab,
273 const struct wl_pointer_grab_interface *interface,
274 struct shell_surface *shsurf,
275 struct wl_pointer *pointer,
276 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300278 struct desktop_shell *shell = shsurf->shell;
279
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400280 popup_grab_end(pointer);
281
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300282 grab->grab.interface = interface;
283 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400284 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
285 wl_signal_add(&shsurf->resource.destroy_signal,
286 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300287
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300288 grab->pointer = pointer;
289 grab->grab.focus = &shsurf->surface->surface;
290
291 wl_pointer_start_grab(pointer, &grab->grab);
292 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
293 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
294 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300295}
296
297static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300298shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300299{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400300 if (grab->shsurf)
301 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300302
303 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300304}
305
306static void
Alex Wu4539b082012-03-01 12:57:46 +0800307center_on_output(struct weston_surface *surface,
308 struct weston_output *output);
309
Daniel Stone496ca172012-05-30 16:31:42 +0100310static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300311get_modifier(char *modifier)
312{
313 if (!modifier)
314 return MODIFIER_SUPER;
315
316 if (!strcmp("ctrl", modifier))
317 return MODIFIER_CTRL;
318 else if (!strcmp("alt", modifier))
319 return MODIFIER_ALT;
320 else if (!strcmp("super", modifier))
321 return MODIFIER_SUPER;
322 else
323 return MODIFIER_SUPER;
324}
325
Juan Zhaoe10d2792012-04-25 19:09:52 +0800326static enum animation_type
327get_animation_type(char *animation)
328{
329 if (!animation)
330 return ANIMATION_NONE;
331
332 if (!strcmp("zoom", animation))
333 return ANIMATION_ZOOM;
334 else if (!strcmp("fade", animation))
335 return ANIMATION_FADE;
336 else
337 return ANIMATION_NONE;
338}
339
Alex Wu4539b082012-03-01 12:57:46 +0800340static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300341shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200342{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200343 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200344 char *path = NULL;
345 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200346 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300347 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800348 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200349
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400350 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300351 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800352 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200353 { "num-workspaces",
354 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200355 };
356
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400357 struct config_key saver_keys[] = {
358 { "path", CONFIG_KEY_STRING, &path },
359 { "duration", CONFIG_KEY_INTEGER, &duration },
360 };
361
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200362 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400363 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200364 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
365 };
366
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200367 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500368 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200369 free(config_file);
370
Pekka Paalanen7296e792011-12-07 16:22:00 +0200371 shell->screensaver.path = path;
372 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300373 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800374 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200375 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
376}
377
378static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200379focus_state_destroy(struct focus_state *state)
380{
381 wl_list_remove(&state->seat_destroy_listener.link);
382 wl_list_remove(&state->surface_destroy_listener.link);
383 free(state);
384}
385
386static void
387focus_state_seat_destroy(struct wl_listener *listener, void *data)
388{
389 struct focus_state *state = container_of(listener,
390 struct focus_state,
391 seat_destroy_listener);
392
393 wl_list_remove(&state->link);
394 focus_state_destroy(state);
395}
396
397static void
398focus_state_surface_destroy(struct wl_listener *listener, void *data)
399{
400 struct focus_state *state = container_of(listener,
401 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400402 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400403 struct desktop_shell *shell;
404 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200405
Kristian Høgsberge3778222012-07-31 17:29:30 -0400406 next = NULL;
407 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
408 if (surface == state->keyboard_focus)
409 continue;
410
411 next = surface;
412 break;
413 }
414
415 if (next) {
416 shell = state->seat->compositor->shell_interface.shell;
417 activate(shell, next, state->seat);
418 } else {
419 wl_list_remove(&state->link);
420 focus_state_destroy(state);
421 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200422}
423
424static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400425focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200426{
Jonas Ådahl04769742012-06-13 00:01:24 +0200427 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200428
429 state = malloc(sizeof *state);
430 if (state == NULL)
431 return NULL;
432
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400433 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200434 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400435 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200436
437 state->seat_destroy_listener.notify = focus_state_seat_destroy;
438 state->surface_destroy_listener.notify = focus_state_surface_destroy;
439 wl_signal_add(&seat->seat.destroy_signal,
440 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400441 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200442
443 return state;
444}
445
Jonas Ådahl8538b222012-08-29 22:13:03 +0200446static struct focus_state *
447ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
448{
449 struct workspace *ws = get_current_workspace(shell);
450 struct focus_state *state;
451
452 wl_list_for_each(state, &ws->focus_list, link)
453 if (state->seat == seat)
454 break;
455
456 if (&state->link == &ws->focus_list)
457 state = focus_state_create(seat, ws);
458
459 return state;
460}
461
Jonas Ådahl04769742012-06-13 00:01:24 +0200462static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400463restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200464{
465 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200466 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200467
468 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200469 surface = state->keyboard_focus ?
470 &state->keyboard_focus->surface : NULL;
471
472 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200473 }
474}
475
476static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200477replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
478 struct weston_seat *seat)
479{
480 struct focus_state *state;
481 struct wl_surface *surface;
482
483 wl_list_for_each(state, &ws->focus_list, link) {
484 if (state->seat == seat) {
485 surface = seat->seat.keyboard->focus;
486 state->keyboard_focus =
487 (struct weston_surface *) surface;
488 return;
489 }
490 }
491}
492
493static void
494drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
495 struct weston_surface *surface)
496{
497 struct focus_state *state;
498
499 wl_list_for_each(state, &ws->focus_list, link)
500 if (state->keyboard_focus == surface)
501 state->keyboard_focus = NULL;
502}
503
504static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200505workspace_destroy(struct workspace *ws)
506{
Jonas Ådahl04769742012-06-13 00:01:24 +0200507 struct focus_state *state, *next;
508
509 wl_list_for_each_safe(state, next, &ws->focus_list, link)
510 focus_state_destroy(state);
511
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200512 free(ws);
513}
514
Jonas Ådahl04769742012-06-13 00:01:24 +0200515static void
516seat_destroyed(struct wl_listener *listener, void *data)
517{
518 struct weston_seat *seat = data;
519 struct focus_state *state, *next;
520 struct workspace *ws = container_of(listener,
521 struct workspace,
522 seat_destroyed_listener);
523
524 wl_list_for_each_safe(state, next, &ws->focus_list, link)
525 if (state->seat == seat)
526 wl_list_remove(&state->link);
527}
528
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200529static struct workspace *
530workspace_create(void)
531{
532 struct workspace *ws = malloc(sizeof *ws);
533 if (ws == NULL)
534 return NULL;
535
536 weston_layer_init(&ws->layer, NULL);
537
Jonas Ådahl04769742012-06-13 00:01:24 +0200538 wl_list_init(&ws->focus_list);
539 wl_list_init(&ws->seat_destroyed_listener.link);
540 ws->seat_destroyed_listener.notify = seat_destroyed;
541
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200542 return ws;
543}
544
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200545static int
546workspace_is_empty(struct workspace *ws)
547{
548 return wl_list_empty(&ws->layer.surface_list);
549}
550
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200551static struct workspace *
552get_workspace(struct desktop_shell *shell, unsigned int index)
553{
554 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200555 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200556 pws += index;
557 return *pws;
558}
559
560static struct workspace *
561get_current_workspace(struct desktop_shell *shell)
562{
563 return get_workspace(shell, shell->workspaces.current);
564}
565
566static void
567activate_workspace(struct desktop_shell *shell, unsigned int index)
568{
569 struct workspace *ws;
570
571 ws = get_workspace(shell, index);
572 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
573
574 shell->workspaces.current = index;
575}
576
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200577static unsigned int
578get_output_height(struct weston_output *output)
579{
580 return abs(output->region.extents.y1 - output->region.extents.y2);
581}
582
583static void
584surface_translate(struct weston_surface *surface, double d)
585{
586 struct shell_surface *shsurf = get_shell_surface(surface);
587 struct weston_transform *transform;
588
589 transform = &shsurf->workspace_transform;
590 if (wl_list_empty(&transform->link))
591 wl_list_insert(surface->geometry.transformation_list.prev,
592 &shsurf->workspace_transform.link);
593
594 weston_matrix_init(&shsurf->workspace_transform.matrix);
595 weston_matrix_translate(&shsurf->workspace_transform.matrix,
596 0.0, d, 0.0);
597 surface->geometry.dirty = 1;
598}
599
600static void
601workspace_translate_out(struct workspace *ws, double fraction)
602{
603 struct weston_surface *surface;
604 unsigned int height;
605 double d;
606
607 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
608 height = get_output_height(surface->output);
609 d = height * fraction;
610
611 surface_translate(surface, d);
612 }
613}
614
615static void
616workspace_translate_in(struct workspace *ws, double fraction)
617{
618 struct weston_surface *surface;
619 unsigned int height;
620 double d;
621
622 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
623 height = get_output_height(surface->output);
624
625 if (fraction > 0)
626 d = -(height - height * fraction);
627 else
628 d = height + height * fraction;
629
630 surface_translate(surface, d);
631 }
632}
633
634static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200635broadcast_current_workspace_state(struct desktop_shell *shell)
636{
637 struct wl_resource *resource;
638
639 wl_list_for_each(resource, &shell->workspaces.client_list, link)
640 workspace_manager_send_state(resource,
641 shell->workspaces.current,
642 shell->workspaces.num);
643}
644
645static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200646reverse_workspace_change_animation(struct desktop_shell *shell,
647 unsigned int index,
648 struct workspace *from,
649 struct workspace *to)
650{
651 shell->workspaces.current = index;
652
653 shell->workspaces.anim_to = to;
654 shell->workspaces.anim_from = from;
655 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
656 shell->workspaces.anim_timestamp = 0;
657
Scott Moreau4272e632012-08-13 09:58:41 -0600658 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200659}
660
661static void
662workspace_deactivate_transforms(struct workspace *ws)
663{
664 struct weston_surface *surface;
665 struct shell_surface *shsurf;
666
667 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
668 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200669 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
670 wl_list_remove(&shsurf->workspace_transform.link);
671 wl_list_init(&shsurf->workspace_transform.link);
672 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200673 shsurf->surface->geometry.dirty = 1;
674 }
675}
676
677static void
678finish_workspace_change_animation(struct desktop_shell *shell,
679 struct workspace *from,
680 struct workspace *to)
681{
Scott Moreau4272e632012-08-13 09:58:41 -0600682 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200683
684 wl_list_remove(&shell->workspaces.animation.link);
685 workspace_deactivate_transforms(from);
686 workspace_deactivate_transforms(to);
687 shell->workspaces.anim_to = NULL;
688
689 wl_list_remove(&shell->workspaces.anim_from->layer.link);
690}
691
692static void
693animate_workspace_change_frame(struct weston_animation *animation,
694 struct weston_output *output, uint32_t msecs)
695{
696 struct desktop_shell *shell =
697 container_of(animation, struct desktop_shell,
698 workspaces.animation);
699 struct workspace *from = shell->workspaces.anim_from;
700 struct workspace *to = shell->workspaces.anim_to;
701 uint32_t t;
702 double x, y;
703
704 if (workspace_is_empty(from) && workspace_is_empty(to)) {
705 finish_workspace_change_animation(shell, from, to);
706 return;
707 }
708
709 if (shell->workspaces.anim_timestamp == 0) {
710 if (shell->workspaces.anim_current == 0.0)
711 shell->workspaces.anim_timestamp = msecs;
712 else
713 shell->workspaces.anim_timestamp =
714 msecs -
715 /* Invers of movement function 'y' below. */
716 (asin(1.0 - shell->workspaces.anim_current) *
717 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
718 M_2_PI);
719 }
720
721 t = msecs - shell->workspaces.anim_timestamp;
722
723 /*
724 * x = [0, π/2]
725 * y(x) = sin(x)
726 */
727 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
728 y = sin(x);
729
730 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600731 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200732
733 workspace_translate_out(from, shell->workspaces.anim_dir * y);
734 workspace_translate_in(to, shell->workspaces.anim_dir * y);
735 shell->workspaces.anim_current = y;
736
Scott Moreau4272e632012-08-13 09:58:41 -0600737 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200738 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200739 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200740 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200741}
742
743static void
744animate_workspace_change(struct desktop_shell *shell,
745 unsigned int index,
746 struct workspace *from,
747 struct workspace *to)
748{
749 struct weston_output *output;
750
751 int dir;
752
753 if (index > shell->workspaces.current)
754 dir = -1;
755 else
756 dir = 1;
757
758 shell->workspaces.current = index;
759
760 shell->workspaces.anim_dir = dir;
761 shell->workspaces.anim_from = from;
762 shell->workspaces.anim_to = to;
763 shell->workspaces.anim_current = 0.0;
764 shell->workspaces.anim_timestamp = 0;
765
766 output = container_of(shell->compositor->output_list.next,
767 struct weston_output, link);
768 wl_list_insert(&output->animation_list,
769 &shell->workspaces.animation.link);
770
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200771 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200772
773 workspace_translate_in(to, 0);
774
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400775 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200776
Scott Moreau4272e632012-08-13 09:58:41 -0600777 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200778}
779
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200780static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200781update_workspace(struct desktop_shell *shell, unsigned int index,
782 struct workspace *from, struct workspace *to)
783{
784 shell->workspaces.current = index;
785 wl_list_insert(&from->layer.link, &to->layer.link);
786 wl_list_remove(&from->layer.link);
787}
788
789static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200790change_workspace(struct desktop_shell *shell, unsigned int index)
791{
792 struct workspace *from;
793 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200794
795 if (index == shell->workspaces.current)
796 return;
797
798 /* Don't change workspace when there is any fullscreen surfaces. */
799 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
800 return;
801
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200802 from = get_current_workspace(shell);
803 to = get_workspace(shell, index);
804
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200805 if (shell->workspaces.anim_from == to &&
806 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200807 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200808 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200809 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200810 return;
811 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200812
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200813 if (shell->workspaces.anim_to != NULL)
814 finish_workspace_change_animation(shell,
815 shell->workspaces.anim_from,
816 shell->workspaces.anim_to);
817
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200818 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200819
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200820 if (workspace_is_empty(to) && workspace_is_empty(from))
821 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200822 else
823 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200824
825 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200826}
827
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200828static bool
829workspace_has_only(struct workspace *ws, struct weston_surface *surface)
830{
831 struct wl_list *list = &ws->layer.surface_list;
832 struct wl_list *e;
833
834 if (wl_list_empty(list))
835 return false;
836
837 e = list->next;
838
839 if (e->next != list)
840 return false;
841
842 return container_of(e, struct weston_surface, layer_link) == surface;
843}
844
845static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200846move_surface_to_workspace(struct desktop_shell *shell,
847 struct weston_surface *surface,
848 uint32_t workspace)
849{
850 struct workspace *from;
851 struct workspace *to;
852 struct weston_seat *seat;
853
854 if (workspace == shell->workspaces.current)
855 return;
856
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200857 if (workspace >= shell->workspaces.num)
858 workspace = shell->workspaces.num - 1;
859
Jonas Ådahle9d22502012-08-29 22:13:01 +0200860 from = get_current_workspace(shell);
861 to = get_workspace(shell, workspace);
862
863 wl_list_remove(&surface->layer_link);
864 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
865
866 drop_focus_state(shell, from, surface);
867 wl_list_for_each(seat, &shell->compositor->seat_list, link)
868 if (seat->has_keyboard &&
869 seat->keyboard.focus == &surface->surface)
870 wl_keyboard_set_focus(&seat->keyboard, NULL);
871
872 weston_surface_damage_below(surface);
873}
874
875static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200876take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200877 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200878 unsigned int index)
879{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200880 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200881 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200882 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200883 struct shell_surface *shsurf;
884 struct workspace *from;
885 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200886 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200887
888 if (surface == NULL ||
889 index == shell->workspaces.current)
890 return;
891
892 from = get_current_workspace(shell);
893 to = get_workspace(shell, index);
894
895 wl_list_remove(&surface->layer_link);
896 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
897
Jonas Ådahle9d22502012-08-29 22:13:01 +0200898 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200899 drop_focus_state(shell, from, surface);
900
901 if (shell->workspaces.anim_from == to &&
902 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200903 wl_list_remove(&to->layer.link);
904 wl_list_insert(from->layer.link.prev, &to->layer.link);
905
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200906 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907 broadcast_current_workspace_state(shell);
908
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200909 return;
910 }
911
912 if (shell->workspaces.anim_to != NULL)
913 finish_workspace_change_animation(shell,
914 shell->workspaces.anim_from,
915 shell->workspaces.anim_to);
916
917 if (workspace_is_empty(from) &&
918 workspace_has_only(to, surface))
919 update_workspace(shell, index, from, to);
920 else {
921 shsurf = get_shell_surface(surface);
922 if (wl_list_empty(&shsurf->workspace_transform.link))
923 wl_list_insert(&shell->workspaces.anim_sticky_list,
924 &shsurf->workspace_transform.link);
925
926 animate_workspace_change(shell, index, from, to);
927 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200928
929 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200930
931 state = ensure_focus_state(shell, seat);
932 if (state != NULL)
933 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200934}
935
936static void
937workspace_manager_move_surface(struct wl_client *client,
938 struct wl_resource *resource,
939 struct wl_resource *surface_resource,
940 uint32_t workspace)
941{
942 struct desktop_shell *shell = resource->data;
943 struct weston_surface *surface =
944 (struct weston_surface *) surface_resource;
945
946 move_surface_to_workspace(shell, surface, workspace);
947}
948
949static const struct workspace_manager_interface workspace_manager_implementation = {
950 workspace_manager_move_surface,
951};
952
953static void
954unbind_resource(struct wl_resource *resource)
955{
956 wl_list_remove(&resource->link);
957 free(resource);
958}
959
960static void
961bind_workspace_manager(struct wl_client *client,
962 void *data, uint32_t version, uint32_t id)
963{
964 struct desktop_shell *shell = data;
965 struct wl_resource *resource;
966
967 resource = wl_client_add_object(client, &workspace_manager_interface,
968 &workspace_manager_implementation,
969 id, shell);
970
971 if (resource == NULL) {
972 weston_log("couldn't add workspace manager object");
973 return;
974 }
975
976 resource->destroy = unbind_resource;
977 wl_list_insert(&shell->workspaces.client_list, &resource->link);
978
979 workspace_manager_send_state(resource,
980 shell->workspaces.current,
981 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200982}
983
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200984static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400985noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100986 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500987{
988 grab->focus = NULL;
989}
990
991static void
Scott Moreau447013d2012-02-18 05:05:29 -0700992move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100993 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500994{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500995 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100996 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300997 struct shell_surface *shsurf = move->base.shsurf;
998 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100999 int dx = wl_fixed_to_int(pointer->x + move->dx);
1000 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001001
1002 if (!shsurf)
1003 return;
1004
1005 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001006
Daniel Stone103db7f2012-05-08 17:17:55 +01001007 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001008 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001009
1010 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001011}
1012
1013static void
Scott Moreau447013d2012-02-18 05:05:29 -07001014move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001015 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001016{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001017 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1018 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001019 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001020 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001021
Daniel Stone4dbadb12012-05-30 16:31:51 +01001022 if (pointer->button_count == 0 &&
1023 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001024 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001025 free(grab);
1026 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001027}
1028
Scott Moreau447013d2012-02-18 05:05:29 -07001029static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001030 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001031 move_grab_motion,
1032 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001033};
1034
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001035static int
1036surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1037{
1038 struct weston_move_grab *move;
1039
1040 if (!shsurf)
1041 return -1;
1042
1043 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1044 return 0;
1045
1046 move = malloc(sizeof *move);
1047 if (!move)
1048 return -1;
1049
1050 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1051 ws->seat.pointer->grab_x;
1052 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1053 ws->seat.pointer->grab_y;
1054
1055 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1056 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1057
1058 return 0;
1059}
1060
1061static void
1062shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1063 struct wl_resource *seat_resource, uint32_t serial)
1064{
1065 struct weston_seat *ws = seat_resource->data;
1066 struct shell_surface *shsurf = resource->data;
1067
1068 if (ws->seat.pointer->button_count == 0 ||
1069 ws->seat.pointer->grab_serial != serial ||
1070 ws->seat.pointer->focus != &shsurf->surface->surface)
1071 return;
1072
1073 if (surface_move(shsurf, ws) < 0)
1074 wl_resource_post_no_memory(resource);
1075}
1076
1077struct weston_resize_grab {
1078 struct shell_grab base;
1079 uint32_t edges;
1080 int32_t width, height;
1081};
1082
1083static void
1084resize_grab_motion(struct wl_pointer_grab *grab,
1085 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1086{
1087 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1088 struct wl_pointer *pointer = grab->pointer;
1089 struct shell_surface *shsurf = resize->base.shsurf;
1090 int32_t width, height;
1091 wl_fixed_t from_x, from_y;
1092 wl_fixed_t to_x, to_y;
1093
1094 if (!shsurf)
1095 return;
1096
1097 weston_surface_from_global_fixed(shsurf->surface,
1098 pointer->grab_x, pointer->grab_y,
1099 &from_x, &from_y);
1100 weston_surface_from_global_fixed(shsurf->surface,
1101 pointer->x, pointer->y, &to_x, &to_y);
1102
1103 width = resize->width;
1104 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1105 width += wl_fixed_to_int(from_x - to_x);
1106 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1107 width += wl_fixed_to_int(to_x - from_x);
1108 }
1109
1110 height = resize->height;
1111 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1112 height += wl_fixed_to_int(from_y - to_y);
1113 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1114 height += wl_fixed_to_int(to_y - from_y);
1115 }
1116
1117 shsurf->client->send_configure(shsurf->surface,
1118 resize->edges, width, height);
1119}
1120
1121static void
1122send_configure(struct weston_surface *surface,
1123 uint32_t edges, int32_t width, int32_t height)
1124{
1125 struct shell_surface *shsurf = get_shell_surface(surface);
1126
1127 wl_shell_surface_send_configure(&shsurf->resource,
1128 edges, width, height);
1129}
1130
1131static const struct weston_shell_client shell_client = {
1132 send_configure
1133};
1134
1135static void
1136resize_grab_button(struct wl_pointer_grab *grab,
1137 uint32_t time, uint32_t button, uint32_t state_w)
1138{
1139 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1140 struct wl_pointer *pointer = grab->pointer;
1141 enum wl_pointer_button_state state = state_w;
1142
1143 if (pointer->button_count == 0 &&
1144 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1145 shell_grab_end(&resize->base);
1146 free(grab);
1147 }
1148}
1149
1150static const struct wl_pointer_grab_interface resize_grab_interface = {
1151 noop_grab_focus,
1152 resize_grab_motion,
1153 resize_grab_button,
1154};
1155
1156static int
1157surface_resize(struct shell_surface *shsurf,
1158 struct weston_seat *ws, uint32_t edges)
1159{
1160 struct weston_resize_grab *resize;
1161
1162 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1163 return 0;
1164
1165 if (edges == 0 || edges > 15 ||
1166 (edges & 3) == 3 || (edges & 12) == 12)
1167 return 0;
1168
1169 resize = malloc(sizeof *resize);
1170 if (!resize)
1171 return -1;
1172
1173 resize->edges = edges;
1174 resize->width = shsurf->surface->geometry.width;
1175 resize->height = shsurf->surface->geometry.height;
1176
1177 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1178 ws->seat.pointer, edges);
1179
1180 return 0;
1181}
1182
1183static void
1184shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1185 struct wl_resource *seat_resource, uint32_t serial,
1186 uint32_t edges)
1187{
1188 struct weston_seat *ws = seat_resource->data;
1189 struct shell_surface *shsurf = resource->data;
1190
1191 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1192 return;
1193
1194 if (ws->seat.pointer->button_count == 0 ||
1195 ws->seat.pointer->grab_serial != serial ||
1196 ws->seat.pointer->focus != &shsurf->surface->surface)
1197 return;
1198
1199 if (surface_resize(shsurf, ws, edges) < 0)
1200 wl_resource_post_no_memory(resource);
1201}
1202
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001203static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001204busy_cursor_grab_focus(struct wl_pointer_grab *base,
1205 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001206{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001207 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001208
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001209 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001210 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001211 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001212 }
1213}
1214
1215static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001216busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1217 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001218{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001219}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001220
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001221static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001222busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001223 uint32_t time, uint32_t button, uint32_t state)
1224{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001225 struct shell_grab *grab = (struct shell_grab *) base;
1226 struct shell_surface *shsurf;
1227 struct weston_surface *surface =
1228 (struct weston_surface *) grab->grab.pointer->current;
1229 struct weston_seat *seat =
1230 (struct weston_seat *) grab->grab.pointer->seat;
1231
1232 shsurf = get_shell_surface(surface);
1233 if (shsurf && button == BTN_LEFT && state) {
1234 activate(shsurf->shell, shsurf->surface, seat);
1235 surface_move(shsurf, seat);
1236 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001237}
1238
1239static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1240 busy_cursor_grab_focus,
1241 busy_cursor_grab_motion,
1242 busy_cursor_grab_button,
1243};
1244
1245static void
1246set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1247{
1248 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001249
1250 grab = malloc(sizeof *grab);
1251 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001252 return;
1253
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001254 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1255 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001256}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001257
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001258static void
1259end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1260{
1261 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1262
1263 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001264 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001265 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001266 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001267}
1268
Scott Moreau9521d5e2012-04-19 13:06:17 -06001269static void
1270ping_timer_destroy(struct shell_surface *shsurf)
1271{
1272 if (!shsurf || !shsurf->ping_timer)
1273 return;
1274
1275 if (shsurf->ping_timer->source)
1276 wl_event_source_remove(shsurf->ping_timer->source);
1277
1278 free(shsurf->ping_timer);
1279 shsurf->ping_timer = NULL;
1280}
1281
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001282static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001283ping_timeout_handler(void *data)
1284{
1285 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001286 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001287
Scott Moreau9521d5e2012-04-19 13:06:17 -06001288 /* Client is not responding */
1289 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001290
1291 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1292 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1293 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001294
1295 return 1;
1296}
1297
1298static void
1299ping_handler(struct weston_surface *surface, uint32_t serial)
1300{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001301 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001302 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001303 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001304
1305 if (!shsurf)
1306 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001307 if (!shsurf->resource.client)
1308 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001309
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001310 if (shsurf->surface == shsurf->shell->grab_surface)
1311 return;
1312
Scott Moreauff1db4a2012-04-17 19:06:18 -06001313 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001314 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001315 if (!shsurf->ping_timer)
1316 return;
1317
1318 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001319 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1320 shsurf->ping_timer->source =
1321 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1322 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1323
1324 wl_shell_surface_send_ping(&shsurf->resource, serial);
1325 }
1326}
1327
1328static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001329handle_pointer_focus(struct wl_listener *listener, void *data)
1330{
1331 struct wl_pointer *pointer = data;
1332 struct weston_surface *surface =
1333 (struct weston_surface *) pointer->focus;
1334 struct weston_compositor *compositor;
1335 struct shell_surface *shsurf;
1336 uint32_t serial;
1337
1338 if (!surface)
1339 return;
1340
1341 compositor = surface->compositor;
1342 shsurf = get_shell_surface(surface);
1343
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001344 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001345 set_busy_cursor(shsurf, pointer);
1346 } else {
1347 serial = wl_display_next_serial(compositor->wl_display);
1348 ping_handler(surface, serial);
1349 }
1350}
1351
1352static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001353create_pointer_focus_listener(struct weston_seat *seat)
1354{
1355 struct wl_listener *listener;
1356
1357 if (!seat->seat.pointer)
1358 return;
1359
1360 listener = malloc(sizeof *listener);
1361 listener->notify = handle_pointer_focus;
1362 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1363}
1364
1365static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001366shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1367 uint32_t serial)
1368{
1369 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001370 struct desktop_shell *shell = shsurf->shell;
1371 struct weston_seat *seat;
1372 struct weston_compositor *ec = shsurf->surface->compositor;
1373 struct wl_pointer *pointer;
1374 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001375
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001376 if (shsurf->ping_timer == NULL)
1377 /* Just ignore unsolicited pong. */
1378 return;
1379
Scott Moreauff1db4a2012-04-17 19:06:18 -06001380 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001381 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001382 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001383 if (was_unresponsive) {
1384 /* Received pong from previously unresponsive client */
1385 wl_list_for_each(seat, &ec->seat_list, link) {
1386 pointer = seat->seat.pointer;
1387 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001388 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001389 pointer->current ==
1390 &shsurf->surface->surface)
1391 end_busy_cursor(shsurf, pointer);
1392 }
1393 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001394 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001395 }
1396}
1397
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001398static void
1399shell_surface_set_title(struct wl_client *client,
1400 struct wl_resource *resource, const char *title)
1401{
1402 struct shell_surface *shsurf = resource->data;
1403
1404 free(shsurf->title);
1405 shsurf->title = strdup(title);
1406}
1407
1408static void
1409shell_surface_set_class(struct wl_client *client,
1410 struct wl_resource *resource, const char *class)
1411{
1412 struct shell_surface *shsurf = resource->data;
1413
1414 free(shsurf->class);
1415 shsurf->class = strdup(class);
1416}
1417
Juan Zhao96879df2012-02-07 08:45:41 +08001418static struct weston_output *
1419get_default_output(struct weston_compositor *compositor)
1420{
1421 return container_of(compositor->output_list.next,
1422 struct weston_output, link);
1423}
1424
Alex Wu4539b082012-03-01 12:57:46 +08001425static void
1426shell_unset_fullscreen(struct shell_surface *shsurf)
1427{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001428 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001429 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001430 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1431 shell_surface_is_top_fullscreen(shsurf)) {
1432 weston_output_switch_mode(shsurf->fullscreen_output,
1433 shsurf->fullscreen_output->origin);
1434 }
Alex Wu4539b082012-03-01 12:57:46 +08001435 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1436 shsurf->fullscreen.framerate = 0;
1437 wl_list_remove(&shsurf->fullscreen.transform.link);
1438 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001439 if (shsurf->fullscreen.black_surface)
1440 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001441 shsurf->fullscreen.black_surface = NULL;
1442 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001443 weston_surface_set_position(shsurf->surface,
1444 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001445 if (shsurf->saved_rotation_valid) {
1446 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1447 &shsurf->rotation.transform.link);
1448 shsurf->saved_rotation_valid = false;
1449 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001450
1451 ws = get_current_workspace(shsurf->shell);
1452 wl_list_remove(&shsurf->surface->layer_link);
1453 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001454}
1455
Pekka Paalanen98262232011-12-01 10:42:22 +02001456static int
1457reset_shell_surface_type(struct shell_surface *surface)
1458{
1459 switch (surface->type) {
1460 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001461 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001462 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001463 case SHELL_SURFACE_MAXIMIZED:
1464 surface->output = get_default_output(surface->surface->compositor);
1465 weston_surface_set_position(surface->surface,
1466 surface->saved_x,
1467 surface->saved_y);
1468 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001469 case SHELL_SURFACE_NONE:
1470 case SHELL_SURFACE_TOPLEVEL:
1471 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001472 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001473 break;
1474 }
1475
1476 surface->type = SHELL_SURFACE_NONE;
1477 return 0;
1478}
1479
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001480static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001481set_surface_type(struct shell_surface *shsurf)
1482{
1483 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001484 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001485
1486 reset_shell_surface_type(shsurf);
1487
1488 shsurf->type = shsurf->next_type;
1489 shsurf->next_type = SHELL_SURFACE_NONE;
1490
1491 switch (shsurf->type) {
1492 case SHELL_SURFACE_TOPLEVEL:
1493 break;
1494 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001495 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001496 pes->geometry.x + shsurf->transient.x,
1497 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001498 break;
1499
1500 case SHELL_SURFACE_MAXIMIZED:
1501 shsurf->saved_x = surface->geometry.x;
1502 shsurf->saved_y = surface->geometry.y;
1503 shsurf->saved_position_valid = true;
1504 break;
1505
1506 case SHELL_SURFACE_FULLSCREEN:
1507 shsurf->saved_x = surface->geometry.x;
1508 shsurf->saved_y = surface->geometry.y;
1509 shsurf->saved_position_valid = true;
1510
1511 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1512 wl_list_remove(&shsurf->rotation.transform.link);
1513 wl_list_init(&shsurf->rotation.transform.link);
1514 shsurf->surface->geometry.dirty = 1;
1515 shsurf->saved_rotation_valid = true;
1516 }
1517 break;
1518
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001519 default:
1520 break;
1521 }
1522}
1523
1524static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001525set_toplevel(struct shell_surface *shsurf)
1526{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001527 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001528}
1529
1530static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001531shell_surface_set_toplevel(struct wl_client *client,
1532 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001533{
Pekka Paalanen98262232011-12-01 10:42:22 +02001534 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001535
Tiago Vignattibc052c92012-04-19 16:18:18 +03001536 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001537}
1538
1539static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001540set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001541 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001542{
1543 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001544 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001545 shsurf->transient.x = x;
1546 shsurf->transient.y = y;
1547 shsurf->transient.flags = flags;
1548 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1549}
1550
1551static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001552shell_surface_set_transient(struct wl_client *client,
1553 struct wl_resource *resource,
1554 struct wl_resource *parent_resource,
1555 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001556{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001557 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001558 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001559
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001560 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001561}
1562
Tiago Vignattibe143262012-04-16 17:31:41 +03001563static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001564shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001565{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001566 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001567}
1568
1569static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001570get_output_panel_height(struct desktop_shell *shell,
1571 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001572{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001573 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001574 int panel_height = 0;
1575
1576 if (!output)
1577 return 0;
1578
Juan Zhao4ab94682012-07-09 22:24:09 -07001579 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001580 if (surface->output == output) {
1581 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001582 break;
1583 }
1584 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001585
Juan Zhao96879df2012-02-07 08:45:41 +08001586 return panel_height;
1587}
1588
1589static void
1590shell_surface_set_maximized(struct wl_client *client,
1591 struct wl_resource *resource,
1592 struct wl_resource *output_resource )
1593{
1594 struct shell_surface *shsurf = resource->data;
1595 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001596 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001597 uint32_t edges = 0, panel_height = 0;
1598
1599 /* get the default output, if the client set it as NULL
1600 check whether the ouput is available */
1601 if (output_resource)
1602 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001603 else if (es->output)
1604 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001605 else
1606 shsurf->output = get_default_output(es->compositor);
1607
Tiago Vignattibe143262012-04-16 17:31:41 +03001608 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001609 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001610 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001611
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001612 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001613 shsurf->output->width,
1614 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001615
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001616 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001617}
1618
Alex Wu21858432012-04-01 20:13:08 +08001619static void
1620black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1621
Alex Wu4539b082012-03-01 12:57:46 +08001622static struct weston_surface *
1623create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001624 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001625 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001626{
1627 struct weston_surface *surface = NULL;
1628
1629 surface = weston_surface_create(ec);
1630 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001631 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001632 return NULL;
1633 }
1634
Alex Wu21858432012-04-01 20:13:08 +08001635 surface->configure = black_surface_configure;
1636 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001637 weston_surface_configure(surface, x, y, w, h);
1638 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001639 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001640 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001641 pixman_region32_fini(&surface->input);
1642 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001643
Alex Wu4539b082012-03-01 12:57:46 +08001644 return surface;
1645}
1646
1647/* Create black surface and append it to the associated fullscreen surface.
1648 * Handle size dismatch and positioning according to the method. */
1649static void
1650shell_configure_fullscreen(struct shell_surface *shsurf)
1651{
1652 struct weston_output *output = shsurf->fullscreen_output;
1653 struct weston_surface *surface = shsurf->surface;
1654 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001655 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001656
Alex Wu4539b082012-03-01 12:57:46 +08001657 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001658 shsurf->fullscreen.black_surface =
1659 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001660 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001661 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001662 output->width,
1663 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001664
1665 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1666 wl_list_insert(&surface->layer_link,
1667 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001668 shsurf->fullscreen.black_surface->output = output;
1669
1670 switch (shsurf->fullscreen.type) {
1671 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001672 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001673 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001674 break;
1675 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1676 matrix = &shsurf->fullscreen.transform.matrix;
1677 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001678
Scott Moreau1bad5db2012-08-18 01:04:05 -06001679 output_aspect = (float) output->width /
1680 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001681 surface_aspect = (float) surface->geometry.width /
1682 (float) surface->geometry.height;
1683 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001684 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001685 (float) surface->geometry.width;
1686 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001687 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001688 (float) surface->geometry.height;
1689
Alex Wu4539b082012-03-01 12:57:46 +08001690 weston_matrix_scale(matrix, scale, scale, 1);
1691 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001692 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001693 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001694 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1695 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001696 weston_surface_set_position(surface, x, y);
1697
Alex Wu4539b082012-03-01 12:57:46 +08001698 break;
1699 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001700 if (shell_surface_is_top_fullscreen(shsurf)) {
1701 struct weston_mode mode = {0,
1702 surface->geometry.width,
1703 surface->geometry.height,
1704 shsurf->fullscreen.framerate};
1705
1706 if (weston_output_switch_mode(output, &mode) == 0) {
1707 weston_surface_configure(shsurf->fullscreen.black_surface,
1708 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001709 output->width,
1710 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001711 weston_surface_set_position(surface, output->x, output->y);
1712 break;
1713 }
1714 }
Alex Wu4539b082012-03-01 12:57:46 +08001715 break;
1716 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1717 break;
1718 default:
1719 break;
1720 }
1721}
1722
1723/* make the fullscreen and black surface at the top */
1724static void
1725shell_stack_fullscreen(struct shell_surface *shsurf)
1726{
Alex Wubd3354b2012-04-17 17:20:49 +08001727 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001728 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001729 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001730
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001731 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001732 wl_list_insert(&shell->fullscreen_layer.surface_list,
1733 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001734 weston_surface_damage(surface);
1735
1736 if (!shsurf->fullscreen.black_surface)
1737 shsurf->fullscreen.black_surface =
1738 create_black_surface(surface->compositor,
1739 surface,
1740 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001741 output->width,
1742 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001743
1744 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001745 wl_list_insert(&surface->layer_link,
1746 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001747 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001748}
1749
1750static void
1751shell_map_fullscreen(struct shell_surface *shsurf)
1752{
Alex Wu4539b082012-03-01 12:57:46 +08001753 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001754 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001755}
1756
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001757static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001758shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001759 struct wl_resource *resource,
1760 uint32_t method,
1761 uint32_t framerate,
1762 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001763{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001764 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001765 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001766
1767 if (output_resource)
1768 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001769 else if (es->output)
1770 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001771 else
1772 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001773
Alex Wu4539b082012-03-01 12:57:46 +08001774 shsurf->fullscreen_output = shsurf->output;
1775 shsurf->fullscreen.type = method;
1776 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001777 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001778
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001779 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001780 shsurf->output->width,
1781 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001782}
1783
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001784static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001785popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001786 struct wl_surface *surface,
1787 wl_fixed_t x,
1788 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001789{
Daniel Stone37816df2012-05-16 18:45:18 +01001790 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001791 struct shell_surface *priv =
1792 container_of(grab, struct shell_surface, popup.grab);
1793 struct wl_client *client = priv->surface->surface.resource.client;
1794
Pekka Paalanencb108432012-01-19 16:25:40 +02001795 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001796 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001797 grab->focus = surface;
1798 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001799 wl_pointer_set_focus(pointer, NULL,
1800 wl_fixed_from_int(0),
1801 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001802 grab->focus = NULL;
1803 }
1804}
1805
1806static void
Scott Moreau447013d2012-02-18 05:05:29 -07001807popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001808 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001809{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001810 struct wl_resource *resource;
1811
Daniel Stone37816df2012-05-16 18:45:18 +01001812 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001813 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001814 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001815}
1816
1817static void
Scott Moreau447013d2012-02-18 05:05:29 -07001818popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001819 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001820{
1821 struct wl_resource *resource;
1822 struct shell_surface *shsurf =
1823 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001824 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001825 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001826 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001827
Daniel Stone37816df2012-05-16 18:45:18 +01001828 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001829 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001830 display = wl_client_get_display(resource->client);
1831 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001832 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001833 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001834 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001835 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001836 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001837 }
1838
Daniel Stone4dbadb12012-05-30 16:31:51 +01001839 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001840 shsurf->popup.initial_up = 1;
1841}
1842
Scott Moreau447013d2012-02-18 05:05:29 -07001843static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001844 popup_grab_focus,
1845 popup_grab_motion,
1846 popup_grab_button,
1847};
1848
1849static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001850popup_grab_end(struct wl_pointer *pointer)
1851{
1852 struct wl_pointer_grab *grab = pointer->grab;
1853 struct shell_surface *shsurf =
1854 container_of(grab, struct shell_surface, popup.grab);
1855
1856 if (pointer->grab->interface == &popup_grab_interface) {
1857 wl_shell_surface_send_popup_done(&shsurf->resource);
1858 wl_pointer_end_grab(grab->pointer);
1859 shsurf->popup.grab.pointer = NULL;
1860 }
1861}
1862
1863static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001864shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001865{
Daniel Stone37816df2012-05-16 18:45:18 +01001866 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001867 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001868 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001869
1870 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001871 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001872
Pekka Paalanen938269a2012-02-07 14:19:01 +02001873 weston_surface_update_transform(parent);
1874 if (parent->transform.enabled) {
1875 shsurf->popup.parent_transform.matrix =
1876 parent->transform.matrix;
1877 } else {
1878 /* construct x, y translation matrix */
1879 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1880 shsurf->popup.parent_transform.matrix.d[12] =
1881 parent->geometry.x;
1882 shsurf->popup.parent_transform.matrix.d[13] =
1883 parent->geometry.y;
1884 }
1885 wl_list_insert(es->geometry.transformation_list.prev,
1886 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001887
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001888 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001889 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1890 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001891
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001892 /* We don't require the grab to still be active, but if another
1893 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001894 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1895 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001896 } else {
1897 wl_shell_surface_send_popup_done(&shsurf->resource);
1898 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001899}
1900
1901static void
1902shell_surface_set_popup(struct wl_client *client,
1903 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001904 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001905 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001906 struct wl_resource *parent_resource,
1907 int32_t x, int32_t y, uint32_t flags)
1908{
1909 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001910
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001911 shsurf->type = SHELL_SURFACE_POPUP;
1912 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001913 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001914 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001915 shsurf->popup.x = x;
1916 shsurf->popup.y = y;
1917}
1918
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001919static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001920 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001921 shell_surface_move,
1922 shell_surface_resize,
1923 shell_surface_set_toplevel,
1924 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001925 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001926 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001927 shell_surface_set_maximized,
1928 shell_surface_set_title,
1929 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001930};
1931
1932static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001933destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001934{
Daniel Stone37816df2012-05-16 18:45:18 +01001935 if (shsurf->popup.grab.pointer)
1936 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001937
Alex Wubd3354b2012-04-17 17:20:49 +08001938 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1939 shell_surface_is_top_fullscreen(shsurf)) {
1940 weston_output_switch_mode(shsurf->fullscreen_output,
1941 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001942 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001943
Alex Wuaa08e2d2012-03-05 11:01:40 +08001944 if (shsurf->fullscreen.black_surface)
1945 weston_surface_destroy(shsurf->fullscreen.black_surface);
1946
Alex Wubd3354b2012-04-17 17:20:49 +08001947 /* As destroy_resource() use wl_list_for_each_safe(),
1948 * we can always remove the listener.
1949 */
1950 wl_list_remove(&shsurf->surface_destroy_listener.link);
1951 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001952 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001953
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001954 wl_list_remove(&shsurf->link);
1955 free(shsurf);
1956}
1957
1958static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001959shell_destroy_shell_surface(struct wl_resource *resource)
1960{
1961 struct shell_surface *shsurf = resource->data;
1962
1963 destroy_shell_surface(shsurf);
1964}
1965
1966static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001967shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001968{
1969 struct shell_surface *shsurf = container_of(listener,
1970 struct shell_surface,
1971 surface_destroy_listener);
1972
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001973 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001974 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001975 } else {
1976 wl_signal_emit(&shsurf->resource.destroy_signal,
1977 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001978 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001979 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001980}
1981
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001982static void
1983shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1984
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001985static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001986get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001987{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001988 if (surface->configure == shell_surface_configure)
1989 return surface->private;
1990 else
1991 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001992}
1993
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001994static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001995create_shell_surface(void *shell, struct weston_surface *surface,
1996 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001997{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001998 struct shell_surface *shsurf;
1999
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002000 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002001 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002002 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002003 }
2004
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002005 shsurf = calloc(1, sizeof *shsurf);
2006 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002007 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002008 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002009 }
2010
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002011 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002012 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002013
Tiago Vignattibc052c92012-04-19 16:18:18 +03002014 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002015 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002016 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002017 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002018 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002019 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2020 shsurf->fullscreen.framerate = 0;
2021 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002022 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002023 wl_list_init(&shsurf->fullscreen.transform.link);
2024
Tiago Vignattibc052c92012-04-19 16:18:18 +03002025 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002026 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2027 wl_signal_add(&surface->surface.resource.destroy_signal,
2028 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002029
2030 /* init link so its safe to always remove it in destroy_shell_surface */
2031 wl_list_init(&shsurf->link);
2032
Pekka Paalanen460099f2012-01-20 16:48:25 +02002033 /* empty when not in use */
2034 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002035 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002036
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002037 wl_list_init(&shsurf->workspace_transform.link);
2038
Pekka Paalanen98262232011-12-01 10:42:22 +02002039 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002040 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002041
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002042 shsurf->client = client;
2043
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002044 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002045}
2046
2047static void
2048shell_get_shell_surface(struct wl_client *client,
2049 struct wl_resource *resource,
2050 uint32_t id,
2051 struct wl_resource *surface_resource)
2052{
2053 struct weston_surface *surface = surface_resource->data;
2054 struct desktop_shell *shell = resource->data;
2055 struct shell_surface *shsurf;
2056
2057 if (get_shell_surface(surface)) {
2058 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002059 WL_DISPLAY_ERROR_INVALID_OBJECT,
2060 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002061 return;
2062 }
2063
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002064 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002065 if (!shsurf) {
2066 wl_resource_post_error(surface_resource,
2067 WL_DISPLAY_ERROR_INVALID_OBJECT,
2068 "surface->configure already set");
2069 return;
2070 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002071
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002072 shsurf->resource.destroy = shell_destroy_shell_surface;
2073 shsurf->resource.object.id = id;
2074 shsurf->resource.object.interface = &wl_shell_surface_interface;
2075 shsurf->resource.object.implementation =
2076 (void (**)(void)) &shell_surface_implementation;
2077 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002078
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002079 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002080}
2081
2082static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002083 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002084};
2085
Kristian Høgsberg07937562011-04-12 17:25:42 -04002086static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002087handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002088{
2089 proc->pid = 0;
2090}
2091
2092static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002093launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002094{
2095 if (shell->screensaver.binding)
2096 return;
2097
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002098 if (!shell->screensaver.path)
2099 return;
2100
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002101 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002102 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002103 return;
2104 }
2105
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002106 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002107 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002108 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002109 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002110}
2111
2112static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002113terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002114{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002115 if (shell->screensaver.process.pid == 0)
2116 return;
2117
2118 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002119}
2120
2121static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002122configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002123{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002124 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002125
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002126 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2127 if (s->output == es->output && s != es) {
2128 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002129 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002130 }
2131 }
2132
2133 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002134 weston_surface_buffer_width(es),
2135 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002136
2137 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002138 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002139 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002140 }
2141}
2142
2143static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002144background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2145{
2146 struct desktop_shell *shell = es->private;
2147
2148 configure_static_surface(es, &shell->background_layer);
2149}
2150
2151static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002152desktop_shell_set_background(struct wl_client *client,
2153 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002154 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002155 struct wl_resource *surface_resource)
2156{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002157 struct desktop_shell *shell = resource->data;
2158 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002159
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002160 if (surface->configure) {
2161 wl_resource_post_error(surface_resource,
2162 WL_DISPLAY_ERROR_INVALID_OBJECT,
2163 "surface role already assigned");
2164 return;
2165 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002166
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002167 surface->configure = background_configure;
2168 surface->private = shell;
2169 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002170 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002171 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002172 surface->output->width,
2173 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002174}
2175
2176static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002177panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2178{
2179 struct desktop_shell *shell = es->private;
2180
2181 configure_static_surface(es, &shell->panel_layer);
2182}
2183
2184static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002185desktop_shell_set_panel(struct wl_client *client,
2186 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002187 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002188 struct wl_resource *surface_resource)
2189{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002190 struct desktop_shell *shell = resource->data;
2191 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002192
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002193 if (surface->configure) {
2194 wl_resource_post_error(surface_resource,
2195 WL_DISPLAY_ERROR_INVALID_OBJECT,
2196 "surface role already assigned");
2197 return;
2198 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002199
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002200 surface->configure = panel_configure;
2201 surface->private = shell;
2202 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002203 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002204 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002205 surface->output->width,
2206 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002207}
2208
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002209static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002210lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2211{
2212 struct desktop_shell *shell = surface->private;
2213
2214 center_on_output(surface, get_default_output(shell->compositor));
2215
2216 if (!weston_surface_is_mapped(surface)) {
2217 wl_list_insert(&shell->lock_layer.surface_list,
2218 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002219 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002220 weston_compositor_wake(shell->compositor);
2221 }
2222}
2223
2224static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002225handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002226{
Tiago Vignattibe143262012-04-16 17:31:41 +03002227 struct desktop_shell *shell =
2228 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002229
Martin Minarik6d118362012-06-07 18:01:59 +02002230 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002231 shell->lock_surface = NULL;
2232}
2233
2234static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002235desktop_shell_set_lock_surface(struct wl_client *client,
2236 struct wl_resource *resource,
2237 struct wl_resource *surface_resource)
2238{
Tiago Vignattibe143262012-04-16 17:31:41 +03002239 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002240 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002241
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002242 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002243
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002244 if (!shell->locked)
2245 return;
2246
Pekka Paalanen98262232011-12-01 10:42:22 +02002247 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002248
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002249 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2250 wl_signal_add(&surface_resource->destroy_signal,
2251 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002252
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002253 surface->configure = lock_surface_configure;
2254 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002255}
2256
2257static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002258resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002259{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002260 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002261 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002262
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002263 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2264 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002265
2266 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002267
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002268 wl_list_remove(&shell->lock_layer.link);
2269 wl_list_insert(&shell->compositor->cursor_layer.link,
2270 &shell->fullscreen_layer.link);
2271 wl_list_insert(&shell->fullscreen_layer.link,
2272 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002273 if (shell->showing_input_panels) {
2274 wl_list_insert(&shell->panel_layer.link,
2275 &shell->input_panel_layer.link);
2276 wl_list_insert(&shell->input_panel_layer.link,
2277 &ws->layer.link);
2278 } else {
2279 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2280 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002281
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002282 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002283
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002284 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002285 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002286 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002287 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002288}
2289
2290static void
2291desktop_shell_unlock(struct wl_client *client,
2292 struct wl_resource *resource)
2293{
Tiago Vignattibe143262012-04-16 17:31:41 +03002294 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002295
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002296 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002297
2298 if (shell->locked)
2299 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002300}
2301
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002302static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002303desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002304 struct wl_resource *resource,
2305 struct wl_resource *surface_resource)
2306{
2307 struct desktop_shell *shell = resource->data;
2308
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002309 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002310}
2311
Kristian Høgsberg75840622011-09-06 13:48:16 -04002312static const struct desktop_shell_interface desktop_shell_implementation = {
2313 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002314 desktop_shell_set_panel,
2315 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002316 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002317 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002318};
2319
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002320static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002321get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002322{
2323 struct shell_surface *shsurf;
2324
2325 shsurf = get_shell_surface(surface);
2326 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002327 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002328 return shsurf->type;
2329}
2330
Kristian Høgsberg75840622011-09-06 13:48:16 -04002331static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002332move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002333{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002334 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002335 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002336 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002337
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002338 if (surface == NULL)
2339 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002340
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002341 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002342 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002343 return;
2344
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002345 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002346}
2347
2348static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002349resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002350{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002351 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002352 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002353 uint32_t edges = 0;
2354 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002355 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002356
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002357 if (surface == NULL)
2358 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002359
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002360 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002361 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002362 return;
2363
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002364 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002365 wl_fixed_to_int(seat->pointer->grab_x),
2366 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002367 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002368
Pekka Paalanen60921e52012-01-25 15:55:43 +02002369 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002370 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002371 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002372 edges |= 0;
2373 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002374 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002375
Pekka Paalanen60921e52012-01-25 15:55:43 +02002376 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002377 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002378 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002379 edges |= 0;
2380 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002381 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002382
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002383 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002384}
2385
2386static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002387surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002388 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002389{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002390 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002391 struct shell_surface *shsurf;
2392 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002393 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002394
2395 if (surface == NULL)
2396 return;
2397
2398 shsurf = get_shell_surface(surface);
2399 if (!shsurf)
2400 return;
2401
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002402 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002403
Scott Moreau02709af2012-05-22 01:54:10 -06002404 if (surface->alpha > 1.0)
2405 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002406 if (surface->alpha < step)
2407 surface->alpha = step;
2408
2409 surface->geometry.dirty = 1;
2410 weston_surface_damage(surface);
2411}
2412
2413static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002414do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002415 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002416{
Daniel Stone37816df2012-05-16 18:45:18 +01002417 struct weston_seat *ws = (struct weston_seat *) seat;
2418 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002419 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002420 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002421
2422 wl_list_for_each(output, &compositor->output_list, link) {
2423 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002424 wl_fixed_to_double(seat->pointer->x),
2425 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002426 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002427 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002428 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002429 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002430 increment = -output->zoom.increment;
2431 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002432 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002433 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002434 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002435 else
2436 increment = 0;
2437
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002438 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002439
Scott Moreaue6603982012-06-11 13:07:51 -06002440 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002441 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002442 else if (output->zoom.level > output->zoom.max_level)
2443 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002444 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002445 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002446 output->disable_planes++;
2447 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002448
Scott Moreaue6603982012-06-11 13:07:51 -06002449 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002450
Scott Moreau8dacaab2012-06-17 18:10:58 -06002451 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002452 }
2453 }
2454}
2455
Scott Moreauccbf29d2012-02-22 14:21:41 -07002456static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002457zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002458 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002459{
2460 do_zoom(seat, time, 0, axis, value);
2461}
2462
2463static void
2464zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2465 void *data)
2466{
2467 do_zoom(seat, time, key, 0, 0);
2468}
2469
2470static void
2471terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2472 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002473{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002474 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002475
Daniel Stone325fc2d2012-05-30 16:31:58 +01002476 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002477}
2478
2479static void
Scott Moreau447013d2012-02-18 05:05:29 -07002480rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002481 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002482{
2483 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002484 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002485 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002486 struct shell_surface *shsurf = rotate->base.shsurf;
2487 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002488 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002489
2490 if (!shsurf)
2491 return;
2492
2493 surface = shsurf->surface;
2494
2495 cx = 0.5f * surface->geometry.width;
2496 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002497
Daniel Stone37816df2012-05-16 18:45:18 +01002498 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2499 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002500 r = sqrtf(dx * dx + dy * dy);
2501
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002502 wl_list_remove(&shsurf->rotation.transform.link);
2503 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002504
2505 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002506 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002507 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002508
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002509 weston_matrix_init(&rotate->rotation);
2510 rotate->rotation.d[0] = dx / r;
2511 rotate->rotation.d[4] = -dy / r;
2512 rotate->rotation.d[1] = -rotate->rotation.d[4];
2513 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002514
2515 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002516 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002517 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002518 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002519 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002520
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002521 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002522 &shsurf->surface->geometry.transformation_list,
2523 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002524 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002525 wl_list_init(&shsurf->rotation.transform.link);
2526 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002527 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002528 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002529
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002530 /* We need to adjust the position of the surface
2531 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002532 cposx = surface->geometry.x + cx;
2533 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002534 dposx = rotate->center.x - cposx;
2535 dposy = rotate->center.y - cposy;
2536 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002537 weston_surface_set_position(surface,
2538 surface->geometry.x + dposx,
2539 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002540 }
2541
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002542 /* Repaint implies weston_surface_update_transform(), which
2543 * lazily applies the damage due to rotation update.
2544 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002545 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002546}
2547
2548static void
Scott Moreau447013d2012-02-18 05:05:29 -07002549rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002550 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002551{
2552 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002553 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002554 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002555 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002556 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002557
Daniel Stone4dbadb12012-05-30 16:31:51 +01002558 if (pointer->button_count == 0 &&
2559 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002560 if (shsurf)
2561 weston_matrix_multiply(&shsurf->rotation.rotation,
2562 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002563 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002564 free(rotate);
2565 }
2566}
2567
Scott Moreau447013d2012-02-18 05:05:29 -07002568static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002569 noop_grab_focus,
2570 rotate_grab_motion,
2571 rotate_grab_button,
2572};
2573
2574static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002575rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002576 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002577{
2578 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002579 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002580 struct shell_surface *surface;
2581 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002582 float dx, dy;
2583 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002584
2585 if (base_surface == NULL)
2586 return;
2587
2588 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002589 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002590 return;
2591
Pekka Paalanen460099f2012-01-20 16:48:25 +02002592 rotate = malloc(sizeof *rotate);
2593 if (!rotate)
2594 return;
2595
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002596 weston_surface_to_global_float(surface->surface,
2597 surface->surface->geometry.width / 2,
2598 surface->surface->geometry.height / 2,
2599 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002600
Daniel Stone37816df2012-05-16 18:45:18 +01002601 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2602 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002603 r = sqrtf(dx * dx + dy * dy);
2604 if (r > 20.0f) {
2605 struct weston_matrix inverse;
2606
2607 weston_matrix_init(&inverse);
2608 inverse.d[0] = dx / r;
2609 inverse.d[4] = dy / r;
2610 inverse.d[1] = -inverse.d[4];
2611 inverse.d[5] = inverse.d[0];
2612 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002613
2614 weston_matrix_init(&rotate->rotation);
2615 rotate->rotation.d[0] = dx / r;
2616 rotate->rotation.d[4] = -dy / r;
2617 rotate->rotation.d[1] = -rotate->rotation.d[4];
2618 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002619 } else {
2620 weston_matrix_init(&surface->rotation.rotation);
2621 weston_matrix_init(&rotate->rotation);
2622 }
2623
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002624 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2625 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002626}
2627
2628static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002629lower_fullscreen_layer(struct desktop_shell *shell)
2630{
2631 struct workspace *ws;
2632 struct weston_surface *surface, *prev;
2633
2634 ws = get_current_workspace(shell);
2635 wl_list_for_each_reverse_safe(surface, prev,
2636 &shell->fullscreen_layer.surface_list,
2637 layer_link)
2638 weston_surface_restack(surface, &ws->layer.surface_list);
2639}
2640
2641static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002642activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002643 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002644{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002645 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002646 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002647
Daniel Stone37816df2012-05-16 18:45:18 +01002648 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002649
Jonas Ådahl8538b222012-08-29 22:13:03 +02002650 state = ensure_focus_state(shell, seat);
2651 if (state == NULL)
2652 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002653
2654 state->keyboard_focus = es;
2655 wl_list_remove(&state->surface_destroy_listener.link);
2656 wl_signal_add(&es->surface.resource.destroy_signal,
2657 &state->surface_destroy_listener);
2658
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002659 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002660 case SHELL_SURFACE_FULLSCREEN:
2661 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002662 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002663 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002664 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002665 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002666 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002667 lower_fullscreen_layer(shell);
2668 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002669 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002670 }
2671}
2672
Alex Wu21858432012-04-01 20:13:08 +08002673/* no-op func for checking black surface */
2674static void
2675black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2676{
2677}
2678
2679static bool
2680is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2681{
2682 if (es->configure == black_surface_configure) {
2683 if (fs_surface)
2684 *fs_surface = (struct weston_surface *)es->private;
2685 return true;
2686 }
2687 return false;
2688}
2689
Kristian Høgsberg75840622011-09-06 13:48:16 -04002690static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002691click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002692 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002693{
Daniel Stone37816df2012-05-16 18:45:18 +01002694 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002695 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002696 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002697 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002698
Daniel Stone37816df2012-05-16 18:45:18 +01002699 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002700 if (!focus)
2701 return;
2702
Alex Wu21858432012-04-01 20:13:08 +08002703 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002704 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002705
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002706 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2707 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002708
Daniel Stone325fc2d2012-05-30 16:31:58 +01002709 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002710 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002711}
2712
2713static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002714lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002715{
Tiago Vignattibe143262012-04-16 17:31:41 +03002716 struct desktop_shell *shell =
2717 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002718 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002719 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002720
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002721 if (shell->locked) {
2722 wl_list_for_each(output, &shell->compositor->output_list, link)
2723 /* TODO: find a way to jump to other DPMS levels */
2724 if (output->set_dpms)
2725 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002726 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002727 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002728
2729 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002730
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002731 /* Hide all surfaces by removing the fullscreen, panel and
2732 * toplevel layers. This way nothing else can show or receive
2733 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002734
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002735 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002736 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002737 if (shell->showing_input_panels)
2738 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002739 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002740 wl_list_insert(&shell->compositor->cursor_layer.link,
2741 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002742
Pekka Paalanen77346a62011-11-30 16:26:35 +02002743 launch_screensaver(shell);
2744
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002745 /* TODO: disable bindings that should not work while locked. */
2746
2747 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002748}
2749
2750static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002751unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002752{
Tiago Vignattibe143262012-04-16 17:31:41 +03002753 struct desktop_shell *shell =
2754 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002755
Pekka Paalanend81c2162011-11-16 13:47:34 +02002756 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002757 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002758 return;
2759 }
2760
2761 /* If desktop-shell client has gone away, unlock immediately. */
2762 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002763 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002764 return;
2765 }
2766
2767 if (shell->prepare_event_sent)
2768 return;
2769
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002770 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002771 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002772}
2773
2774static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002775show_input_panels(struct wl_listener *listener, void *data)
2776{
2777 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002778 container_of(listener, struct desktop_shell,
2779 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002780 struct input_panel_surface *surface, *next;
2781 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002782
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002783 shell->showing_input_panels = true;
2784
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002785 if (!shell->locked)
2786 wl_list_insert(&shell->panel_layer.link,
2787 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002788
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002789 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002790 &shell->input_panel.surfaces, link) {
2791 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002792 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002793 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002794 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002795 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002796 weston_surface_damage(ws);
2797 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002798 }
2799}
2800
2801static void
2802hide_input_panels(struct wl_listener *listener, void *data)
2803{
2804 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002805 container_of(listener, struct desktop_shell,
2806 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002807 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002808
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002809 shell->showing_input_panels = false;
2810
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002811 if (!shell->locked)
2812 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002813
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002814 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002815 &shell->input_panel_layer.surface_list, layer_link)
2816 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002817}
2818
2819static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002820center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002821{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002822 int32_t width = weston_surface_buffer_width(surface);
2823 int32_t height = weston_surface_buffer_height(surface);
2824 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002825
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002826 x = output->x + (output->width - width) / 2;
2827 y = output->y + (output->height - height) / 2;
2828
2829 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002830}
2831
2832static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002833weston_surface_set_initial_position (struct weston_surface *surface,
2834 struct desktop_shell *shell)
2835{
2836 struct weston_compositor *compositor = shell->compositor;
2837 int ix = 0, iy = 0;
2838 int range_x, range_y;
2839 int dx, dy, x, y, panel_height;
2840 struct weston_output *output, *target_output = NULL;
2841 struct weston_seat *seat;
2842
2843 /* As a heuristic place the new window on the same output as the
2844 * pointer. Falling back to the output containing 0, 0.
2845 *
2846 * TODO: Do something clever for touch too?
2847 */
2848 wl_list_for_each(seat, &compositor->seat_list, link) {
2849 if (seat->has_pointer) {
2850 ix = wl_fixed_to_int(seat->pointer.x);
2851 iy = wl_fixed_to_int(seat->pointer.y);
2852 break;
2853 }
2854 }
2855
2856 wl_list_for_each(output, &compositor->output_list, link) {
2857 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2858 target_output = output;
2859 break;
2860 }
2861 }
2862
2863 if (!target_output) {
2864 weston_surface_set_position(surface, 10 + random() % 400,
2865 10 + random() % 400);
2866 return;
2867 }
2868
2869 /* Valid range within output where the surface will still be onscreen.
2870 * If this is negative it means that the surface is bigger than
2871 * output.
2872 */
2873 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002874 range_x = target_output->width - surface->geometry.width;
2875 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002876 surface->geometry.height;
2877
Rob Bradford4cb88c72012-08-13 15:18:44 +01002878 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002879 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002880 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002881 dx = 0;
2882
2883 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002884 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002885 else
2886 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002887
2888 x = target_output->x + dx;
2889 y = target_output->y + dy;
2890
2891 weston_surface_set_position (surface, x, y);
2892}
2893
2894static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002895map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002896 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002898 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002899 struct shell_surface *shsurf = get_shell_surface(surface);
2900 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002901 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002902 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002903 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002904 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002905
Pekka Paalanen60921e52012-01-25 15:55:43 +02002906 surface->geometry.width = width;
2907 surface->geometry.height = height;
2908 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002909
2910 /* initial positioning, see also configure() */
2911 switch (surface_type) {
2912 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002913 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002914 break;
Alex Wu4539b082012-03-01 12:57:46 +08002915 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002916 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002917 shell_map_fullscreen(shsurf);
2918 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002919 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002920 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002921 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002922 weston_surface_set_position(surface, shsurf->output->x,
2923 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002924 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002925 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002926 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002927 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002928 case SHELL_SURFACE_NONE:
2929 weston_surface_set_position(surface,
2930 surface->geometry.x + sx,
2931 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002932 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002933 default:
2934 ;
2935 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002936
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002937 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002938 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002939 case SHELL_SURFACE_POPUP:
2940 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002941 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002942 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2943 break;
Alex Wu4539b082012-03-01 12:57:46 +08002944 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002945 case SHELL_SURFACE_NONE:
2946 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002947 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002948 ws = get_current_workspace(shell);
2949 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002950 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002951 }
2952
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002953 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002954 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002955 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2956 surface->output = shsurf->output;
2957 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002958
Juan Zhao7bb92f02011-12-15 11:31:51 -05002959 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002960 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002961 if (shsurf->transient.flags ==
2962 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2963 break;
2964 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002965 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002966 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002967 if (!shell->locked) {
2968 wl_list_for_each(seat, &compositor->seat_list, link)
2969 activate(shell, surface, seat);
2970 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002971 break;
2972 default:
2973 break;
2974 }
2975
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002976 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002977 {
2978 switch (shell->win_animation_type) {
2979 case ANIMATION_FADE:
2980 weston_fade_run(surface, NULL, NULL);
2981 break;
2982 case ANIMATION_ZOOM:
2983 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2984 break;
2985 default:
2986 break;
2987 }
2988 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002989}
2990
2991static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002992configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002993 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002994{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002995 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2996 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002997
Pekka Paalanen77346a62011-11-30 16:26:35 +02002998 shsurf = get_shell_surface(surface);
2999 if (shsurf)
3000 surface_type = shsurf->type;
3001
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003002 surface->geometry.x = x;
3003 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003004 surface->geometry.width = width;
3005 surface->geometry.height = height;
3006 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003007
3008 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003009 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003010 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003011 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003012 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003013 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003014 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003015 surface->geometry.x = surface->output->x;
3016 surface->geometry.y = surface->output->y +
3017 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003018 break;
Alex Wu4539b082012-03-01 12:57:46 +08003019 case SHELL_SURFACE_TOPLEVEL:
3020 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003021 default:
3022 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003023 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003024
Alex Wu4539b082012-03-01 12:57:46 +08003025 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003026 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003027 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003028
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003029 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003030 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003031 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003032}
3033
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003034static void
3035shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3036{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003037 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003038 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003039 int32_t width = weston_surface_buffer_width(es);
3040 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003041 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003042
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003043 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003044 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003045 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003046 type_changed = 1;
3047 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003048
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003049 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003050 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003051 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003052 es->geometry.width != width ||
3053 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003054 float from_x, from_y;
3055 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003056
3057 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3058 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3059 configure(shell, es,
3060 es->geometry.x + to_x - from_x,
3061 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003062 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003063 }
3064}
3065
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003066static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003067
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003068static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003069desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003070{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003071 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003072 struct desktop_shell *shell =
3073 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003074
3075 shell->child.process.pid = 0;
3076 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003077
3078 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3079 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003080 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003081 shell->child.deathstamp = time;
3082 shell->child.deathcount = 0;
3083 }
3084
3085 shell->child.deathcount++;
3086 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003087 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003088 return;
3089 }
3090
Martin Minarik6d118362012-06-07 18:01:59 +02003091 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003092 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003093}
3094
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003095static void
3096launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003097{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003098 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003099 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003100
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003101 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003102 &shell->child.process,
3103 shell_exe,
3104 desktop_shell_sigchld);
3105
3106 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003107 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003108}
3109
3110static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003111bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3112{
Tiago Vignattibe143262012-04-16 17:31:41 +03003113 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003114
3115 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003117}
3118
Kristian Høgsberg75840622011-09-06 13:48:16 -04003119static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003120unbind_desktop_shell(struct wl_resource *resource)
3121{
Tiago Vignattibe143262012-04-16 17:31:41 +03003122 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003123
3124 if (shell->locked)
3125 resume_desktop(shell);
3126
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003127 shell->child.desktop_shell = NULL;
3128 shell->prepare_event_sent = false;
3129 free(resource);
3130}
3131
3132static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003133bind_desktop_shell(struct wl_client *client,
3134 void *data, uint32_t version, uint32_t id)
3135{
Tiago Vignattibe143262012-04-16 17:31:41 +03003136 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003137 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003138
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003139 resource = wl_client_add_object(client, &desktop_shell_interface,
3140 &desktop_shell_implementation,
3141 id, shell);
3142
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003143 if (client == shell->child.client) {
3144 resource->destroy = unbind_desktop_shell;
3145 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003146 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003147 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003148
3149 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3150 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003151 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003152}
3153
Pekka Paalanen6e168112011-11-24 11:34:05 +02003154static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003155screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3156{
3157 struct desktop_shell *shell = surface->private;
3158
3159 if (!shell->locked)
3160 return;
3161
3162 center_on_output(surface, surface->output);
3163
3164 if (wl_list_empty(&surface->layer_link)) {
3165 wl_list_insert(shell->lock_layer.surface_list.prev,
3166 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003167 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003168 shell->compositor->idle_time = shell->screensaver.duration;
3169 weston_compositor_wake(shell->compositor);
3170 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3171 }
3172}
3173
3174static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003175screensaver_set_surface(struct wl_client *client,
3176 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003177 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003178 struct wl_resource *output_resource)
3179{
Tiago Vignattibe143262012-04-16 17:31:41 +03003180 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003181 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003182 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003183
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003184 surface->configure = screensaver_configure;
3185 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003186 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003187}
3188
3189static const struct screensaver_interface screensaver_implementation = {
3190 screensaver_set_surface
3191};
3192
3193static void
3194unbind_screensaver(struct wl_resource *resource)
3195{
Tiago Vignattibe143262012-04-16 17:31:41 +03003196 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003197
Pekka Paalanen77346a62011-11-30 16:26:35 +02003198 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003199 free(resource);
3200}
3201
3202static void
3203bind_screensaver(struct wl_client *client,
3204 void *data, uint32_t version, uint32_t id)
3205{
Tiago Vignattibe143262012-04-16 17:31:41 +03003206 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003207 struct wl_resource *resource;
3208
3209 resource = wl_client_add_object(client, &screensaver_interface,
3210 &screensaver_implementation,
3211 id, shell);
3212
Pekka Paalanen77346a62011-11-30 16:26:35 +02003213 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003214 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003215 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003216 return;
3217 }
3218
3219 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3220 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003221 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003222}
3223
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003224static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003225input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3226{
3227 struct weston_mode *mode = surface->output->current;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003228 int32_t width = weston_surface_buffer_width(surface);
3229 int32_t height = weston_surface_buffer_height(surface);
3230 float x = (mode->width - width) / 2;
3231 float y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003232
3233 /* Don't map the input panel here, wait for
3234 * show_input_panels signal. */
3235
3236 weston_surface_configure(surface,
3237 surface->output->x + x,
3238 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003239 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003240}
3241
3242static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003243destroy_input_panel_surface(struct wl_listener *listener,
3244 void *data)
3245{
3246 struct input_panel_surface *input_panel_surface =
3247 container_of(listener, struct input_panel_surface, listener);
3248
3249 wl_list_remove(&listener->link);
3250 wl_list_remove(&input_panel_surface->link);
3251
3252 free(input_panel_surface);
3253}
3254
3255static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003256input_panel_set_surface(struct wl_client *client,
3257 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003258 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003259 struct wl_resource *output_resource)
3260{
3261 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003262 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003263 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003264 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003265
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003266 surface->configure = input_panel_configure;
3267 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003268 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003269
3270 input_panel_surface = malloc(sizeof *input_panel_surface);
3271 if (!input_panel_surface) {
3272 wl_resource_post_no_memory(resource);
3273 return;
3274 }
3275
3276 input_panel_surface->surface = surface;
3277 input_panel_surface->listener.notify = destroy_input_panel_surface;
3278
3279 wl_signal_add(&surface_resource->destroy_signal,
3280 &input_panel_surface->listener);
3281
3282 wl_list_insert(&shell->input_panel.surfaces,
3283 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003284}
3285
3286static const struct input_panel_interface input_panel_implementation = {
3287 input_panel_set_surface
3288};
3289
3290static void
3291unbind_input_panel(struct wl_resource *resource)
3292{
3293 struct desktop_shell *shell = resource->data;
3294
3295 shell->input_panel.binding = NULL;
3296 free(resource);
3297}
3298
3299static void
3300bind_input_panel(struct wl_client *client,
3301 void *data, uint32_t version, uint32_t id)
3302{
3303 struct desktop_shell *shell = data;
3304 struct wl_resource *resource;
3305
3306 resource = wl_client_add_object(client, &input_panel_interface,
3307 &input_panel_implementation,
3308 id, shell);
3309
3310 if (shell->input_panel.binding == NULL) {
3311 resource->destroy = unbind_input_panel;
3312 shell->input_panel.binding = resource;
3313 return;
3314 }
3315
3316 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3317 "interface object already bound");
3318 wl_resource_destroy(resource);
3319}
3320
Kristian Høgsberg07045392012-02-19 18:52:44 -05003321struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003322 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003323 struct weston_surface *current;
3324 struct wl_listener listener;
3325 struct wl_keyboard_grab grab;
3326};
3327
3328static void
3329switcher_next(struct switcher *switcher)
3330{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003331 struct weston_surface *surface;
3332 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003333 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003334 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003335
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003336 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003337 switch (get_shell_surface_type(surface)) {
3338 case SHELL_SURFACE_TOPLEVEL:
3339 case SHELL_SURFACE_FULLSCREEN:
3340 case SHELL_SURFACE_MAXIMIZED:
3341 if (first == NULL)
3342 first = surface;
3343 if (prev == switcher->current)
3344 next = surface;
3345 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003346 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003347 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003348 weston_surface_damage(surface);
3349 break;
3350 default:
3351 break;
3352 }
Alex Wu1659daa2012-04-01 20:13:09 +08003353
3354 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003355 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003356 surface->geometry.dirty = 1;
3357 weston_surface_damage(surface);
3358 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003359 }
3360
3361 if (next == NULL)
3362 next = first;
3363
Alex Wu07b26062012-03-12 16:06:01 +08003364 if (next == NULL)
3365 return;
3366
Kristian Høgsberg07045392012-02-19 18:52:44 -05003367 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003368 wl_signal_add(&next->surface.resource.destroy_signal,
3369 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003370
3371 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003372 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003373
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003374 shsurf = get_shell_surface(switcher->current);
3375 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003376 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003377}
3378
3379static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003380switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003381{
3382 struct switcher *switcher =
3383 container_of(listener, struct switcher, listener);
3384
3385 switcher_next(switcher);
3386}
3387
3388static void
Daniel Stone351eb612012-05-31 15:27:47 -04003389switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003390{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003391 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003392 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003393 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003394
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003395 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003396 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003397 weston_surface_damage(surface);
3398 }
3399
Alex Wu07b26062012-03-12 16:06:01 +08003400 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003401 activate(switcher->shell, switcher->current,
3402 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003403 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003404 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003405 free(switcher);
3406}
3407
3408static void
3409switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003410 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003411{
3412 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003413 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003414
Daniel Stonec9785ea2012-05-30 16:31:52 +01003415 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003416 switcher_next(switcher);
3417}
3418
3419static void
3420switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3421 uint32_t mods_depressed, uint32_t mods_latched,
3422 uint32_t mods_locked, uint32_t group)
3423{
3424 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003425 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003426
Daniel Stone351eb612012-05-31 15:27:47 -04003427 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3428 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003429}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003430
3431static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003432 switcher_key,
3433 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003434};
3435
3436static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003437switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3438 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003439{
Tiago Vignattibe143262012-04-16 17:31:41 +03003440 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003441 struct switcher *switcher;
3442
3443 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003444 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003445 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003446 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003447 wl_list_init(&switcher->listener.link);
3448
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003449 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003450 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003451 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3452 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003453 switcher_next(switcher);
3454}
3455
Pekka Paalanen3c647232011-12-22 13:43:43 +02003456static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003457backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3458 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003459{
3460 struct weston_compositor *compositor = data;
3461 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003462 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003463
3464 /* TODO: we're limiting to simple use cases, where we assume just
3465 * control on the primary display. We'd have to extend later if we
3466 * ever get support for setting backlights on random desktop LCD
3467 * panels though */
3468 output = get_default_output(compositor);
3469 if (!output)
3470 return;
3471
3472 if (!output->set_backlight)
3473 return;
3474
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003475 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3476 backlight_new = output->backlight_current - 25;
3477 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3478 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003479
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003480 if (backlight_new < 5)
3481 backlight_new = 5;
3482 if (backlight_new > 255)
3483 backlight_new = 255;
3484
3485 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003486 output->set_backlight(output, output->backlight_current);
3487}
3488
3489static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003490fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3491 void *data)
3492{
3493 struct desktop_shell *shell = data;
3494 struct weston_compositor *compositor = shell->compositor;
3495 compositor->fan_debug = !compositor->fan_debug;
3496 weston_compositor_damage_all(compositor);
3497}
3498
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003499struct debug_binding_grab {
3500 struct wl_keyboard_grab grab;
3501 struct weston_seat *seat;
3502 uint32_t key[2];
3503 int key_released[2];
3504};
3505
3506static void
3507debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3508 uint32_t key, uint32_t state)
3509{
3510 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3511 struct wl_resource *resource;
3512 struct wl_display *display;
3513 uint32_t serial;
3514 int send = 0, terminate = 0;
3515 int check_binding = 1;
3516 int i;
3517
3518 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3519 /* Do not run bindings on key releases */
3520 check_binding = 0;
3521
3522 for (i = 0; i < 2; i++)
3523 if (key == db->key[i])
3524 db->key_released[i] = 1;
3525
3526 if (db->key_released[0] && db->key_released[1]) {
3527 /* All key releases been swalled so end the grab */
3528 terminate = 1;
3529 } else if (key != db->key[0] && key != db->key[1]) {
3530 /* Should not swallow release of other keys */
3531 send = 1;
3532 }
3533 } else if (key == db->key[0] && !db->key_released[0]) {
3534 /* Do not check bindings for the first press of the binding
3535 * key. This allows it to be used as a debug shortcut.
3536 * We still need to swallow this event. */
3537 check_binding = 0;
3538 } else if (db->key[1]) {
3539 /* If we already ran a binding don't process another one since
3540 * we can't keep track of all the binding keys that were
3541 * pressed in order to swallow the release events. */
3542 send = 1;
3543 check_binding = 0;
3544 }
3545
3546 if (check_binding) {
3547 struct weston_compositor *ec = db->seat->compositor;
3548
3549 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3550 key, state)) {
3551 /* We ran a binding so swallow the press and keep the
3552 * grab to swallow the released too. */
3553 send = 0;
3554 terminate = 0;
3555 db->key[1] = key;
3556 } else {
3557 /* Terminate the grab since the key pressed is not a
3558 * debug binding key. */
3559 send = 1;
3560 terminate = 1;
3561 }
3562 }
3563
3564 if (send) {
3565 resource = grab->keyboard->focus_resource;
3566
3567 if (resource) {
3568 display = wl_client_get_display(resource->client);
3569 serial = wl_display_next_serial(display);
3570 wl_keyboard_send_key(resource, serial, time, key, state);
3571 }
3572 }
3573
3574 if (terminate) {
3575 wl_keyboard_end_grab(grab->keyboard);
3576 free(db);
3577 }
3578}
3579
3580static void
3581debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3582 uint32_t mods_depressed, uint32_t mods_latched,
3583 uint32_t mods_locked, uint32_t group)
3584{
3585 struct wl_resource *resource;
3586
3587 resource = grab->keyboard->focus_resource;
3588 if (!resource)
3589 return;
3590
3591 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3592 mods_latched, mods_locked, group);
3593}
3594
3595struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3596 debug_binding_key,
3597 debug_binding_modifiers
3598};
3599
3600static void
3601debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3602{
3603 struct debug_binding_grab *grab;
3604
3605 grab = calloc(1, sizeof *grab);
3606 if (!grab)
3607 return;
3608
3609 grab->seat = (struct weston_seat *) seat;
3610 grab->key[0] = key;
3611 grab->grab.interface = &debug_binding_keyboard_grab;
3612 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3613}
3614
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003615static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003616force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3617 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003618{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003619 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003620 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003621 struct desktop_shell *shell = data;
3622 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003623 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003624
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003625 focus_surface = seat->keyboard->focus;
3626 if (!focus_surface)
3627 return;
3628
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003629 wl_signal_emit(&compositor->kill_signal, focus_surface);
3630
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003631 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003632 wl_client_get_credentials(client, &pid, NULL, NULL);
3633
3634 /* Skip clients that we launched ourselves (the credentials of
3635 * the socketpair is ours) */
3636 if (pid == getpid())
3637 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003638
Daniel Stone325fc2d2012-05-30 16:31:58 +01003639 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003640}
3641
3642static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003643workspace_up_binding(struct wl_seat *seat, uint32_t time,
3644 uint32_t key, void *data)
3645{
3646 struct desktop_shell *shell = data;
3647 unsigned int new_index = shell->workspaces.current;
3648
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003649 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003650 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003651 if (new_index != 0)
3652 new_index--;
3653
3654 change_workspace(shell, new_index);
3655}
3656
3657static void
3658workspace_down_binding(struct wl_seat *seat, uint32_t time,
3659 uint32_t key, void *data)
3660{
3661 struct desktop_shell *shell = data;
3662 unsigned int new_index = shell->workspaces.current;
3663
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003664 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003665 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003666 if (new_index < shell->workspaces.num - 1)
3667 new_index++;
3668
3669 change_workspace(shell, new_index);
3670}
3671
3672static void
3673workspace_f_binding(struct wl_seat *seat, uint32_t time,
3674 uint32_t key, void *data)
3675{
3676 struct desktop_shell *shell = data;
3677 unsigned int new_index;
3678
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003679 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003680 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003681 new_index = key - KEY_F1;
3682 if (new_index >= shell->workspaces.num)
3683 new_index = shell->workspaces.num - 1;
3684
3685 change_workspace(shell, new_index);
3686}
3687
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003688static void
3689workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3690 uint32_t key, void *data)
3691{
3692 struct desktop_shell *shell = data;
3693 unsigned int new_index = shell->workspaces.current;
3694
3695 if (shell->locked)
3696 return;
3697
3698 if (new_index != 0)
3699 new_index--;
3700
3701 take_surface_to_workspace_by_seat(shell, seat, new_index);
3702}
3703
3704static void
3705workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3706 uint32_t key, void *data)
3707{
3708 struct desktop_shell *shell = data;
3709 unsigned int new_index = shell->workspaces.current;
3710
3711 if (shell->locked)
3712 return;
3713
3714 if (new_index < shell->workspaces.num - 1)
3715 new_index++;
3716
3717 take_surface_to_workspace_by_seat(shell, seat, new_index);
3718}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003719
3720static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003721shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003722{
Tiago Vignattibe143262012-04-16 17:31:41 +03003723 struct desktop_shell *shell =
3724 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003725 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003726
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003727 if (shell->child.client)
3728 wl_client_destroy(shell->child.client);
3729
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003730 wl_list_remove(&shell->lock_listener.link);
3731 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003732 wl_list_remove(&shell->show_input_panel_listener.link);
3733 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003734
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003735 wl_array_for_each(ws, &shell->workspaces.array)
3736 workspace_destroy(*ws);
3737 wl_array_release(&shell->workspaces.array);
3738
Pekka Paalanen3c647232011-12-22 13:43:43 +02003739 free(shell->screensaver.path);
3740 free(shell);
3741}
3742
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003743static void
3744shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3745{
3746 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003747 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003748
3749 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003750 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3751 MODIFIER_CTRL | MODIFIER_ALT,
3752 terminate_binding, ec);
3753 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3754 click_to_activate_binding,
3755 shell);
3756 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3757 MODIFIER_SUPER | MODIFIER_ALT,
3758 surface_opacity_binding, NULL);
3759 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3760 MODIFIER_SUPER, zoom_axis_binding,
3761 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003762
3763 /* configurable bindings */
3764 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003765 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3766 zoom_key_binding, NULL);
3767 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3768 zoom_key_binding, NULL);
3769 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3770 shell);
3771 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3772 resize_binding, shell);
3773 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3774 rotate_binding, NULL);
3775 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3776 shell);
3777 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3778 ec);
3779 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3780 backlight_binding, ec);
3781 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3782 ec);
3783 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3784 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003785 weston_compositor_add_key_binding(ec, KEY_K, mod,
3786 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003787 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3788 workspace_up_binding, shell);
3789 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3790 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003791 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3792 workspace_move_surface_up_binding,
3793 shell);
3794 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3795 workspace_move_surface_down_binding,
3796 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003797
3798 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3799 if (shell->workspaces.num > 1) {
3800 num_workspace_bindings = shell->workspaces.num;
3801 if (num_workspace_bindings > 6)
3802 num_workspace_bindings = 6;
3803 for (i = 0; i < num_workspace_bindings; i++)
3804 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3805 workspace_f_binding,
3806 shell);
3807 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003808
3809 /* Debug bindings */
3810 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3811 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003812 weston_compositor_add_debug_binding(ec, KEY_F,
3813 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003814}
3815
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003816WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003817module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003818{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003819 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003820 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003821 struct workspace **pws;
3822 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003823 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003824
3825 shell = malloc(sizeof *shell);
3826 if (shell == NULL)
3827 return -1;
3828
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003829 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003830 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003831
3832 shell->destroy_listener.notify = shell_destroy;
3833 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3834 shell->lock_listener.notify = lock;
3835 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3836 shell->unlock_listener.notify = unlock;
3837 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003838 shell->show_input_panel_listener.notify = show_input_panels;
3839 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3840 shell->hide_input_panel_listener.notify = hide_input_panels;
3841 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003842 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003843 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003844 ec->shell_interface.create_shell_surface = create_shell_surface;
3845 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003846 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003847 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003848 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003849
Pekka Paalanen77346a62011-11-30 16:26:35 +02003850 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003851 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003852
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003853 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3854 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003855 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3856 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003857 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003858
3859 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003860 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003861
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003862 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003863
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003864 for (i = 0; i < shell->workspaces.num; i++) {
3865 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3866 if (pws == NULL)
3867 return -1;
3868
3869 *pws = workspace_create();
3870 if (*pws == NULL)
3871 return -1;
3872 }
3873 activate_workspace(shell, 0);
3874
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003875 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003876 wl_list_init(&shell->workspaces.animation.link);
3877 shell->workspaces.animation.frame = animate_workspace_change_frame;
3878
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003879 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3880 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003881 return -1;
3882
Kristian Høgsberg75840622011-09-06 13:48:16 -04003883 if (wl_display_add_global(ec->wl_display,
3884 &desktop_shell_interface,
3885 shell, bind_desktop_shell) == NULL)
3886 return -1;
3887
Pekka Paalanen6e168112011-11-24 11:34:05 +02003888 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3889 shell, bind_screensaver) == NULL)
3890 return -1;
3891
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003892 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3893 shell, bind_input_panel) == NULL)
3894 return -1;
3895
Jonas Ådahle9d22502012-08-29 22:13:01 +02003896 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3897 shell, bind_workspace_manager) == NULL)
3898 return -1;
3899
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003900 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003901
3902 loop = wl_display_get_event_loop(ec->wl_display);
3903 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003904
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003905 wl_list_for_each(seat, &ec->seat_list, link)
3906 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003907
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003908 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003909
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003910 return 0;
3911}