blob: da17cfbdd8029604c5f33bafe9b0166f0b5be982 [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"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010038#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050041
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020043#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044
Juan Zhaoe10d2792012-04-25 19:09:52 +080045enum animation_type {
46 ANIMATION_NONE,
47
48 ANIMATION_ZOOM,
49 ANIMATION_FADE
50};
51
Jonas Ådahl04769742012-06-13 00:01:24 +020052struct focus_state {
53 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Jonas Ådahle3cddce2012-06-13 00:01:22 +020061struct workspace {
62 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020063
64 struct wl_list focus_list;
65 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020066};
67
Philipp Brüschweiler88013572012-08-06 13:44:42 +020068struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010069 struct wl_resource resource;
70
71 struct desktop_shell *shell;
72
Philipp Brüschweiler88013572012-08-06 13:44:42 +020073 struct wl_list link;
74 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010075 struct wl_listener surface_destroy_listener;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020076};
77
Tiago Vignattibe143262012-04-16 17:31:41 +030078struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050079 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040080
81 struct wl_listener lock_listener;
82 struct wl_listener unlock_listener;
83 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020084 struct wl_listener show_input_panel_listener;
85 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020086
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050087 struct weston_layer fullscreen_layer;
88 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050089 struct weston_layer background_layer;
90 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020091 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050092
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040093 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030094 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040095
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020096 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020099 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200100
101 unsigned deathcount;
102 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200103 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200104
105 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200106 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200107 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200108
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400109 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500110 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100111
Pekka Paalanen77346a62011-11-30 16:26:35 +0200112 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200113 struct wl_array array;
114 unsigned int current;
115 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200116
Jonas Ådahle9d22502012-08-29 22:13:01 +0200117 struct wl_list client_list;
118
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200119 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200120 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200121 int anim_dir;
122 uint32_t anim_timestamp;
123 double anim_current;
124 struct workspace *anim_from;
125 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200126 } workspaces;
127
128 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200129 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200130 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200131 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500132 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200133 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500134
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200135 struct {
136 struct wl_resource *binding;
137 struct wl_list surfaces;
138 } input_panel;
139
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300140 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800141 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400142};
143
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500144enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200145 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500146 SHELL_SURFACE_TOPLEVEL,
147 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500148 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800149 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500150 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200151};
152
Scott Moreauff1db4a2012-04-17 19:06:18 -0600153struct ping_timer {
154 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600155 uint32_t serial;
156};
157
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200158struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200159 struct wl_resource resource;
160
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500161 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200162 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400163 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300164 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200165
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400166 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400167 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500168 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800169 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800170 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600171 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100172
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500175 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200176 } rotation;
177
178 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700179 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500180 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200181 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500182 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100183 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400184 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500185 } popup;
186
Alex Wu4539b082012-03-01 12:57:46 +0800187 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300188 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400189 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300190 } transient;
191
192 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800193 enum wl_shell_surface_fullscreen_method type;
194 struct weston_transform transform; /* matrix from x, y */
195 uint32_t framerate;
196 struct weston_surface *black_surface;
197 } fullscreen;
198
Scott Moreauff1db4a2012-04-17 19:06:18 -0600199 struct ping_timer *ping_timer;
200
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200201 struct weston_transform workspace_transform;
202
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500203 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500204 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100205 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400206
207 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200208};
209
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700211 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212 struct shell_surface *shsurf;
213 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300214 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300215};
216
217struct weston_move_grab {
218 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100219 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220};
221
Pekka Paalanen460099f2012-01-20 16:48:25 +0200222struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300223 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500224 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200225 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200226 float x;
227 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200228 } center;
229};
230
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400231static void
232activate(struct desktop_shell *shell, struct weston_surface *es,
233 struct weston_seat *seat);
234
235static struct workspace *
236get_current_workspace(struct desktop_shell *shell);
237
Alex Wubd3354b2012-04-17 17:20:49 +0800238static struct shell_surface *
239get_shell_surface(struct weston_surface *surface);
240
241static struct desktop_shell *
242shell_surface_get_shell(struct shell_surface *shsurf);
243
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244static void
245surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
246
Alex Wubd3354b2012-04-17 17:20:49 +0800247static bool
248shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
249{
250 struct desktop_shell *shell;
251 struct weston_surface *top_fs_es;
252
253 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100254
Alex Wubd3354b2012-04-17 17:20:49 +0800255 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
256 return false;
257
258 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100259 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800260 layer_link);
261 return (shsurf == get_shell_surface(top_fs_es));
262}
263
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500264static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400265destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300266{
267 struct shell_grab *grab;
268
269 grab = container_of(listener, struct shell_grab,
270 shsurf_destroy_listener);
271
272 grab->shsurf = NULL;
273}
274
275static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400276popup_grab_end(struct wl_pointer *pointer);
277
278static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300279shell_grab_start(struct shell_grab *grab,
280 const struct wl_pointer_grab_interface *interface,
281 struct shell_surface *shsurf,
282 struct wl_pointer *pointer,
283 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300285 struct desktop_shell *shell = shsurf->shell;
286
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400287 popup_grab_end(pointer);
288
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289 grab->grab.interface = interface;
290 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400291 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
292 wl_signal_add(&shsurf->resource.destroy_signal,
293 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300295 grab->pointer = pointer;
296 grab->grab.focus = &shsurf->surface->surface;
297
298 wl_pointer_start_grab(pointer, &grab->grab);
299 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
300 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
301 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300302}
303
304static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300305shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400307 if (grab->shsurf)
308 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300309
310 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300311}
312
313static void
Alex Wu4539b082012-03-01 12:57:46 +0800314center_on_output(struct weston_surface *surface,
315 struct weston_output *output);
316
Daniel Stone496ca172012-05-30 16:31:42 +0100317static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300318get_modifier(char *modifier)
319{
320 if (!modifier)
321 return MODIFIER_SUPER;
322
323 if (!strcmp("ctrl", modifier))
324 return MODIFIER_CTRL;
325 else if (!strcmp("alt", modifier))
326 return MODIFIER_ALT;
327 else if (!strcmp("super", modifier))
328 return MODIFIER_SUPER;
329 else
330 return MODIFIER_SUPER;
331}
332
Juan Zhaoe10d2792012-04-25 19:09:52 +0800333static enum animation_type
334get_animation_type(char *animation)
335{
336 if (!animation)
337 return ANIMATION_NONE;
338
339 if (!strcmp("zoom", animation))
340 return ANIMATION_ZOOM;
341 else if (!strcmp("fade", animation))
342 return ANIMATION_FADE;
343 else
344 return ANIMATION_NONE;
345}
346
Alex Wu4539b082012-03-01 12:57:46 +0800347static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500348shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200349{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200350 char *path = NULL;
351 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200352 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300353 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800354 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200355
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400356 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300357 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800358 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200359 { "num-workspaces",
360 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200361 };
362
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400363 struct config_key saver_keys[] = {
364 { "path", CONFIG_KEY_STRING, &path },
365 { "duration", CONFIG_KEY_INTEGER, &duration },
366 };
367
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200368 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400369 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200370 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
371 };
372
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500373 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200374
Pekka Paalanen7296e792011-12-07 16:22:00 +0200375 shell->screensaver.path = path;
376 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300377 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800378 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200379 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
380}
381
382static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200383focus_state_destroy(struct focus_state *state)
384{
385 wl_list_remove(&state->seat_destroy_listener.link);
386 wl_list_remove(&state->surface_destroy_listener.link);
387 free(state);
388}
389
390static void
391focus_state_seat_destroy(struct wl_listener *listener, void *data)
392{
393 struct focus_state *state = container_of(listener,
394 struct focus_state,
395 seat_destroy_listener);
396
397 wl_list_remove(&state->link);
398 focus_state_destroy(state);
399}
400
401static void
402focus_state_surface_destroy(struct wl_listener *listener, void *data)
403{
404 struct focus_state *state = container_of(listener,
405 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400406 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400407 struct desktop_shell *shell;
408 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200409
Kristian Høgsberge3778222012-07-31 17:29:30 -0400410 next = NULL;
411 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
412 if (surface == state->keyboard_focus)
413 continue;
414
415 next = surface;
416 break;
417 }
418
419 if (next) {
420 shell = state->seat->compositor->shell_interface.shell;
421 activate(shell, next, state->seat);
422 } else {
423 wl_list_remove(&state->link);
424 focus_state_destroy(state);
425 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200426}
427
428static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400429focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200430{
Jonas Ådahl04769742012-06-13 00:01:24 +0200431 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200432
433 state = malloc(sizeof *state);
434 if (state == NULL)
435 return NULL;
436
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400437 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200438 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400439 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200440
441 state->seat_destroy_listener.notify = focus_state_seat_destroy;
442 state->surface_destroy_listener.notify = focus_state_surface_destroy;
443 wl_signal_add(&seat->seat.destroy_signal,
444 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400445 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200446
447 return state;
448}
449
Jonas Ådahl8538b222012-08-29 22:13:03 +0200450static struct focus_state *
451ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
452{
453 struct workspace *ws = get_current_workspace(shell);
454 struct focus_state *state;
455
456 wl_list_for_each(state, &ws->focus_list, link)
457 if (state->seat == seat)
458 break;
459
460 if (&state->link == &ws->focus_list)
461 state = focus_state_create(seat, ws);
462
463 return state;
464}
465
Jonas Ådahl04769742012-06-13 00:01:24 +0200466static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400467restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200468{
469 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200470 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200471
472 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200473 surface = state->keyboard_focus ?
474 &state->keyboard_focus->surface : NULL;
475
476 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200477 }
478}
479
480static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200481replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
482 struct weston_seat *seat)
483{
484 struct focus_state *state;
485 struct wl_surface *surface;
486
487 wl_list_for_each(state, &ws->focus_list, link) {
488 if (state->seat == seat) {
489 surface = seat->seat.keyboard->focus;
490 state->keyboard_focus =
491 (struct weston_surface *) surface;
492 return;
493 }
494 }
495}
496
497static void
498drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
499 struct weston_surface *surface)
500{
501 struct focus_state *state;
502
503 wl_list_for_each(state, &ws->focus_list, link)
504 if (state->keyboard_focus == surface)
505 state->keyboard_focus = NULL;
506}
507
508static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200509workspace_destroy(struct workspace *ws)
510{
Jonas Ådahl04769742012-06-13 00:01:24 +0200511 struct focus_state *state, *next;
512
513 wl_list_for_each_safe(state, next, &ws->focus_list, link)
514 focus_state_destroy(state);
515
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200516 free(ws);
517}
518
Jonas Ådahl04769742012-06-13 00:01:24 +0200519static void
520seat_destroyed(struct wl_listener *listener, void *data)
521{
522 struct weston_seat *seat = data;
523 struct focus_state *state, *next;
524 struct workspace *ws = container_of(listener,
525 struct workspace,
526 seat_destroyed_listener);
527
528 wl_list_for_each_safe(state, next, &ws->focus_list, link)
529 if (state->seat == seat)
530 wl_list_remove(&state->link);
531}
532
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200533static struct workspace *
534workspace_create(void)
535{
536 struct workspace *ws = malloc(sizeof *ws);
537 if (ws == NULL)
538 return NULL;
539
540 weston_layer_init(&ws->layer, NULL);
541
Jonas Ådahl04769742012-06-13 00:01:24 +0200542 wl_list_init(&ws->focus_list);
543 wl_list_init(&ws->seat_destroyed_listener.link);
544 ws->seat_destroyed_listener.notify = seat_destroyed;
545
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200546 return ws;
547}
548
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200549static int
550workspace_is_empty(struct workspace *ws)
551{
552 return wl_list_empty(&ws->layer.surface_list);
553}
554
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200555static struct workspace *
556get_workspace(struct desktop_shell *shell, unsigned int index)
557{
558 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200559 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200560 pws += index;
561 return *pws;
562}
563
564static struct workspace *
565get_current_workspace(struct desktop_shell *shell)
566{
567 return get_workspace(shell, shell->workspaces.current);
568}
569
570static void
571activate_workspace(struct desktop_shell *shell, unsigned int index)
572{
573 struct workspace *ws;
574
575 ws = get_workspace(shell, index);
576 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
577
578 shell->workspaces.current = index;
579}
580
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200581static unsigned int
582get_output_height(struct weston_output *output)
583{
584 return abs(output->region.extents.y1 - output->region.extents.y2);
585}
586
587static void
588surface_translate(struct weston_surface *surface, double d)
589{
590 struct shell_surface *shsurf = get_shell_surface(surface);
591 struct weston_transform *transform;
592
593 transform = &shsurf->workspace_transform;
594 if (wl_list_empty(&transform->link))
595 wl_list_insert(surface->geometry.transformation_list.prev,
596 &shsurf->workspace_transform.link);
597
598 weston_matrix_init(&shsurf->workspace_transform.matrix);
599 weston_matrix_translate(&shsurf->workspace_transform.matrix,
600 0.0, d, 0.0);
601 surface->geometry.dirty = 1;
602}
603
604static void
605workspace_translate_out(struct workspace *ws, double fraction)
606{
607 struct weston_surface *surface;
608 unsigned int height;
609 double d;
610
611 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
612 height = get_output_height(surface->output);
613 d = height * fraction;
614
615 surface_translate(surface, d);
616 }
617}
618
619static void
620workspace_translate_in(struct workspace *ws, double fraction)
621{
622 struct weston_surface *surface;
623 unsigned int height;
624 double d;
625
626 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
627 height = get_output_height(surface->output);
628
629 if (fraction > 0)
630 d = -(height - height * fraction);
631 else
632 d = height + height * fraction;
633
634 surface_translate(surface, d);
635 }
636}
637
638static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200639broadcast_current_workspace_state(struct desktop_shell *shell)
640{
641 struct wl_resource *resource;
642
643 wl_list_for_each(resource, &shell->workspaces.client_list, link)
644 workspace_manager_send_state(resource,
645 shell->workspaces.current,
646 shell->workspaces.num);
647}
648
649static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200650reverse_workspace_change_animation(struct desktop_shell *shell,
651 unsigned int index,
652 struct workspace *from,
653 struct workspace *to)
654{
655 shell->workspaces.current = index;
656
657 shell->workspaces.anim_to = to;
658 shell->workspaces.anim_from = from;
659 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
660 shell->workspaces.anim_timestamp = 0;
661
Scott Moreau4272e632012-08-13 09:58:41 -0600662 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200663}
664
665static void
666workspace_deactivate_transforms(struct workspace *ws)
667{
668 struct weston_surface *surface;
669 struct shell_surface *shsurf;
670
671 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
672 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200673 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
674 wl_list_remove(&shsurf->workspace_transform.link);
675 wl_list_init(&shsurf->workspace_transform.link);
676 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200677 shsurf->surface->geometry.dirty = 1;
678 }
679}
680
681static void
682finish_workspace_change_animation(struct desktop_shell *shell,
683 struct workspace *from,
684 struct workspace *to)
685{
Scott Moreau4272e632012-08-13 09:58:41 -0600686 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200687
688 wl_list_remove(&shell->workspaces.animation.link);
689 workspace_deactivate_transforms(from);
690 workspace_deactivate_transforms(to);
691 shell->workspaces.anim_to = NULL;
692
693 wl_list_remove(&shell->workspaces.anim_from->layer.link);
694}
695
696static void
697animate_workspace_change_frame(struct weston_animation *animation,
698 struct weston_output *output, uint32_t msecs)
699{
700 struct desktop_shell *shell =
701 container_of(animation, struct desktop_shell,
702 workspaces.animation);
703 struct workspace *from = shell->workspaces.anim_from;
704 struct workspace *to = shell->workspaces.anim_to;
705 uint32_t t;
706 double x, y;
707
708 if (workspace_is_empty(from) && workspace_is_empty(to)) {
709 finish_workspace_change_animation(shell, from, to);
710 return;
711 }
712
713 if (shell->workspaces.anim_timestamp == 0) {
714 if (shell->workspaces.anim_current == 0.0)
715 shell->workspaces.anim_timestamp = msecs;
716 else
717 shell->workspaces.anim_timestamp =
718 msecs -
719 /* Invers of movement function 'y' below. */
720 (asin(1.0 - shell->workspaces.anim_current) *
721 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
722 M_2_PI);
723 }
724
725 t = msecs - shell->workspaces.anim_timestamp;
726
727 /*
728 * x = [0, π/2]
729 * y(x) = sin(x)
730 */
731 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
732 y = sin(x);
733
734 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600735 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200736
737 workspace_translate_out(from, shell->workspaces.anim_dir * y);
738 workspace_translate_in(to, shell->workspaces.anim_dir * y);
739 shell->workspaces.anim_current = y;
740
Scott Moreau4272e632012-08-13 09:58:41 -0600741 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200742 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200743 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200744 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200745}
746
747static void
748animate_workspace_change(struct desktop_shell *shell,
749 unsigned int index,
750 struct workspace *from,
751 struct workspace *to)
752{
753 struct weston_output *output;
754
755 int dir;
756
757 if (index > shell->workspaces.current)
758 dir = -1;
759 else
760 dir = 1;
761
762 shell->workspaces.current = index;
763
764 shell->workspaces.anim_dir = dir;
765 shell->workspaces.anim_from = from;
766 shell->workspaces.anim_to = to;
767 shell->workspaces.anim_current = 0.0;
768 shell->workspaces.anim_timestamp = 0;
769
770 output = container_of(shell->compositor->output_list.next,
771 struct weston_output, link);
772 wl_list_insert(&output->animation_list,
773 &shell->workspaces.animation.link);
774
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200775 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776
777 workspace_translate_in(to, 0);
778
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400779 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200780
Scott Moreau4272e632012-08-13 09:58:41 -0600781 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782}
783
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200784static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200785update_workspace(struct desktop_shell *shell, unsigned int index,
786 struct workspace *from, struct workspace *to)
787{
788 shell->workspaces.current = index;
789 wl_list_insert(&from->layer.link, &to->layer.link);
790 wl_list_remove(&from->layer.link);
791}
792
793static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200794change_workspace(struct desktop_shell *shell, unsigned int index)
795{
796 struct workspace *from;
797 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200798
799 if (index == shell->workspaces.current)
800 return;
801
802 /* Don't change workspace when there is any fullscreen surfaces. */
803 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
804 return;
805
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200806 from = get_current_workspace(shell);
807 to = get_workspace(shell, index);
808
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200809 if (shell->workspaces.anim_from == to &&
810 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200811 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200813 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200814 return;
815 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200816
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200817 if (shell->workspaces.anim_to != NULL)
818 finish_workspace_change_animation(shell,
819 shell->workspaces.anim_from,
820 shell->workspaces.anim_to);
821
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200822 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200823
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200824 if (workspace_is_empty(to) && workspace_is_empty(from))
825 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200826 else
827 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200828
829 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200830}
831
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200832static bool
833workspace_has_only(struct workspace *ws, struct weston_surface *surface)
834{
835 struct wl_list *list = &ws->layer.surface_list;
836 struct wl_list *e;
837
838 if (wl_list_empty(list))
839 return false;
840
841 e = list->next;
842
843 if (e->next != list)
844 return false;
845
846 return container_of(e, struct weston_surface, layer_link) == surface;
847}
848
849static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200850move_surface_to_workspace(struct desktop_shell *shell,
851 struct weston_surface *surface,
852 uint32_t workspace)
853{
854 struct workspace *from;
855 struct workspace *to;
856 struct weston_seat *seat;
857
858 if (workspace == shell->workspaces.current)
859 return;
860
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200861 if (workspace >= shell->workspaces.num)
862 workspace = shell->workspaces.num - 1;
863
Jonas Ådahle9d22502012-08-29 22:13:01 +0200864 from = get_current_workspace(shell);
865 to = get_workspace(shell, workspace);
866
867 wl_list_remove(&surface->layer_link);
868 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
869
870 drop_focus_state(shell, from, surface);
871 wl_list_for_each(seat, &shell->compositor->seat_list, link)
872 if (seat->has_keyboard &&
Jan Arne Petersena75a7892013-01-16 21:26:50 +0100873 seat->keyboard.keyboard.focus == &surface->surface)
874 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200875
876 weston_surface_damage_below(surface);
877}
878
879static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200880take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200881 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200882 unsigned int index)
883{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200885 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200886 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200887 struct shell_surface *shsurf;
888 struct workspace *from;
889 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200890 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200891
892 if (surface == NULL ||
893 index == shell->workspaces.current)
894 return;
895
896 from = get_current_workspace(shell);
897 to = get_workspace(shell, index);
898
899 wl_list_remove(&surface->layer_link);
900 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
901
Jonas Ådahle9d22502012-08-29 22:13:01 +0200902 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200903 drop_focus_state(shell, from, surface);
904
905 if (shell->workspaces.anim_from == to &&
906 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907 wl_list_remove(&to->layer.link);
908 wl_list_insert(from->layer.link.prev, &to->layer.link);
909
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200910 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200911 broadcast_current_workspace_state(shell);
912
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200913 return;
914 }
915
916 if (shell->workspaces.anim_to != NULL)
917 finish_workspace_change_animation(shell,
918 shell->workspaces.anim_from,
919 shell->workspaces.anim_to);
920
921 if (workspace_is_empty(from) &&
922 workspace_has_only(to, surface))
923 update_workspace(shell, index, from, to);
924 else {
925 shsurf = get_shell_surface(surface);
926 if (wl_list_empty(&shsurf->workspace_transform.link))
927 wl_list_insert(&shell->workspaces.anim_sticky_list,
928 &shsurf->workspace_transform.link);
929
930 animate_workspace_change(shell, index, from, to);
931 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200932
933 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200934
935 state = ensure_focus_state(shell, seat);
936 if (state != NULL)
937 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200938}
939
940static void
941workspace_manager_move_surface(struct wl_client *client,
942 struct wl_resource *resource,
943 struct wl_resource *surface_resource,
944 uint32_t workspace)
945{
946 struct desktop_shell *shell = resource->data;
947 struct weston_surface *surface =
948 (struct weston_surface *) surface_resource;
949
950 move_surface_to_workspace(shell, surface, workspace);
951}
952
953static const struct workspace_manager_interface workspace_manager_implementation = {
954 workspace_manager_move_surface,
955};
956
957static void
958unbind_resource(struct wl_resource *resource)
959{
960 wl_list_remove(&resource->link);
961 free(resource);
962}
963
964static void
965bind_workspace_manager(struct wl_client *client,
966 void *data, uint32_t version, uint32_t id)
967{
968 struct desktop_shell *shell = data;
969 struct wl_resource *resource;
970
971 resource = wl_client_add_object(client, &workspace_manager_interface,
972 &workspace_manager_implementation,
973 id, shell);
974
975 if (resource == NULL) {
976 weston_log("couldn't add workspace manager object");
977 return;
978 }
979
980 resource->destroy = unbind_resource;
981 wl_list_insert(&shell->workspaces.client_list, &resource->link);
982
983 workspace_manager_send_state(resource,
984 shell->workspaces.current,
985 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200986}
987
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200988static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400989noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100990 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500991{
992 grab->focus = NULL;
993}
994
995static void
Scott Moreau447013d2012-02-18 05:05:29 -0700996move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100997 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500998{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500999 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001000 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001001 struct shell_surface *shsurf = move->base.shsurf;
1002 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001003 int dx = wl_fixed_to_int(pointer->x + move->dx);
1004 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001005
1006 if (!shsurf)
1007 return;
1008
1009 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010
Daniel Stone103db7f2012-05-08 17:17:55 +01001011 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001012 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001013
1014 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001015}
1016
1017static void
Scott Moreau447013d2012-02-18 05:05:29 -07001018move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001019 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001021 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1022 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001023 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001024 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001025
Daniel Stone4dbadb12012-05-30 16:31:51 +01001026 if (pointer->button_count == 0 &&
1027 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001028 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001029 free(grab);
1030 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001031}
1032
Scott Moreau447013d2012-02-18 05:05:29 -07001033static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001034 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001035 move_grab_motion,
1036 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001037};
1038
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001039static int
1040surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1041{
1042 struct weston_move_grab *move;
1043
1044 if (!shsurf)
1045 return -1;
1046
1047 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1048 return 0;
1049
1050 move = malloc(sizeof *move);
1051 if (!move)
1052 return -1;
1053
1054 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1055 ws->seat.pointer->grab_x;
1056 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1057 ws->seat.pointer->grab_y;
1058
1059 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1060 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1061
1062 return 0;
1063}
1064
1065static void
1066shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1067 struct wl_resource *seat_resource, uint32_t serial)
1068{
1069 struct weston_seat *ws = seat_resource->data;
1070 struct shell_surface *shsurf = resource->data;
1071
1072 if (ws->seat.pointer->button_count == 0 ||
1073 ws->seat.pointer->grab_serial != serial ||
1074 ws->seat.pointer->focus != &shsurf->surface->surface)
1075 return;
1076
1077 if (surface_move(shsurf, ws) < 0)
1078 wl_resource_post_no_memory(resource);
1079}
1080
1081struct weston_resize_grab {
1082 struct shell_grab base;
1083 uint32_t edges;
1084 int32_t width, height;
1085};
1086
1087static void
1088resize_grab_motion(struct wl_pointer_grab *grab,
1089 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1090{
1091 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1092 struct wl_pointer *pointer = grab->pointer;
1093 struct shell_surface *shsurf = resize->base.shsurf;
1094 int32_t width, height;
1095 wl_fixed_t from_x, from_y;
1096 wl_fixed_t to_x, to_y;
1097
1098 if (!shsurf)
1099 return;
1100
1101 weston_surface_from_global_fixed(shsurf->surface,
1102 pointer->grab_x, pointer->grab_y,
1103 &from_x, &from_y);
1104 weston_surface_from_global_fixed(shsurf->surface,
1105 pointer->x, pointer->y, &to_x, &to_y);
1106
1107 width = resize->width;
1108 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1109 width += wl_fixed_to_int(from_x - to_x);
1110 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1111 width += wl_fixed_to_int(to_x - from_x);
1112 }
1113
1114 height = resize->height;
1115 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1116 height += wl_fixed_to_int(from_y - to_y);
1117 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1118 height += wl_fixed_to_int(to_y - from_y);
1119 }
1120
1121 shsurf->client->send_configure(shsurf->surface,
1122 resize->edges, width, height);
1123}
1124
1125static void
1126send_configure(struct weston_surface *surface,
1127 uint32_t edges, int32_t width, int32_t height)
1128{
1129 struct shell_surface *shsurf = get_shell_surface(surface);
1130
1131 wl_shell_surface_send_configure(&shsurf->resource,
1132 edges, width, height);
1133}
1134
1135static const struct weston_shell_client shell_client = {
1136 send_configure
1137};
1138
1139static void
1140resize_grab_button(struct wl_pointer_grab *grab,
1141 uint32_t time, uint32_t button, uint32_t state_w)
1142{
1143 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1144 struct wl_pointer *pointer = grab->pointer;
1145 enum wl_pointer_button_state state = state_w;
1146
1147 if (pointer->button_count == 0 &&
1148 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1149 shell_grab_end(&resize->base);
1150 free(grab);
1151 }
1152}
1153
1154static const struct wl_pointer_grab_interface resize_grab_interface = {
1155 noop_grab_focus,
1156 resize_grab_motion,
1157 resize_grab_button,
1158};
1159
1160static int
1161surface_resize(struct shell_surface *shsurf,
1162 struct weston_seat *ws, uint32_t edges)
1163{
1164 struct weston_resize_grab *resize;
1165
1166 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1167 return 0;
1168
1169 if (edges == 0 || edges > 15 ||
1170 (edges & 3) == 3 || (edges & 12) == 12)
1171 return 0;
1172
1173 resize = malloc(sizeof *resize);
1174 if (!resize)
1175 return -1;
1176
1177 resize->edges = edges;
1178 resize->width = shsurf->surface->geometry.width;
1179 resize->height = shsurf->surface->geometry.height;
1180
1181 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1182 ws->seat.pointer, edges);
1183
1184 return 0;
1185}
1186
1187static void
1188shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1189 struct wl_resource *seat_resource, uint32_t serial,
1190 uint32_t edges)
1191{
1192 struct weston_seat *ws = seat_resource->data;
1193 struct shell_surface *shsurf = resource->data;
1194
1195 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1196 return;
1197
1198 if (ws->seat.pointer->button_count == 0 ||
1199 ws->seat.pointer->grab_serial != serial ||
1200 ws->seat.pointer->focus != &shsurf->surface->surface)
1201 return;
1202
1203 if (surface_resize(shsurf, ws, edges) < 0)
1204 wl_resource_post_no_memory(resource);
1205}
1206
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001207static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001208busy_cursor_grab_focus(struct wl_pointer_grab *base,
1209 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001210{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001211 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001212
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001213 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001214 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001215 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001216 }
1217}
1218
1219static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001220busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1221 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001222{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001223}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001224
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001225static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001226busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001227 uint32_t time, uint32_t button, uint32_t state)
1228{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001229 struct shell_grab *grab = (struct shell_grab *) base;
1230 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001231 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001232 (struct weston_surface *) grab->grab.pointer->current;
1233 struct weston_seat *seat =
1234 (struct weston_seat *) grab->grab.pointer->seat;
1235
1236 shsurf = get_shell_surface(surface);
1237 if (shsurf && button == BTN_LEFT && state) {
1238 activate(shsurf->shell, shsurf->surface, seat);
1239 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001240 } else if (shsurf && button == BTN_RIGHT && state) {
1241 activate(shsurf->shell, shsurf->surface, seat);
1242 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001243 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001244}
1245
1246static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1247 busy_cursor_grab_focus,
1248 busy_cursor_grab_motion,
1249 busy_cursor_grab_button,
1250};
1251
1252static void
1253set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1254{
1255 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001256
1257 grab = malloc(sizeof *grab);
1258 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001259 return;
1260
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001261 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1262 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001263}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001264
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001265static void
1266end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1267{
1268 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1269
1270 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001271 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001272 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001273 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001274}
1275
Scott Moreau9521d5e2012-04-19 13:06:17 -06001276static void
1277ping_timer_destroy(struct shell_surface *shsurf)
1278{
1279 if (!shsurf || !shsurf->ping_timer)
1280 return;
1281
1282 if (shsurf->ping_timer->source)
1283 wl_event_source_remove(shsurf->ping_timer->source);
1284
1285 free(shsurf->ping_timer);
1286 shsurf->ping_timer = NULL;
1287}
1288
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001289static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001290ping_timeout_handler(void *data)
1291{
1292 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001293 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001294
Scott Moreau9521d5e2012-04-19 13:06:17 -06001295 /* Client is not responding */
1296 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001297
1298 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1299 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1300 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001301
1302 return 1;
1303}
1304
1305static void
1306ping_handler(struct weston_surface *surface, uint32_t serial)
1307{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001308 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001309 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001310 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001311
1312 if (!shsurf)
1313 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001314 if (!shsurf->resource.client)
1315 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001316
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001317 if (shsurf->surface == shsurf->shell->grab_surface)
1318 return;
1319
Scott Moreauff1db4a2012-04-17 19:06:18 -06001320 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001321 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001322 if (!shsurf->ping_timer)
1323 return;
1324
1325 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001326 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1327 shsurf->ping_timer->source =
1328 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1329 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1330
1331 wl_shell_surface_send_ping(&shsurf->resource, serial);
1332 }
1333}
1334
1335static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001336handle_pointer_focus(struct wl_listener *listener, void *data)
1337{
1338 struct wl_pointer *pointer = data;
1339 struct weston_surface *surface =
1340 (struct weston_surface *) pointer->focus;
1341 struct weston_compositor *compositor;
1342 struct shell_surface *shsurf;
1343 uint32_t serial;
1344
1345 if (!surface)
1346 return;
1347
1348 compositor = surface->compositor;
1349 shsurf = get_shell_surface(surface);
1350
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001351 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001352 set_busy_cursor(shsurf, pointer);
1353 } else {
1354 serial = wl_display_next_serial(compositor->wl_display);
1355 ping_handler(surface, serial);
1356 }
1357}
1358
1359static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001360create_pointer_focus_listener(struct weston_seat *seat)
1361{
1362 struct wl_listener *listener;
1363
1364 if (!seat->seat.pointer)
1365 return;
1366
1367 listener = malloc(sizeof *listener);
1368 listener->notify = handle_pointer_focus;
1369 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1370}
1371
1372static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001373shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1374 uint32_t serial)
1375{
1376 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001377 struct desktop_shell *shell = shsurf->shell;
1378 struct weston_seat *seat;
1379 struct weston_compositor *ec = shsurf->surface->compositor;
1380 struct wl_pointer *pointer;
1381 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001382
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001383 if (shsurf->ping_timer == NULL)
1384 /* Just ignore unsolicited pong. */
1385 return;
1386
Scott Moreauff1db4a2012-04-17 19:06:18 -06001387 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001388 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001389 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001390 if (was_unresponsive) {
1391 /* Received pong from previously unresponsive client */
1392 wl_list_for_each(seat, &ec->seat_list, link) {
1393 pointer = seat->seat.pointer;
1394 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001395 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001396 pointer->current ==
1397 &shsurf->surface->surface)
1398 end_busy_cursor(shsurf, pointer);
1399 }
1400 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001401 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001402 }
1403}
1404
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001405static void
1406shell_surface_set_title(struct wl_client *client,
1407 struct wl_resource *resource, const char *title)
1408{
1409 struct shell_surface *shsurf = resource->data;
1410
1411 free(shsurf->title);
1412 shsurf->title = strdup(title);
1413}
1414
1415static void
1416shell_surface_set_class(struct wl_client *client,
1417 struct wl_resource *resource, const char *class)
1418{
1419 struct shell_surface *shsurf = resource->data;
1420
1421 free(shsurf->class);
1422 shsurf->class = strdup(class);
1423}
1424
Juan Zhao96879df2012-02-07 08:45:41 +08001425static struct weston_output *
1426get_default_output(struct weston_compositor *compositor)
1427{
1428 return container_of(compositor->output_list.next,
1429 struct weston_output, link);
1430}
1431
Alex Wu4539b082012-03-01 12:57:46 +08001432static void
1433shell_unset_fullscreen(struct shell_surface *shsurf)
1434{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001435 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001436 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001437 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1438 shell_surface_is_top_fullscreen(shsurf)) {
1439 weston_output_switch_mode(shsurf->fullscreen_output,
1440 shsurf->fullscreen_output->origin);
1441 }
Alex Wu4539b082012-03-01 12:57:46 +08001442 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1443 shsurf->fullscreen.framerate = 0;
1444 wl_list_remove(&shsurf->fullscreen.transform.link);
1445 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001446 if (shsurf->fullscreen.black_surface)
1447 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001448 shsurf->fullscreen.black_surface = NULL;
1449 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001450 weston_surface_set_position(shsurf->surface,
1451 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001452 if (shsurf->saved_rotation_valid) {
1453 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1454 &shsurf->rotation.transform.link);
1455 shsurf->saved_rotation_valid = false;
1456 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001457
1458 ws = get_current_workspace(shsurf->shell);
1459 wl_list_remove(&shsurf->surface->layer_link);
1460 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001461}
1462
Pekka Paalanen98262232011-12-01 10:42:22 +02001463static int
1464reset_shell_surface_type(struct shell_surface *surface)
1465{
1466 switch (surface->type) {
1467 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001468 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001469 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001470 case SHELL_SURFACE_MAXIMIZED:
1471 surface->output = get_default_output(surface->surface->compositor);
1472 weston_surface_set_position(surface->surface,
1473 surface->saved_x,
1474 surface->saved_y);
1475 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001476 case SHELL_SURFACE_NONE:
1477 case SHELL_SURFACE_TOPLEVEL:
1478 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001479 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001480 break;
1481 }
1482
1483 surface->type = SHELL_SURFACE_NONE;
1484 return 0;
1485}
1486
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001487static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001488set_surface_type(struct shell_surface *shsurf)
1489{
1490 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001491 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001492
1493 reset_shell_surface_type(shsurf);
1494
1495 shsurf->type = shsurf->next_type;
1496 shsurf->next_type = SHELL_SURFACE_NONE;
1497
1498 switch (shsurf->type) {
1499 case SHELL_SURFACE_TOPLEVEL:
1500 break;
1501 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001502 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001503 pes->geometry.x + shsurf->transient.x,
1504 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001505 break;
1506
1507 case SHELL_SURFACE_MAXIMIZED:
1508 shsurf->saved_x = surface->geometry.x;
1509 shsurf->saved_y = surface->geometry.y;
1510 shsurf->saved_position_valid = true;
1511 break;
1512
1513 case SHELL_SURFACE_FULLSCREEN:
1514 shsurf->saved_x = surface->geometry.x;
1515 shsurf->saved_y = surface->geometry.y;
1516 shsurf->saved_position_valid = true;
1517
1518 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1519 wl_list_remove(&shsurf->rotation.transform.link);
1520 wl_list_init(&shsurf->rotation.transform.link);
1521 shsurf->surface->geometry.dirty = 1;
1522 shsurf->saved_rotation_valid = true;
1523 }
1524 break;
1525
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001526 default:
1527 break;
1528 }
1529}
1530
1531static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001532set_toplevel(struct shell_surface *shsurf)
1533{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001534 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001535}
1536
1537static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001538shell_surface_set_toplevel(struct wl_client *client,
1539 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001540{
Pekka Paalanen98262232011-12-01 10:42:22 +02001541 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001542
Tiago Vignattibc052c92012-04-19 16:18:18 +03001543 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001544}
1545
1546static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001547set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001548 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001549{
1550 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001551 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001552 shsurf->transient.x = x;
1553 shsurf->transient.y = y;
1554 shsurf->transient.flags = flags;
1555 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1556}
1557
1558static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001559shell_surface_set_transient(struct wl_client *client,
1560 struct wl_resource *resource,
1561 struct wl_resource *parent_resource,
1562 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001563{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001564 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001565 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001566
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001567 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001568}
1569
Tiago Vignattibe143262012-04-16 17:31:41 +03001570static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001571shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001572{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001573 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001574}
1575
1576static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001577get_output_panel_height(struct desktop_shell *shell,
1578 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001579{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001580 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001581 int panel_height = 0;
1582
1583 if (!output)
1584 return 0;
1585
Juan Zhao4ab94682012-07-09 22:24:09 -07001586 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001587 if (surface->output == output) {
1588 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001589 break;
1590 }
1591 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001592
Juan Zhao96879df2012-02-07 08:45:41 +08001593 return panel_height;
1594}
1595
1596static void
1597shell_surface_set_maximized(struct wl_client *client,
1598 struct wl_resource *resource,
1599 struct wl_resource *output_resource )
1600{
1601 struct shell_surface *shsurf = resource->data;
1602 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001603 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001604 uint32_t edges = 0, panel_height = 0;
1605
1606 /* get the default output, if the client set it as NULL
1607 check whether the ouput is available */
1608 if (output_resource)
1609 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001610 else if (es->output)
1611 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001612 else
1613 shsurf->output = get_default_output(es->compositor);
1614
Tiago Vignattibe143262012-04-16 17:31:41 +03001615 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001616 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001617 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001618
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001619 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001620 shsurf->output->width,
1621 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001622
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001623 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001624}
1625
Alex Wu21858432012-04-01 20:13:08 +08001626static void
1627black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1628
Alex Wu4539b082012-03-01 12:57:46 +08001629static struct weston_surface *
1630create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001631 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001632 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001633{
1634 struct weston_surface *surface = NULL;
1635
1636 surface = weston_surface_create(ec);
1637 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001638 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001639 return NULL;
1640 }
1641
Alex Wu21858432012-04-01 20:13:08 +08001642 surface->configure = black_surface_configure;
1643 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001644 weston_surface_configure(surface, x, y, w, h);
1645 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001646 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001647 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001648 pixman_region32_fini(&surface->input);
1649 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001650
Alex Wu4539b082012-03-01 12:57:46 +08001651 return surface;
1652}
1653
1654/* Create black surface and append it to the associated fullscreen surface.
1655 * Handle size dismatch and positioning according to the method. */
1656static void
1657shell_configure_fullscreen(struct shell_surface *shsurf)
1658{
1659 struct weston_output *output = shsurf->fullscreen_output;
1660 struct weston_surface *surface = shsurf->surface;
1661 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001662 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001663
Alex Wu4539b082012-03-01 12:57:46 +08001664 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001665 shsurf->fullscreen.black_surface =
1666 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001667 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001668 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001669 output->width,
1670 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001671
1672 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1673 wl_list_insert(&surface->layer_link,
1674 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001675 shsurf->fullscreen.black_surface->output = output;
1676
1677 switch (shsurf->fullscreen.type) {
1678 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001679 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001680 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001681 break;
1682 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001683 /* 1:1 mapping between surface and output dimensions */
1684 if (output->width == surface->geometry.width &&
1685 output->height == surface->geometry.height) {
1686 weston_surface_set_position(surface, output->x, output->y);
1687 break;
1688 }
1689
Alex Wu4539b082012-03-01 12:57:46 +08001690 matrix = &shsurf->fullscreen.transform.matrix;
1691 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001692
Scott Moreau1bad5db2012-08-18 01:04:05 -06001693 output_aspect = (float) output->width /
1694 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001695 surface_aspect = (float) surface->geometry.width /
1696 (float) surface->geometry.height;
1697 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001698 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001699 (float) surface->geometry.width;
1700 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001701 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001702 (float) surface->geometry.height;
1703
Alex Wu4539b082012-03-01 12:57:46 +08001704 weston_matrix_scale(matrix, scale, scale, 1);
1705 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001706 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001707 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001708 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1709 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001710 weston_surface_set_position(surface, x, y);
1711
Alex Wu4539b082012-03-01 12:57:46 +08001712 break;
1713 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001714 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001715 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001716 surface->geometry.width,
1717 surface->geometry.height,
1718 shsurf->fullscreen.framerate};
1719
1720 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001721 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001722 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001723 output->width,
1724 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001725 weston_surface_set_position(surface, output->x, output->y);
1726 break;
1727 }
1728 }
Alex Wu4539b082012-03-01 12:57:46 +08001729 break;
1730 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1731 break;
1732 default:
1733 break;
1734 }
1735}
1736
1737/* make the fullscreen and black surface at the top */
1738static void
1739shell_stack_fullscreen(struct shell_surface *shsurf)
1740{
Alex Wubd3354b2012-04-17 17:20:49 +08001741 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001742 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001743 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001744
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001745 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001746 wl_list_insert(&shell->fullscreen_layer.surface_list,
1747 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001748 weston_surface_damage(surface);
1749
1750 if (!shsurf->fullscreen.black_surface)
1751 shsurf->fullscreen.black_surface =
1752 create_black_surface(surface->compositor,
1753 surface,
1754 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001755 output->width,
1756 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001757
1758 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001759 wl_list_insert(&surface->layer_link,
1760 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001761 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001762}
1763
1764static void
1765shell_map_fullscreen(struct shell_surface *shsurf)
1766{
Alex Wu4539b082012-03-01 12:57:46 +08001767 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001768 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001769}
1770
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001771static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001772set_fullscreen(struct shell_surface *shsurf,
1773 uint32_t method,
1774 uint32_t framerate,
1775 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001776{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001777 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001778
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001779 if (output)
1780 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001781 else if (es->output)
1782 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001783 else
1784 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001785
Alex Wu4539b082012-03-01 12:57:46 +08001786 shsurf->fullscreen_output = shsurf->output;
1787 shsurf->fullscreen.type = method;
1788 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001789 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001790
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001791 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001792 shsurf->output->width,
1793 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001794}
1795
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001796static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001797shell_surface_set_fullscreen(struct wl_client *client,
1798 struct wl_resource *resource,
1799 uint32_t method,
1800 uint32_t framerate,
1801 struct wl_resource *output_resource)
1802{
1803 struct shell_surface *shsurf = resource->data;
1804 struct weston_output *output;
1805
1806 if (output_resource)
1807 output = output_resource->data;
1808 else
1809 output = NULL;
1810
1811 set_fullscreen(shsurf, method, framerate, output);
1812}
1813
1814static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001815popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001816 struct wl_surface *surface,
1817 wl_fixed_t x,
1818 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001819{
Daniel Stone37816df2012-05-16 18:45:18 +01001820 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001821 struct shell_surface *priv =
1822 container_of(grab, struct shell_surface, popup.grab);
1823 struct wl_client *client = priv->surface->surface.resource.client;
1824
Pekka Paalanencb108432012-01-19 16:25:40 +02001825 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001826 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001827 grab->focus = surface;
1828 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001829 wl_pointer_set_focus(pointer, NULL,
1830 wl_fixed_from_int(0),
1831 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001832 grab->focus = NULL;
1833 }
1834}
1835
1836static void
Scott Moreau447013d2012-02-18 05:05:29 -07001837popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001838 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001839{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001840 struct wl_resource *resource;
1841
Daniel Stone37816df2012-05-16 18:45:18 +01001842 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001844 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001845}
1846
1847static void
Scott Moreau447013d2012-02-18 05:05:29 -07001848popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001849 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001850{
1851 struct wl_resource *resource;
1852 struct shell_surface *shsurf =
1853 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001854 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001855 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001856 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001857
Daniel Stone37816df2012-05-16 18:45:18 +01001858 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001859 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001860 display = wl_client_get_display(resource->client);
1861 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001862 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001863 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001864 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001865 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001866 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001867 }
1868
Daniel Stone4dbadb12012-05-30 16:31:51 +01001869 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001870 shsurf->popup.initial_up = 1;
1871}
1872
Scott Moreau447013d2012-02-18 05:05:29 -07001873static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001874 popup_grab_focus,
1875 popup_grab_motion,
1876 popup_grab_button,
1877};
1878
1879static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001880popup_grab_end(struct wl_pointer *pointer)
1881{
1882 struct wl_pointer_grab *grab = pointer->grab;
1883 struct shell_surface *shsurf =
1884 container_of(grab, struct shell_surface, popup.grab);
1885
1886 if (pointer->grab->interface == &popup_grab_interface) {
1887 wl_shell_surface_send_popup_done(&shsurf->resource);
1888 wl_pointer_end_grab(grab->pointer);
1889 shsurf->popup.grab.pointer = NULL;
1890 }
1891}
1892
1893static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001894shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001895{
Daniel Stone37816df2012-05-16 18:45:18 +01001896 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001897 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001898 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001899
Giulio Camuffo8aa16172013-02-18 22:26:01 +01001900 /* Remove the old transform. We don't want to add it twice
1901 * otherwise Weston will go into an infinite loop when going
1902 * through the transforms. */
1903 if (!wl_list_empty(&shsurf->popup.parent_transform.link)) {
1904 wl_list_remove(&shsurf->popup.parent_transform.link);
1905 wl_list_init(&shsurf->popup.parent_transform.link);
1906 }
1907
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001908 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001909 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001910
Pekka Paalanen938269a2012-02-07 14:19:01 +02001911 weston_surface_update_transform(parent);
1912 if (parent->transform.enabled) {
1913 shsurf->popup.parent_transform.matrix =
1914 parent->transform.matrix;
1915 } else {
1916 /* construct x, y translation matrix */
1917 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001918 shsurf->popup.parent_transform.matrix.type =
1919 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001920 shsurf->popup.parent_transform.matrix.d[12] =
1921 parent->geometry.x;
1922 shsurf->popup.parent_transform.matrix.d[13] =
1923 parent->geometry.y;
1924 }
1925 wl_list_insert(es->geometry.transformation_list.prev,
1926 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001927
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001928 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001929 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1930 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001931
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001932 /* We don't require the grab to still be active, but if another
1933 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001934 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1935 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001936 } else {
1937 wl_shell_surface_send_popup_done(&shsurf->resource);
1938 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001939}
1940
1941static void
1942shell_surface_set_popup(struct wl_client *client,
1943 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001944 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001945 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001946 struct wl_resource *parent_resource,
1947 int32_t x, int32_t y, uint32_t flags)
1948{
1949 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001950
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001951 shsurf->type = SHELL_SURFACE_POPUP;
1952 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001953 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001954 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955 shsurf->popup.x = x;
1956 shsurf->popup.y = y;
1957}
1958
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001959static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001960 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001961 shell_surface_move,
1962 shell_surface_resize,
1963 shell_surface_set_toplevel,
1964 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001965 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001966 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001967 shell_surface_set_maximized,
1968 shell_surface_set_title,
1969 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001970};
1971
1972static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001973destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001974{
Daniel Stone37816df2012-05-16 18:45:18 +01001975 if (shsurf->popup.grab.pointer)
1976 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001977
Alex Wubd3354b2012-04-17 17:20:49 +08001978 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1979 shell_surface_is_top_fullscreen(shsurf)) {
1980 weston_output_switch_mode(shsurf->fullscreen_output,
1981 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001982 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001983
Alex Wuaa08e2d2012-03-05 11:01:40 +08001984 if (shsurf->fullscreen.black_surface)
1985 weston_surface_destroy(shsurf->fullscreen.black_surface);
1986
Alex Wubd3354b2012-04-17 17:20:49 +08001987 /* As destroy_resource() use wl_list_for_each_safe(),
1988 * we can always remove the listener.
1989 */
1990 wl_list_remove(&shsurf->surface_destroy_listener.link);
1991 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001992 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001993
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001994 wl_list_remove(&shsurf->link);
1995 free(shsurf);
1996}
1997
1998static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001999shell_destroy_shell_surface(struct wl_resource *resource)
2000{
2001 struct shell_surface *shsurf = resource->data;
2002
2003 destroy_shell_surface(shsurf);
2004}
2005
2006static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002007shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002008{
2009 struct shell_surface *shsurf = container_of(listener,
2010 struct shell_surface,
2011 surface_destroy_listener);
2012
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002013 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002014 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002015 } else {
2016 wl_signal_emit(&shsurf->resource.destroy_signal,
2017 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002018 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002019 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002020}
2021
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002022static void
2023shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2024
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002025static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002026get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002027{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002028 if (surface->configure == shell_surface_configure)
2029 return surface->private;
2030 else
2031 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002032}
2033
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002034static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002035create_shell_surface(void *shell, struct weston_surface *surface,
2036 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002037{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002038 struct shell_surface *shsurf;
2039
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002040 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002041 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002042 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002043 }
2044
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002045 shsurf = calloc(1, sizeof *shsurf);
2046 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002047 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002048 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002049 }
2050
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002051 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002052 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002053
Tiago Vignattibc052c92012-04-19 16:18:18 +03002054 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002055 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002056 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002057 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002058 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002059 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2060 shsurf->fullscreen.framerate = 0;
2061 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002062 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002063 wl_list_init(&shsurf->fullscreen.transform.link);
2064
Tiago Vignattibc052c92012-04-19 16:18:18 +03002065 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002066 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2067 wl_signal_add(&surface->surface.resource.destroy_signal,
2068 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069
2070 /* init link so its safe to always remove it in destroy_shell_surface */
2071 wl_list_init(&shsurf->link);
2072
Pekka Paalanen460099f2012-01-20 16:48:25 +02002073 /* empty when not in use */
2074 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002075 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002076
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002077 wl_list_init(&shsurf->workspace_transform.link);
Giulio Camuffo8aa16172013-02-18 22:26:01 +01002078 wl_list_init(&shsurf->popup.parent_transform.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002079
Pekka Paalanen98262232011-12-01 10:42:22 +02002080 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002081 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002083 shsurf->client = client;
2084
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002085 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002086}
2087
2088static void
2089shell_get_shell_surface(struct wl_client *client,
2090 struct wl_resource *resource,
2091 uint32_t id,
2092 struct wl_resource *surface_resource)
2093{
2094 struct weston_surface *surface = surface_resource->data;
2095 struct desktop_shell *shell = resource->data;
2096 struct shell_surface *shsurf;
2097
2098 if (get_shell_surface(surface)) {
2099 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002100 WL_DISPLAY_ERROR_INVALID_OBJECT,
2101 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002102 return;
2103 }
2104
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002105 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002106 if (!shsurf) {
2107 wl_resource_post_error(surface_resource,
2108 WL_DISPLAY_ERROR_INVALID_OBJECT,
2109 "surface->configure already set");
2110 return;
2111 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002112
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002113 shsurf->resource.destroy = shell_destroy_shell_surface;
2114 shsurf->resource.object.id = id;
2115 shsurf->resource.object.interface = &wl_shell_surface_interface;
2116 shsurf->resource.object.implementation =
2117 (void (**)(void)) &shell_surface_implementation;
2118 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002119
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002120 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002121}
2122
2123static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002124 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002125};
2126
Kristian Høgsberg07937562011-04-12 17:25:42 -04002127static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002128handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002129{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002130 struct desktop_shell *shell =
2131 container_of(proc, struct desktop_shell, screensaver.process);
2132 struct weston_output *output;
2133
Pekka Paalanen18027e52011-12-02 16:31:49 +02002134 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002135
2136 if (shell->locked)
2137 wl_list_for_each(output, &shell->compositor->output_list, link)
2138 if (output->set_dpms)
2139 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002140}
2141
2142static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002143launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002144{
2145 if (shell->screensaver.binding)
2146 return;
2147
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002148 if (!shell->screensaver.path)
2149 return;
2150
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002151 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002152 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002153 return;
2154 }
2155
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002156 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002157 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002158 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002159 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002160}
2161
2162static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002163terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002164{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002165 if (shell->screensaver.process.pid == 0)
2166 return;
2167
2168 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002169}
2170
2171static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002172configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002173{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002174 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002175
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002176 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2177 if (s->output == es->output && s != es) {
2178 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002179 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002180 }
2181 }
2182
2183 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002184 weston_surface_buffer_width(es),
2185 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002186
2187 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002188 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002189 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002190 }
2191}
2192
2193static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002194background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2195{
2196 struct desktop_shell *shell = es->private;
2197
2198 configure_static_surface(es, &shell->background_layer);
2199}
2200
2201static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002202desktop_shell_set_background(struct wl_client *client,
2203 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002204 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002205 struct wl_resource *surface_resource)
2206{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002207 struct desktop_shell *shell = resource->data;
2208 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002209
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002210 if (surface->configure) {
2211 wl_resource_post_error(surface_resource,
2212 WL_DISPLAY_ERROR_INVALID_OBJECT,
2213 "surface role already assigned");
2214 return;
2215 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002216
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002217 surface->configure = background_configure;
2218 surface->private = shell;
2219 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002220 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002221 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002222 surface->output->width,
2223 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002224}
2225
2226static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002227panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2228{
2229 struct desktop_shell *shell = es->private;
2230
2231 configure_static_surface(es, &shell->panel_layer);
2232}
2233
2234static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002235desktop_shell_set_panel(struct wl_client *client,
2236 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002237 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002238 struct wl_resource *surface_resource)
2239{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002240 struct desktop_shell *shell = resource->data;
2241 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002242
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002243 if (surface->configure) {
2244 wl_resource_post_error(surface_resource,
2245 WL_DISPLAY_ERROR_INVALID_OBJECT,
2246 "surface role already assigned");
2247 return;
2248 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002249
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002250 surface->configure = panel_configure;
2251 surface->private = shell;
2252 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002253 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002254 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002255 surface->output->width,
2256 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002257}
2258
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002259static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002260lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2261{
2262 struct desktop_shell *shell = surface->private;
2263
2264 center_on_output(surface, get_default_output(shell->compositor));
2265
2266 if (!weston_surface_is_mapped(surface)) {
2267 wl_list_insert(&shell->lock_layer.surface_list,
2268 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002269 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002270 weston_compositor_wake(shell->compositor);
2271 }
2272}
2273
2274static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002275handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002276{
Tiago Vignattibe143262012-04-16 17:31:41 +03002277 struct desktop_shell *shell =
2278 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002279
Martin Minarik6d118362012-06-07 18:01:59 +02002280 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002281 shell->lock_surface = NULL;
2282}
2283
2284static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002285desktop_shell_set_lock_surface(struct wl_client *client,
2286 struct wl_resource *resource,
2287 struct wl_resource *surface_resource)
2288{
Tiago Vignattibe143262012-04-16 17:31:41 +03002289 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002290 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002291
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002292 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002293
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002294 if (!shell->locked)
2295 return;
2296
Pekka Paalanen98262232011-12-01 10:42:22 +02002297 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002298
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002299 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2300 wl_signal_add(&surface_resource->destroy_signal,
2301 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002302
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002303 surface->configure = lock_surface_configure;
2304 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002305}
2306
2307static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002308resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002309{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002310 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002311
Pekka Paalanen77346a62011-11-30 16:26:35 +02002312 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002313
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002314 wl_list_remove(&shell->lock_layer.link);
2315 wl_list_insert(&shell->compositor->cursor_layer.link,
2316 &shell->fullscreen_layer.link);
2317 wl_list_insert(&shell->fullscreen_layer.link,
2318 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002319 if (shell->showing_input_panels) {
2320 wl_list_insert(&shell->panel_layer.link,
2321 &shell->input_panel_layer.link);
2322 wl_list_insert(&shell->input_panel_layer.link,
2323 &ws->layer.link);
2324 } else {
2325 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2326 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002327
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002328 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002329
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002330 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002331 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002332 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002333 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002334}
2335
2336static void
2337desktop_shell_unlock(struct wl_client *client,
2338 struct wl_resource *resource)
2339{
Tiago Vignattibe143262012-04-16 17:31:41 +03002340 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002341
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002342 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002343
2344 if (shell->locked)
2345 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002346}
2347
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002348static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002349desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002350 struct wl_resource *resource,
2351 struct wl_resource *surface_resource)
2352{
2353 struct desktop_shell *shell = resource->data;
2354
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002355 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002356}
2357
Kristian Høgsberg75840622011-09-06 13:48:16 -04002358static const struct desktop_shell_interface desktop_shell_implementation = {
2359 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002360 desktop_shell_set_panel,
2361 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002362 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002363 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002364};
2365
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002366static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002367get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002368{
2369 struct shell_surface *shsurf;
2370
2371 shsurf = get_shell_surface(surface);
2372 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002373 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002374 return shsurf->type;
2375}
2376
Kristian Høgsberg75840622011-09-06 13:48:16 -04002377static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002378move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002379{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002380 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002381 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002382 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002383
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002384 if (surface == NULL)
2385 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002386
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002387 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002388 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002389 return;
2390
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002391 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002392}
2393
2394static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002395resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002396{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002397 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002398 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002399 uint32_t edges = 0;
2400 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002401 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002402
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002403 if (surface == NULL)
2404 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002405
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002406 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002407 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002408 return;
2409
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002410 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002411 wl_fixed_to_int(seat->pointer->grab_x),
2412 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002413 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002414
Pekka Paalanen60921e52012-01-25 15:55:43 +02002415 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002416 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002417 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002418 edges |= 0;
2419 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002420 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002421
Pekka Paalanen60921e52012-01-25 15:55:43 +02002422 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002423 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002424 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002425 edges |= 0;
2426 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002427 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002428
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002429 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002430}
2431
2432static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002433surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002434 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002435{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002436 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002437 struct shell_surface *shsurf;
2438 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002439 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002440
2441 if (surface == NULL)
2442 return;
2443
2444 shsurf = get_shell_surface(surface);
2445 if (!shsurf)
2446 return;
2447
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002448 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002449
Scott Moreau02709af2012-05-22 01:54:10 -06002450 if (surface->alpha > 1.0)
2451 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002452 if (surface->alpha < step)
2453 surface->alpha = step;
2454
2455 surface->geometry.dirty = 1;
2456 weston_surface_damage(surface);
2457}
2458
2459static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002460do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002461 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002462{
Daniel Stone37816df2012-05-16 18:45:18 +01002463 struct weston_seat *ws = (struct weston_seat *) seat;
2464 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002465 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002466 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002467
2468 wl_list_for_each(output, &compositor->output_list, link) {
2469 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002470 wl_fixed_to_double(seat->pointer->x),
2471 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002472 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002473 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002474 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002475 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002476 increment = -output->zoom.increment;
2477 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002478 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002479 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002480 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002481 else
2482 increment = 0;
2483
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002484 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002485
Scott Moreaue6603982012-06-11 13:07:51 -06002486 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002487 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002488 else if (output->zoom.level > output->zoom.max_level)
2489 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002490 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002491 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002492 output->disable_planes++;
2493 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002494
Scott Moreaue6603982012-06-11 13:07:51 -06002495 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002496
Scott Moreau8dacaab2012-06-17 18:10:58 -06002497 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002498 }
2499 }
2500}
2501
Scott Moreauccbf29d2012-02-22 14:21:41 -07002502static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002503zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002504 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002505{
2506 do_zoom(seat, time, 0, axis, value);
2507}
2508
2509static void
2510zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2511 void *data)
2512{
2513 do_zoom(seat, time, key, 0, 0);
2514}
2515
2516static void
2517terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2518 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002519{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002520 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002521
Daniel Stone325fc2d2012-05-30 16:31:58 +01002522 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002523}
2524
2525static void
Scott Moreau447013d2012-02-18 05:05:29 -07002526rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002527 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002528{
2529 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002530 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002531 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002532 struct shell_surface *shsurf = rotate->base.shsurf;
2533 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002534 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002535
2536 if (!shsurf)
2537 return;
2538
2539 surface = shsurf->surface;
2540
2541 cx = 0.5f * surface->geometry.width;
2542 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002543
Daniel Stone37816df2012-05-16 18:45:18 +01002544 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2545 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002546 r = sqrtf(dx * dx + dy * dy);
2547
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002548 wl_list_remove(&shsurf->rotation.transform.link);
2549 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002550
2551 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002552 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002553 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002554
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002555 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002556 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002557
2558 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002559 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002560 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002561 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002562 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002563
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002564 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002565 &shsurf->surface->geometry.transformation_list,
2566 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002567 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002568 wl_list_init(&shsurf->rotation.transform.link);
2569 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002570 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002571 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002572
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002573 /* We need to adjust the position of the surface
2574 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002575 cposx = surface->geometry.x + cx;
2576 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002577 dposx = rotate->center.x - cposx;
2578 dposy = rotate->center.y - cposy;
2579 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002580 weston_surface_set_position(surface,
2581 surface->geometry.x + dposx,
2582 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002583 }
2584
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002585 /* Repaint implies weston_surface_update_transform(), which
2586 * lazily applies the damage due to rotation update.
2587 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002588 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002589}
2590
2591static void
Scott Moreau447013d2012-02-18 05:05:29 -07002592rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002593 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002594{
2595 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002596 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002597 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002598 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002599 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002600
Daniel Stone4dbadb12012-05-30 16:31:51 +01002601 if (pointer->button_count == 0 &&
2602 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002603 if (shsurf)
2604 weston_matrix_multiply(&shsurf->rotation.rotation,
2605 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002606 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002607 free(rotate);
2608 }
2609}
2610
Scott Moreau447013d2012-02-18 05:05:29 -07002611static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002612 noop_grab_focus,
2613 rotate_grab_motion,
2614 rotate_grab_button,
2615};
2616
2617static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002618surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002619{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002620 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002621 float dx, dy;
2622 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002623
Pekka Paalanen460099f2012-01-20 16:48:25 +02002624 rotate = malloc(sizeof *rotate);
2625 if (!rotate)
2626 return;
2627
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002628 weston_surface_to_global_float(surface->surface,
2629 surface->surface->geometry.width / 2,
2630 surface->surface->geometry.height / 2,
2631 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002632
Daniel Stone37816df2012-05-16 18:45:18 +01002633 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2634 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002635 r = sqrtf(dx * dx + dy * dy);
2636 if (r > 20.0f) {
2637 struct weston_matrix inverse;
2638
2639 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002640 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002641 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002642
2643 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002644 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002645 } else {
2646 weston_matrix_init(&surface->rotation.rotation);
2647 weston_matrix_init(&rotate->rotation);
2648 }
2649
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002650 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2651 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002652}
2653
2654static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002655rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2656 void *data)
2657{
2658 struct weston_surface *base_surface =
2659 (struct weston_surface *) seat->pointer->focus;
2660 struct shell_surface *surface;
2661
2662 if (base_surface == NULL)
2663 return;
2664
2665 surface = get_shell_surface(base_surface);
2666 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2667 return;
2668
2669 surface_rotate(surface, seat);
2670}
2671
2672static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002673lower_fullscreen_layer(struct desktop_shell *shell)
2674{
2675 struct workspace *ws;
2676 struct weston_surface *surface, *prev;
2677
2678 ws = get_current_workspace(shell);
2679 wl_list_for_each_reverse_safe(surface, prev,
2680 &shell->fullscreen_layer.surface_list,
2681 layer_link)
2682 weston_surface_restack(surface, &ws->layer.surface_list);
2683}
2684
2685static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002686activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002687 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002688{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002689 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002690 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002691
Daniel Stone37816df2012-05-16 18:45:18 +01002692 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002693
Jonas Ådahl8538b222012-08-29 22:13:03 +02002694 state = ensure_focus_state(shell, seat);
2695 if (state == NULL)
2696 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002697
2698 state->keyboard_focus = es;
2699 wl_list_remove(&state->surface_destroy_listener.link);
2700 wl_signal_add(&es->surface.resource.destroy_signal,
2701 &state->surface_destroy_listener);
2702
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002703 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002704 case SHELL_SURFACE_FULLSCREEN:
2705 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002706 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002707 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002708 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002709 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002710 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002711 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002712 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002713 }
2714}
2715
Alex Wu21858432012-04-01 20:13:08 +08002716/* no-op func for checking black surface */
2717static void
2718black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2719{
2720}
2721
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002722static bool
Alex Wu21858432012-04-01 20:13:08 +08002723is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2724{
2725 if (es->configure == black_surface_configure) {
2726 if (fs_surface)
2727 *fs_surface = (struct weston_surface *)es->private;
2728 return true;
2729 }
2730 return false;
2731}
2732
Kristian Høgsberg75840622011-09-06 13:48:16 -04002733static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002734click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002735 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002736{
Daniel Stone37816df2012-05-16 18:45:18 +01002737 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002738 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002739 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002740 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002741
Daniel Stone37816df2012-05-16 18:45:18 +01002742 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002743 if (!focus)
2744 return;
2745
Alex Wu21858432012-04-01 20:13:08 +08002746 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002747 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002748
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002749 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2750 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002751
Daniel Stone325fc2d2012-05-30 16:31:58 +01002752 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002753 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002754}
2755
2756static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002757lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002758{
Tiago Vignattibe143262012-04-16 17:31:41 +03002759 struct desktop_shell *shell =
2760 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002761 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002762 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002763
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002764 if (shell->locked) {
2765 wl_list_for_each(output, &shell->compositor->output_list, link)
2766 /* TODO: find a way to jump to other DPMS levels */
2767 if (output->set_dpms)
2768 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002769 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002770 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002771
2772 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002773
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002774 /* Hide all surfaces by removing the fullscreen, panel and
2775 * toplevel layers. This way nothing else can show or receive
2776 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002777
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002778 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002779 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002780 if (shell->showing_input_panels)
2781 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002782 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002783 wl_list_insert(&shell->compositor->cursor_layer.link,
2784 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002785
Pekka Paalanen77346a62011-11-30 16:26:35 +02002786 launch_screensaver(shell);
2787
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002788 /* TODO: disable bindings that should not work while locked. */
2789
2790 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002791}
2792
2793static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002794unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002795{
Tiago Vignattibe143262012-04-16 17:31:41 +03002796 struct desktop_shell *shell =
2797 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002798
Pekka Paalanend81c2162011-11-16 13:47:34 +02002799 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002800 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002801 return;
2802 }
2803
2804 /* If desktop-shell client has gone away, unlock immediately. */
2805 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002806 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002807 return;
2808 }
2809
2810 if (shell->prepare_event_sent)
2811 return;
2812
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002813 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002814 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002815}
2816
2817static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002818show_input_panels(struct wl_listener *listener, void *data)
2819{
2820 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002821 container_of(listener, struct desktop_shell,
2822 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002823 struct input_panel_surface *surface, *next;
2824 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002825
Jan Arne Petersen451a9712013-02-11 15:10:11 +01002826 if (shell->showing_input_panels)
2827 return;
2828
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002829 shell->showing_input_panels = true;
2830
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002831 if (!shell->locked)
2832 wl_list_insert(&shell->panel_layer.link,
2833 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002834
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002835 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002836 &shell->input_panel.surfaces, link) {
2837 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002838 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002839 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002840 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002841 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002842 weston_surface_damage(ws);
2843 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002844 }
2845}
2846
2847static void
2848hide_input_panels(struct wl_listener *listener, void *data)
2849{
2850 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002851 container_of(listener, struct desktop_shell,
2852 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002853 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002854
Jan Arne Petersen61381972013-01-31 15:52:21 +01002855 if (!shell->showing_input_panels)
2856 return;
2857
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002858 shell->showing_input_panels = false;
2859
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002860 if (!shell->locked)
2861 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002862
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002863 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002864 &shell->input_panel_layer.surface_list, layer_link)
2865 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002866}
2867
2868static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002869center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002870{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002871 int32_t width = weston_surface_buffer_width(surface);
2872 int32_t height = weston_surface_buffer_height(surface);
2873 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002874
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002875 x = output->x + (output->width - width) / 2;
2876 y = output->y + (output->height - height) / 2;
2877
2878 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002879}
2880
2881static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002882weston_surface_set_initial_position (struct weston_surface *surface,
2883 struct desktop_shell *shell)
2884{
2885 struct weston_compositor *compositor = shell->compositor;
2886 int ix = 0, iy = 0;
2887 int range_x, range_y;
2888 int dx, dy, x, y, panel_height;
2889 struct weston_output *output, *target_output = NULL;
2890 struct weston_seat *seat;
2891
2892 /* As a heuristic place the new window on the same output as the
2893 * pointer. Falling back to the output containing 0, 0.
2894 *
2895 * TODO: Do something clever for touch too?
2896 */
2897 wl_list_for_each(seat, &compositor->seat_list, link) {
2898 if (seat->has_pointer) {
2899 ix = wl_fixed_to_int(seat->pointer.x);
2900 iy = wl_fixed_to_int(seat->pointer.y);
2901 break;
2902 }
2903 }
2904
2905 wl_list_for_each(output, &compositor->output_list, link) {
2906 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2907 target_output = output;
2908 break;
2909 }
2910 }
2911
2912 if (!target_output) {
2913 weston_surface_set_position(surface, 10 + random() % 400,
2914 10 + random() % 400);
2915 return;
2916 }
2917
2918 /* Valid range within output where the surface will still be onscreen.
2919 * If this is negative it means that the surface is bigger than
2920 * output.
2921 */
2922 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002923 range_x = target_output->width - surface->geometry.width;
2924 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002925 surface->geometry.height;
2926
Rob Bradford4cb88c72012-08-13 15:18:44 +01002927 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002928 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002929 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002930 dx = 0;
2931
2932 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002933 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002934 else
2935 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002936
2937 x = target_output->x + dx;
2938 y = target_output->y + dy;
2939
2940 weston_surface_set_position (surface, x, y);
2941}
2942
2943static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002944map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002945 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002947 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002948 struct shell_surface *shsurf = get_shell_surface(surface);
2949 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002950 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002951 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002952 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002953 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002954
Pekka Paalanen60921e52012-01-25 15:55:43 +02002955 surface->geometry.width = width;
2956 surface->geometry.height = height;
2957 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002958
2959 /* initial positioning, see also configure() */
2960 switch (surface_type) {
2961 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002962 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002963 break;
Alex Wu4539b082012-03-01 12:57:46 +08002964 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002965 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002966 shell_map_fullscreen(shsurf);
2967 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002968 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002969 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002970 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002971 weston_surface_set_position(surface, shsurf->output->x,
2972 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002973 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002974 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002975 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002976 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002977 case SHELL_SURFACE_NONE:
2978 weston_surface_set_position(surface,
2979 surface->geometry.x + sx,
2980 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002981 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002982 default:
2983 ;
2984 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002985
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002986 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002987 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002988 case SHELL_SURFACE_POPUP:
2989 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002990 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002991 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2992 break;
Alex Wu4539b082012-03-01 12:57:46 +08002993 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002994 case SHELL_SURFACE_NONE:
2995 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002996 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002997 ws = get_current_workspace(shell);
2998 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002999 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003000 }
3001
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003002 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003003 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003004 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3005 surface->output = shsurf->output;
3006 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003007
Juan Zhao7bb92f02011-12-15 11:31:51 -05003008 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003009 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003010 if (shsurf->transient.flags ==
3011 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3012 break;
3013 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003014 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003015 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003016 if (!shell->locked) {
3017 wl_list_for_each(seat, &compositor->seat_list, link)
3018 activate(shell, surface, seat);
3019 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003020 break;
3021 default:
3022 break;
3023 }
3024
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003025 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003026 {
3027 switch (shell->win_animation_type) {
3028 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003029 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003030 break;
3031 case ANIMATION_ZOOM:
3032 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3033 break;
3034 default:
3035 break;
3036 }
3037 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003038}
3039
3040static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003041configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003042 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003043{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003044 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3045 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003046
Pekka Paalanen77346a62011-11-30 16:26:35 +02003047 shsurf = get_shell_surface(surface);
3048 if (shsurf)
3049 surface_type = shsurf->type;
3050
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003051 surface->geometry.x = x;
3052 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003053 surface->geometry.width = width;
3054 surface->geometry.height = height;
3055 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003056
3057 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003058 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003059 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003060 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003061 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003062 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003063 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003064 surface->geometry.x = surface->output->x;
3065 surface->geometry.y = surface->output->y +
3066 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003067 break;
Alex Wu4539b082012-03-01 12:57:46 +08003068 case SHELL_SURFACE_TOPLEVEL:
3069 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003070 default:
3071 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003072 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003073
Alex Wu4539b082012-03-01 12:57:46 +08003074 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003075 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003076 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003077
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003078 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003079 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003080 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003081}
3082
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003083static void
3084shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3085{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003086 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003087 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003088 int32_t width = weston_surface_buffer_width(es);
3089 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003090 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003091
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003092 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003093 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003094 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003095 type_changed = 1;
3096 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003097
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003098 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003099 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003100 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003101 es->geometry.width != width ||
3102 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003103 float from_x, from_y;
3104 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003105
3106 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3107 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3108 configure(shell, es,
3109 es->geometry.x + to_x - from_x,
3110 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003111 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003112 }
3113}
3114
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003115static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003116
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003117static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003118desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003119{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003120 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003121 struct desktop_shell *shell =
3122 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003123
3124 shell->child.process.pid = 0;
3125 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003126
3127 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3128 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003129 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003130 shell->child.deathstamp = time;
3131 shell->child.deathcount = 0;
3132 }
3133
3134 shell->child.deathcount++;
3135 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003136 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003137 return;
3138 }
3139
Martin Minarik6d118362012-06-07 18:01:59 +02003140 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003141 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003142}
3143
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003144static void
3145launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003146{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003147 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003148 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003149
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003150 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003151 &shell->child.process,
3152 shell_exe,
3153 desktop_shell_sigchld);
3154
3155 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003156 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003157}
3158
3159static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003160bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3161{
Tiago Vignattibe143262012-04-16 17:31:41 +03003162 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003163
3164 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003165 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003166}
3167
Kristian Høgsberg75840622011-09-06 13:48:16 -04003168static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003169unbind_desktop_shell(struct wl_resource *resource)
3170{
Tiago Vignattibe143262012-04-16 17:31:41 +03003171 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003172
3173 if (shell->locked)
3174 resume_desktop(shell);
3175
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003176 shell->child.desktop_shell = NULL;
3177 shell->prepare_event_sent = false;
3178 free(resource);
3179}
3180
3181static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003182bind_desktop_shell(struct wl_client *client,
3183 void *data, uint32_t version, uint32_t id)
3184{
Tiago Vignattibe143262012-04-16 17:31:41 +03003185 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003186 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003187
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003188 resource = wl_client_add_object(client, &desktop_shell_interface,
3189 &desktop_shell_implementation,
3190 id, shell);
3191
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003192 if (client == shell->child.client) {
3193 resource->destroy = unbind_desktop_shell;
3194 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003195 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003196 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003197
3198 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3199 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003200 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003201}
3202
Pekka Paalanen6e168112011-11-24 11:34:05 +02003203static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003204screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3205{
3206 struct desktop_shell *shell = surface->private;
3207
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003208 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003209 if (!shell->locked)
3210 return;
3211
3212 center_on_output(surface, surface->output);
3213
3214 if (wl_list_empty(&surface->layer_link)) {
3215 wl_list_insert(shell->lock_layer.surface_list.prev,
3216 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003217 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003218 shell->compositor->idle_time = shell->screensaver.duration;
3219 weston_compositor_wake(shell->compositor);
3220 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3221 }
3222}
3223
3224static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003225screensaver_set_surface(struct wl_client *client,
3226 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003227 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003228 struct wl_resource *output_resource)
3229{
Tiago Vignattibe143262012-04-16 17:31:41 +03003230 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003231 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003232 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003233
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003234 surface->configure = screensaver_configure;
3235 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003236 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003237}
3238
3239static const struct screensaver_interface screensaver_implementation = {
3240 screensaver_set_surface
3241};
3242
3243static void
3244unbind_screensaver(struct wl_resource *resource)
3245{
Tiago Vignattibe143262012-04-16 17:31:41 +03003246 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003247
Pekka Paalanen77346a62011-11-30 16:26:35 +02003248 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003249 free(resource);
3250}
3251
3252static void
3253bind_screensaver(struct wl_client *client,
3254 void *data, uint32_t version, uint32_t id)
3255{
Tiago Vignattibe143262012-04-16 17:31:41 +03003256 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003257 struct wl_resource *resource;
3258
3259 resource = wl_client_add_object(client, &screensaver_interface,
3260 &screensaver_implementation,
3261 id, shell);
3262
Pekka Paalanen77346a62011-11-30 16:26:35 +02003263 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003264 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003265 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003266 return;
3267 }
3268
3269 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3270 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003271 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003272}
3273
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003274static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003275input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3276{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003277 struct weston_mode *mode;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003278 int32_t width = weston_surface_buffer_width(surface);
3279 int32_t height = weston_surface_buffer_height(surface);
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003280 float x, y;
3281
3282 if (!weston_surface_is_mapped(surface))
3283 return;
3284
3285 mode = surface->output->current;
3286 x = (mode->width - width) / 2;
3287 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003288
3289 /* Don't map the input panel here, wait for
3290 * show_input_panels signal. */
3291
3292 weston_surface_configure(surface,
3293 surface->output->x + x,
3294 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003295 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003296}
3297
3298static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003299destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003300{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003301 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003302 wl_list_remove(&input_panel_surface->link);
3303
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003304 input_panel_surface->surface->configure = NULL;
3305
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003306 free(input_panel_surface);
3307}
3308
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003309static struct input_panel_surface *
3310get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003311{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003312 if (surface->configure == input_panel_configure) {
3313 return surface->private;
3314 } else {
3315 return NULL;
3316 }
3317}
3318
3319static void
3320input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3321{
3322 struct input_panel_surface *ipsurface = container_of(listener,
3323 struct input_panel_surface,
3324 surface_destroy_listener);
3325
3326 if (ipsurface->resource.client) {
3327 wl_resource_destroy(&ipsurface->resource);
3328 } else {
3329 wl_signal_emit(&ipsurface->resource.destroy_signal,
3330 &ipsurface->resource);
3331 destroy_input_panel_surface(ipsurface);
3332 }
3333}
3334static struct input_panel_surface *
3335create_input_panel_surface(struct desktop_shell *shell,
3336 struct weston_surface *surface)
3337{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003338 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003339
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003340 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3341 if (!input_panel_surface)
3342 return NULL;
3343
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003344 surface->configure = input_panel_configure;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003345 surface->private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003346
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003347 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003348
3349 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003350
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003351 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3352 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3353 wl_signal_add(&surface->surface.resource.destroy_signal,
3354 &input_panel_surface->surface_destroy_listener);
3355
3356 wl_list_init(&input_panel_surface->link);
3357
3358 return input_panel_surface;
3359}
3360
3361static void
3362input_panel_surface_set_toplevel(struct wl_client *client,
3363 struct wl_resource *resource,
3364 uint32_t position)
3365{
3366 struct input_panel_surface *input_panel_surface = resource->data;
3367 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003368
3369 wl_list_insert(&shell->input_panel.surfaces,
3370 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003371}
3372
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003373static const struct input_panel_surface_interface input_panel_surface_implementation = {
3374 input_panel_surface_set_toplevel
3375};
3376
3377static void
3378destroy_input_panel_surface_resource(struct wl_resource *resource)
3379{
3380 struct input_panel_surface *ipsurf = resource->data;
3381
3382 destroy_input_panel_surface(ipsurf);
3383}
3384
3385static void
3386input_panel_get_input_panel_surface(struct wl_client *client,
3387 struct wl_resource *resource,
3388 uint32_t id,
3389 struct wl_resource *surface_resource)
3390{
3391 struct weston_surface *surface = surface_resource->data;
3392 struct desktop_shell *shell = resource->data;
3393 struct input_panel_surface *ipsurf;
3394
3395 if (get_input_panel_surface(surface)) {
3396 wl_resource_post_error(surface_resource,
3397 WL_DISPLAY_ERROR_INVALID_OBJECT,
3398 "input_panel::get_input_panel_surface already requested");
3399 return;
3400 }
3401
3402 ipsurf = create_input_panel_surface(shell, surface);
3403 if (!ipsurf) {
3404 wl_resource_post_error(surface_resource,
3405 WL_DISPLAY_ERROR_INVALID_OBJECT,
3406 "surface->configure already set");
3407 return;
3408 }
3409
3410 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3411 ipsurf->resource.object.id = id;
3412 ipsurf->resource.object.interface = &input_panel_surface_interface;
3413 ipsurf->resource.object.implementation =
3414 (void (**)(void)) &input_panel_surface_implementation;
3415 ipsurf->resource.data = ipsurf;
3416
3417 wl_client_add_resource(client, &ipsurf->resource);
3418}
3419
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003420static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003421 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003422};
3423
3424static void
3425unbind_input_panel(struct wl_resource *resource)
3426{
3427 struct desktop_shell *shell = resource->data;
3428
3429 shell->input_panel.binding = NULL;
3430 free(resource);
3431}
3432
3433static void
3434bind_input_panel(struct wl_client *client,
3435 void *data, uint32_t version, uint32_t id)
3436{
3437 struct desktop_shell *shell = data;
3438 struct wl_resource *resource;
3439
3440 resource = wl_client_add_object(client, &input_panel_interface,
3441 &input_panel_implementation,
3442 id, shell);
3443
3444 if (shell->input_panel.binding == NULL) {
3445 resource->destroy = unbind_input_panel;
3446 shell->input_panel.binding = resource;
3447 return;
3448 }
3449
3450 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3451 "interface object already bound");
3452 wl_resource_destroy(resource);
3453}
3454
Kristian Høgsberg07045392012-02-19 18:52:44 -05003455struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003456 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003457 struct weston_surface *current;
3458 struct wl_listener listener;
3459 struct wl_keyboard_grab grab;
3460};
3461
3462static void
3463switcher_next(struct switcher *switcher)
3464{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003465 struct weston_surface *surface;
3466 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003467 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003468 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003469
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003470 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003471 switch (get_shell_surface_type(surface)) {
3472 case SHELL_SURFACE_TOPLEVEL:
3473 case SHELL_SURFACE_FULLSCREEN:
3474 case SHELL_SURFACE_MAXIMIZED:
3475 if (first == NULL)
3476 first = surface;
3477 if (prev == switcher->current)
3478 next = surface;
3479 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003480 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003481 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003482 weston_surface_damage(surface);
3483 break;
3484 default:
3485 break;
3486 }
Alex Wu1659daa2012-04-01 20:13:09 +08003487
3488 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003489 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003490 surface->geometry.dirty = 1;
3491 weston_surface_damage(surface);
3492 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003493 }
3494
3495 if (next == NULL)
3496 next = first;
3497
Alex Wu07b26062012-03-12 16:06:01 +08003498 if (next == NULL)
3499 return;
3500
Kristian Høgsberg07045392012-02-19 18:52:44 -05003501 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003502 wl_signal_add(&next->surface.resource.destroy_signal,
3503 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003504
3505 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003506 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003507
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003508 shsurf = get_shell_surface(switcher->current);
3509 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003510 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003511}
3512
3513static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003514switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003515{
3516 struct switcher *switcher =
3517 container_of(listener, struct switcher, listener);
3518
3519 switcher_next(switcher);
3520}
3521
3522static void
Daniel Stone351eb612012-05-31 15:27:47 -04003523switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003524{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003525 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003526 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003527 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003528 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003529
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003530 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003531 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003532 weston_surface_damage(surface);
3533 }
3534
Alex Wu07b26062012-03-12 16:06:01 +08003535 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003536 activate(switcher->shell, switcher->current,
3537 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003538 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003539 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003540 if (weston_keyboard->input_method_resource)
3541 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003542 free(switcher);
3543}
3544
3545static void
3546switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003547 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003548{
3549 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003550 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003551
Daniel Stonec9785ea2012-05-30 16:31:52 +01003552 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003553 switcher_next(switcher);
3554}
3555
3556static void
3557switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3558 uint32_t mods_depressed, uint32_t mods_latched,
3559 uint32_t mods_locked, uint32_t group)
3560{
3561 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003562 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003563
Daniel Stone351eb612012-05-31 15:27:47 -04003564 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3565 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003566}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003567
3568static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003569 switcher_key,
3570 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003571};
3572
3573static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003574switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3575 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003576{
Tiago Vignattibe143262012-04-16 17:31:41 +03003577 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003578 struct switcher *switcher;
3579
3580 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003581 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003582 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003583 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003584 wl_list_init(&switcher->listener.link);
3585
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003586 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003587 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003588 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3589 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003590 switcher_next(switcher);
3591}
3592
Pekka Paalanen3c647232011-12-22 13:43:43 +02003593static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003594backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3595 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003596{
3597 struct weston_compositor *compositor = data;
3598 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003599 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003600
3601 /* TODO: we're limiting to simple use cases, where we assume just
3602 * control on the primary display. We'd have to extend later if we
3603 * ever get support for setting backlights on random desktop LCD
3604 * panels though */
3605 output = get_default_output(compositor);
3606 if (!output)
3607 return;
3608
3609 if (!output->set_backlight)
3610 return;
3611
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003612 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3613 backlight_new = output->backlight_current - 25;
3614 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3615 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003616
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003617 if (backlight_new < 5)
3618 backlight_new = 5;
3619 if (backlight_new > 255)
3620 backlight_new = 255;
3621
3622 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003623 output->set_backlight(output, output->backlight_current);
3624}
3625
3626static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003627fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3628 void *data)
3629{
3630 struct desktop_shell *shell = data;
3631 struct weston_compositor *compositor = shell->compositor;
3632 compositor->fan_debug = !compositor->fan_debug;
3633 weston_compositor_damage_all(compositor);
3634}
3635
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003636struct debug_binding_grab {
3637 struct wl_keyboard_grab grab;
3638 struct weston_seat *seat;
3639 uint32_t key[2];
3640 int key_released[2];
3641};
3642
3643static void
3644debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3645 uint32_t key, uint32_t state)
3646{
3647 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3648 struct wl_resource *resource;
3649 struct wl_display *display;
3650 uint32_t serial;
3651 int send = 0, terminate = 0;
3652 int check_binding = 1;
3653 int i;
3654
3655 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3656 /* Do not run bindings on key releases */
3657 check_binding = 0;
3658
3659 for (i = 0; i < 2; i++)
3660 if (key == db->key[i])
3661 db->key_released[i] = 1;
3662
3663 if (db->key_released[0] && db->key_released[1]) {
3664 /* All key releases been swalled so end the grab */
3665 terminate = 1;
3666 } else if (key != db->key[0] && key != db->key[1]) {
3667 /* Should not swallow release of other keys */
3668 send = 1;
3669 }
3670 } else if (key == db->key[0] && !db->key_released[0]) {
3671 /* Do not check bindings for the first press of the binding
3672 * key. This allows it to be used as a debug shortcut.
3673 * We still need to swallow this event. */
3674 check_binding = 0;
3675 } else if (db->key[1]) {
3676 /* If we already ran a binding don't process another one since
3677 * we can't keep track of all the binding keys that were
3678 * pressed in order to swallow the release events. */
3679 send = 1;
3680 check_binding = 0;
3681 }
3682
3683 if (check_binding) {
3684 struct weston_compositor *ec = db->seat->compositor;
3685
3686 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3687 key, state)) {
3688 /* We ran a binding so swallow the press and keep the
3689 * grab to swallow the released too. */
3690 send = 0;
3691 terminate = 0;
3692 db->key[1] = key;
3693 } else {
3694 /* Terminate the grab since the key pressed is not a
3695 * debug binding key. */
3696 send = 1;
3697 terminate = 1;
3698 }
3699 }
3700
3701 if (send) {
3702 resource = grab->keyboard->focus_resource;
3703
3704 if (resource) {
3705 display = wl_client_get_display(resource->client);
3706 serial = wl_display_next_serial(display);
3707 wl_keyboard_send_key(resource, serial, time, key, state);
3708 }
3709 }
3710
3711 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003712 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003713 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003714 if (weston_keyboard->input_method_resource)
3715 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003716 free(db);
3717 }
3718}
3719
3720static void
3721debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3722 uint32_t mods_depressed, uint32_t mods_latched,
3723 uint32_t mods_locked, uint32_t group)
3724{
3725 struct wl_resource *resource;
3726
3727 resource = grab->keyboard->focus_resource;
3728 if (!resource)
3729 return;
3730
3731 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3732 mods_latched, mods_locked, group);
3733}
3734
3735struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3736 debug_binding_key,
3737 debug_binding_modifiers
3738};
3739
3740static void
3741debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3742{
3743 struct debug_binding_grab *grab;
3744
3745 grab = calloc(1, sizeof *grab);
3746 if (!grab)
3747 return;
3748
3749 grab->seat = (struct weston_seat *) seat;
3750 grab->key[0] = key;
3751 grab->grab.interface = &debug_binding_keyboard_grab;
3752 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3753}
3754
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003755static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003756force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3757 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003758{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003759 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003760 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003761 struct desktop_shell *shell = data;
3762 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003763 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003764
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003765 focus_surface = seat->keyboard->focus;
3766 if (!focus_surface)
3767 return;
3768
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003769 wl_signal_emit(&compositor->kill_signal, focus_surface);
3770
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003771 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003772 wl_client_get_credentials(client, &pid, NULL, NULL);
3773
3774 /* Skip clients that we launched ourselves (the credentials of
3775 * the socketpair is ours) */
3776 if (pid == getpid())
3777 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003778
Daniel Stone325fc2d2012-05-30 16:31:58 +01003779 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003780}
3781
3782static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003783workspace_up_binding(struct wl_seat *seat, uint32_t time,
3784 uint32_t key, void *data)
3785{
3786 struct desktop_shell *shell = data;
3787 unsigned int new_index = shell->workspaces.current;
3788
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003789 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003790 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003791 if (new_index != 0)
3792 new_index--;
3793
3794 change_workspace(shell, new_index);
3795}
3796
3797static void
3798workspace_down_binding(struct wl_seat *seat, uint32_t time,
3799 uint32_t key, void *data)
3800{
3801 struct desktop_shell *shell = data;
3802 unsigned int new_index = shell->workspaces.current;
3803
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003804 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003805 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003806 if (new_index < shell->workspaces.num - 1)
3807 new_index++;
3808
3809 change_workspace(shell, new_index);
3810}
3811
3812static void
3813workspace_f_binding(struct wl_seat *seat, uint32_t time,
3814 uint32_t key, void *data)
3815{
3816 struct desktop_shell *shell = data;
3817 unsigned int new_index;
3818
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003819 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003820 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003821 new_index = key - KEY_F1;
3822 if (new_index >= shell->workspaces.num)
3823 new_index = shell->workspaces.num - 1;
3824
3825 change_workspace(shell, new_index);
3826}
3827
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003828static void
3829workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3830 uint32_t key, void *data)
3831{
3832 struct desktop_shell *shell = data;
3833 unsigned int new_index = shell->workspaces.current;
3834
3835 if (shell->locked)
3836 return;
3837
3838 if (new_index != 0)
3839 new_index--;
3840
3841 take_surface_to_workspace_by_seat(shell, seat, new_index);
3842}
3843
3844static void
3845workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3846 uint32_t key, void *data)
3847{
3848 struct desktop_shell *shell = data;
3849 unsigned int new_index = shell->workspaces.current;
3850
3851 if (shell->locked)
3852 return;
3853
3854 if (new_index < shell->workspaces.num - 1)
3855 new_index++;
3856
3857 take_surface_to_workspace_by_seat(shell, seat, new_index);
3858}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003859
3860static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003861shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003862{
Tiago Vignattibe143262012-04-16 17:31:41 +03003863 struct desktop_shell *shell =
3864 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003865 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003866
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003867 if (shell->child.client)
3868 wl_client_destroy(shell->child.client);
3869
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003870 wl_list_remove(&shell->lock_listener.link);
3871 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003872 wl_list_remove(&shell->show_input_panel_listener.link);
3873 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003874
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003875 wl_array_for_each(ws, &shell->workspaces.array)
3876 workspace_destroy(*ws);
3877 wl_array_release(&shell->workspaces.array);
3878
Pekka Paalanen3c647232011-12-22 13:43:43 +02003879 free(shell->screensaver.path);
3880 free(shell);
3881}
3882
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003883static void
3884shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3885{
3886 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003887 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003888
3889 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003890 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3891 MODIFIER_CTRL | MODIFIER_ALT,
3892 terminate_binding, ec);
3893 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3894 click_to_activate_binding,
3895 shell);
3896 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3897 MODIFIER_SUPER | MODIFIER_ALT,
3898 surface_opacity_binding, NULL);
3899 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3900 MODIFIER_SUPER, zoom_axis_binding,
3901 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003902
3903 /* configurable bindings */
3904 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003905 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3906 zoom_key_binding, NULL);
3907 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3908 zoom_key_binding, NULL);
3909 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3910 shell);
3911 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3912 resize_binding, shell);
3913 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3914 rotate_binding, NULL);
3915 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3916 shell);
3917 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3918 ec);
3919 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3920 backlight_binding, ec);
3921 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3922 ec);
3923 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3924 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003925 weston_compositor_add_key_binding(ec, KEY_K, mod,
3926 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003927 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3928 workspace_up_binding, shell);
3929 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3930 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003931 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3932 workspace_move_surface_up_binding,
3933 shell);
3934 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3935 workspace_move_surface_down_binding,
3936 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003937
3938 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3939 if (shell->workspaces.num > 1) {
3940 num_workspace_bindings = shell->workspaces.num;
3941 if (num_workspace_bindings > 6)
3942 num_workspace_bindings = 6;
3943 for (i = 0; i < num_workspace_bindings; i++)
3944 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3945 workspace_f_binding,
3946 shell);
3947 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003948
3949 /* Debug bindings */
3950 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3951 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003952 weston_compositor_add_debug_binding(ec, KEY_F,
3953 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003954}
3955
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003956WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003957module_init(struct weston_compositor *ec,
3958 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003959{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003960 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003961 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003962 struct workspace **pws;
3963 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003964 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003965
3966 shell = malloc(sizeof *shell);
3967 if (shell == NULL)
3968 return -1;
3969
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003970 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003971 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003972
3973 shell->destroy_listener.notify = shell_destroy;
3974 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3975 shell->lock_listener.notify = lock;
3976 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3977 shell->unlock_listener.notify = unlock;
3978 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003979 shell->show_input_panel_listener.notify = show_input_panels;
3980 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3981 shell->hide_input_panel_listener.notify = hide_input_panels;
3982 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003983 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003984 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003985 ec->shell_interface.create_shell_surface = create_shell_surface;
3986 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003987 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003988 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003989 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003990 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003991
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003992 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003993
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003994 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3995 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003996 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3997 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003998 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003999
4000 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004001 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004002
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004003 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004004
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004005 for (i = 0; i < shell->workspaces.num; i++) {
4006 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4007 if (pws == NULL)
4008 return -1;
4009
4010 *pws = workspace_create();
4011 if (*pws == NULL)
4012 return -1;
4013 }
4014 activate_workspace(shell, 0);
4015
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004016 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004017 wl_list_init(&shell->workspaces.animation.link);
4018 shell->workspaces.animation.frame = animate_workspace_change_frame;
4019
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004020 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4021 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004022 return -1;
4023
Kristian Høgsberg75840622011-09-06 13:48:16 -04004024 if (wl_display_add_global(ec->wl_display,
4025 &desktop_shell_interface,
4026 shell, bind_desktop_shell) == NULL)
4027 return -1;
4028
Pekka Paalanen6e168112011-11-24 11:34:05 +02004029 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4030 shell, bind_screensaver) == NULL)
4031 return -1;
4032
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004033 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4034 shell, bind_input_panel) == NULL)
4035 return -1;
4036
Jonas Ådahle9d22502012-08-29 22:13:01 +02004037 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4038 shell, bind_workspace_manager) == NULL)
4039 return -1;
4040
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004041 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004042
4043 loop = wl_display_get_event_loop(ec->wl_display);
4044 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004045
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004046 wl_list_for_each(seat, &ec->seat_list, link)
4047 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004048
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004049 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004050
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004051 return 0;
4052}