blob: af802a57772b318de802851f82a8dd53be8b2560 [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
Tiago Vignattibe143262012-04-16 17:31:41 +0300348shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200349{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200350 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200351 char *path = NULL;
352 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200353 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300354 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800355 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200356
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400357 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300358 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800359 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200360 { "num-workspaces",
361 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200362 };
363
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400364 struct config_key saver_keys[] = {
365 { "path", CONFIG_KEY_STRING, &path },
366 { "duration", CONFIG_KEY_INTEGER, &duration },
367 };
368
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200369 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400370 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200371 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
372 };
373
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200374 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500375 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200376 free(config_file);
377
Pekka Paalanen7296e792011-12-07 16:22:00 +0200378 shell->screensaver.path = path;
379 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300380 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800381 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200382 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
383}
384
385static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200386focus_state_destroy(struct focus_state *state)
387{
388 wl_list_remove(&state->seat_destroy_listener.link);
389 wl_list_remove(&state->surface_destroy_listener.link);
390 free(state);
391}
392
393static void
394focus_state_seat_destroy(struct wl_listener *listener, void *data)
395{
396 struct focus_state *state = container_of(listener,
397 struct focus_state,
398 seat_destroy_listener);
399
400 wl_list_remove(&state->link);
401 focus_state_destroy(state);
402}
403
404static void
405focus_state_surface_destroy(struct wl_listener *listener, void *data)
406{
407 struct focus_state *state = container_of(listener,
408 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400409 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400410 struct desktop_shell *shell;
411 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200412
Kristian Høgsberge3778222012-07-31 17:29:30 -0400413 next = NULL;
414 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
415 if (surface == state->keyboard_focus)
416 continue;
417
418 next = surface;
419 break;
420 }
421
422 if (next) {
423 shell = state->seat->compositor->shell_interface.shell;
424 activate(shell, next, state->seat);
425 } else {
426 wl_list_remove(&state->link);
427 focus_state_destroy(state);
428 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200429}
430
431static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400432focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200433{
Jonas Ådahl04769742012-06-13 00:01:24 +0200434 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200435
436 state = malloc(sizeof *state);
437 if (state == NULL)
438 return NULL;
439
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400440 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200441 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400442 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200443
444 state->seat_destroy_listener.notify = focus_state_seat_destroy;
445 state->surface_destroy_listener.notify = focus_state_surface_destroy;
446 wl_signal_add(&seat->seat.destroy_signal,
447 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400448 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200449
450 return state;
451}
452
Jonas Ådahl8538b222012-08-29 22:13:03 +0200453static struct focus_state *
454ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
455{
456 struct workspace *ws = get_current_workspace(shell);
457 struct focus_state *state;
458
459 wl_list_for_each(state, &ws->focus_list, link)
460 if (state->seat == seat)
461 break;
462
463 if (&state->link == &ws->focus_list)
464 state = focus_state_create(seat, ws);
465
466 return state;
467}
468
Jonas Ådahl04769742012-06-13 00:01:24 +0200469static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400470restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200471{
472 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200473 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200474
475 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200476 surface = state->keyboard_focus ?
477 &state->keyboard_focus->surface : NULL;
478
479 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200480 }
481}
482
483static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200484replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
485 struct weston_seat *seat)
486{
487 struct focus_state *state;
488 struct wl_surface *surface;
489
490 wl_list_for_each(state, &ws->focus_list, link) {
491 if (state->seat == seat) {
492 surface = seat->seat.keyboard->focus;
493 state->keyboard_focus =
494 (struct weston_surface *) surface;
495 return;
496 }
497 }
498}
499
500static void
501drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
502 struct weston_surface *surface)
503{
504 struct focus_state *state;
505
506 wl_list_for_each(state, &ws->focus_list, link)
507 if (state->keyboard_focus == surface)
508 state->keyboard_focus = NULL;
509}
510
511static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200512workspace_destroy(struct workspace *ws)
513{
Jonas Ådahl04769742012-06-13 00:01:24 +0200514 struct focus_state *state, *next;
515
516 wl_list_for_each_safe(state, next, &ws->focus_list, link)
517 focus_state_destroy(state);
518
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200519 free(ws);
520}
521
Jonas Ådahl04769742012-06-13 00:01:24 +0200522static void
523seat_destroyed(struct wl_listener *listener, void *data)
524{
525 struct weston_seat *seat = data;
526 struct focus_state *state, *next;
527 struct workspace *ws = container_of(listener,
528 struct workspace,
529 seat_destroyed_listener);
530
531 wl_list_for_each_safe(state, next, &ws->focus_list, link)
532 if (state->seat == seat)
533 wl_list_remove(&state->link);
534}
535
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200536static struct workspace *
537workspace_create(void)
538{
539 struct workspace *ws = malloc(sizeof *ws);
540 if (ws == NULL)
541 return NULL;
542
543 weston_layer_init(&ws->layer, NULL);
544
Jonas Ådahl04769742012-06-13 00:01:24 +0200545 wl_list_init(&ws->focus_list);
546 wl_list_init(&ws->seat_destroyed_listener.link);
547 ws->seat_destroyed_listener.notify = seat_destroyed;
548
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200549 return ws;
550}
551
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200552static int
553workspace_is_empty(struct workspace *ws)
554{
555 return wl_list_empty(&ws->layer.surface_list);
556}
557
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200558static struct workspace *
559get_workspace(struct desktop_shell *shell, unsigned int index)
560{
561 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200562 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200563 pws += index;
564 return *pws;
565}
566
567static struct workspace *
568get_current_workspace(struct desktop_shell *shell)
569{
570 return get_workspace(shell, shell->workspaces.current);
571}
572
573static void
574activate_workspace(struct desktop_shell *shell, unsigned int index)
575{
576 struct workspace *ws;
577
578 ws = get_workspace(shell, index);
579 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
580
581 shell->workspaces.current = index;
582}
583
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200584static unsigned int
585get_output_height(struct weston_output *output)
586{
587 return abs(output->region.extents.y1 - output->region.extents.y2);
588}
589
590static void
591surface_translate(struct weston_surface *surface, double d)
592{
593 struct shell_surface *shsurf = get_shell_surface(surface);
594 struct weston_transform *transform;
595
596 transform = &shsurf->workspace_transform;
597 if (wl_list_empty(&transform->link))
598 wl_list_insert(surface->geometry.transformation_list.prev,
599 &shsurf->workspace_transform.link);
600
601 weston_matrix_init(&shsurf->workspace_transform.matrix);
602 weston_matrix_translate(&shsurf->workspace_transform.matrix,
603 0.0, d, 0.0);
604 surface->geometry.dirty = 1;
605}
606
607static void
608workspace_translate_out(struct workspace *ws, double fraction)
609{
610 struct weston_surface *surface;
611 unsigned int height;
612 double d;
613
614 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
615 height = get_output_height(surface->output);
616 d = height * fraction;
617
618 surface_translate(surface, d);
619 }
620}
621
622static void
623workspace_translate_in(struct workspace *ws, double fraction)
624{
625 struct weston_surface *surface;
626 unsigned int height;
627 double d;
628
629 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
630 height = get_output_height(surface->output);
631
632 if (fraction > 0)
633 d = -(height - height * fraction);
634 else
635 d = height + height * fraction;
636
637 surface_translate(surface, d);
638 }
639}
640
641static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200642broadcast_current_workspace_state(struct desktop_shell *shell)
643{
644 struct wl_resource *resource;
645
646 wl_list_for_each(resource, &shell->workspaces.client_list, link)
647 workspace_manager_send_state(resource,
648 shell->workspaces.current,
649 shell->workspaces.num);
650}
651
652static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200653reverse_workspace_change_animation(struct desktop_shell *shell,
654 unsigned int index,
655 struct workspace *from,
656 struct workspace *to)
657{
658 shell->workspaces.current = index;
659
660 shell->workspaces.anim_to = to;
661 shell->workspaces.anim_from = from;
662 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
663 shell->workspaces.anim_timestamp = 0;
664
Scott Moreau4272e632012-08-13 09:58:41 -0600665 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200666}
667
668static void
669workspace_deactivate_transforms(struct workspace *ws)
670{
671 struct weston_surface *surface;
672 struct shell_surface *shsurf;
673
674 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
675 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200676 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
677 wl_list_remove(&shsurf->workspace_transform.link);
678 wl_list_init(&shsurf->workspace_transform.link);
679 }
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200680 shsurf->surface->geometry.dirty = 1;
681 }
682}
683
684static void
685finish_workspace_change_animation(struct desktop_shell *shell,
686 struct workspace *from,
687 struct workspace *to)
688{
Scott Moreau4272e632012-08-13 09:58:41 -0600689 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200690
691 wl_list_remove(&shell->workspaces.animation.link);
692 workspace_deactivate_transforms(from);
693 workspace_deactivate_transforms(to);
694 shell->workspaces.anim_to = NULL;
695
696 wl_list_remove(&shell->workspaces.anim_from->layer.link);
697}
698
699static void
700animate_workspace_change_frame(struct weston_animation *animation,
701 struct weston_output *output, uint32_t msecs)
702{
703 struct desktop_shell *shell =
704 container_of(animation, struct desktop_shell,
705 workspaces.animation);
706 struct workspace *from = shell->workspaces.anim_from;
707 struct workspace *to = shell->workspaces.anim_to;
708 uint32_t t;
709 double x, y;
710
711 if (workspace_is_empty(from) && workspace_is_empty(to)) {
712 finish_workspace_change_animation(shell, from, to);
713 return;
714 }
715
716 if (shell->workspaces.anim_timestamp == 0) {
717 if (shell->workspaces.anim_current == 0.0)
718 shell->workspaces.anim_timestamp = msecs;
719 else
720 shell->workspaces.anim_timestamp =
721 msecs -
722 /* Invers of movement function 'y' below. */
723 (asin(1.0 - shell->workspaces.anim_current) *
724 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
725 M_2_PI);
726 }
727
728 t = msecs - shell->workspaces.anim_timestamp;
729
730 /*
731 * x = [0, π/2]
732 * y(x) = sin(x)
733 */
734 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
735 y = sin(x);
736
737 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600738 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200739
740 workspace_translate_out(from, shell->workspaces.anim_dir * y);
741 workspace_translate_in(to, shell->workspaces.anim_dir * y);
742 shell->workspaces.anim_current = y;
743
Scott Moreau4272e632012-08-13 09:58:41 -0600744 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200745 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200746 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200747 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200748}
749
750static void
751animate_workspace_change(struct desktop_shell *shell,
752 unsigned int index,
753 struct workspace *from,
754 struct workspace *to)
755{
756 struct weston_output *output;
757
758 int dir;
759
760 if (index > shell->workspaces.current)
761 dir = -1;
762 else
763 dir = 1;
764
765 shell->workspaces.current = index;
766
767 shell->workspaces.anim_dir = dir;
768 shell->workspaces.anim_from = from;
769 shell->workspaces.anim_to = to;
770 shell->workspaces.anim_current = 0.0;
771 shell->workspaces.anim_timestamp = 0;
772
773 output = container_of(shell->compositor->output_list.next,
774 struct weston_output, link);
775 wl_list_insert(&output->animation_list,
776 &shell->workspaces.animation.link);
777
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200779
780 workspace_translate_in(to, 0);
781
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400782 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200783
Scott Moreau4272e632012-08-13 09:58:41 -0600784 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200785}
786
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200787static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200788update_workspace(struct desktop_shell *shell, unsigned int index,
789 struct workspace *from, struct workspace *to)
790{
791 shell->workspaces.current = index;
792 wl_list_insert(&from->layer.link, &to->layer.link);
793 wl_list_remove(&from->layer.link);
794}
795
796static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200797change_workspace(struct desktop_shell *shell, unsigned int index)
798{
799 struct workspace *from;
800 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200801
802 if (index == shell->workspaces.current)
803 return;
804
805 /* Don't change workspace when there is any fullscreen surfaces. */
806 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
807 return;
808
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200809 from = get_current_workspace(shell);
810 to = get_workspace(shell, index);
811
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812 if (shell->workspaces.anim_from == to &&
813 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200814 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200815 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200816 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200817 return;
818 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200819
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200820 if (shell->workspaces.anim_to != NULL)
821 finish_workspace_change_animation(shell,
822 shell->workspaces.anim_from,
823 shell->workspaces.anim_to);
824
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200825 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200826
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200827 if (workspace_is_empty(to) && workspace_is_empty(from))
828 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200829 else
830 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200831
832 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200833}
834
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200835static bool
836workspace_has_only(struct workspace *ws, struct weston_surface *surface)
837{
838 struct wl_list *list = &ws->layer.surface_list;
839 struct wl_list *e;
840
841 if (wl_list_empty(list))
842 return false;
843
844 e = list->next;
845
846 if (e->next != list)
847 return false;
848
849 return container_of(e, struct weston_surface, layer_link) == surface;
850}
851
852static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200853move_surface_to_workspace(struct desktop_shell *shell,
854 struct weston_surface *surface,
855 uint32_t workspace)
856{
857 struct workspace *from;
858 struct workspace *to;
859 struct weston_seat *seat;
860
861 if (workspace == shell->workspaces.current)
862 return;
863
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200864 if (workspace >= shell->workspaces.num)
865 workspace = shell->workspaces.num - 1;
866
Jonas Ådahle9d22502012-08-29 22:13:01 +0200867 from = get_current_workspace(shell);
868 to = get_workspace(shell, workspace);
869
870 wl_list_remove(&surface->layer_link);
871 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
872
873 drop_focus_state(shell, from, surface);
874 wl_list_for_each(seat, &shell->compositor->seat_list, link)
875 if (seat->has_keyboard &&
Jan Arne Petersena75a7892013-01-16 21:26:50 +0100876 seat->keyboard.keyboard.focus == &surface->surface)
877 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200878
879 weston_surface_damage_below(surface);
880}
881
882static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200883take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884 struct wl_seat *wl_seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200885 unsigned int index)
886{
Jonas Ådahle9d22502012-08-29 22:13:01 +0200887 struct weston_seat *seat = (struct weston_seat *) wl_seat;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200888 struct weston_surface *surface =
Jonas Ådahle9d22502012-08-29 22:13:01 +0200889 (struct weston_surface *) wl_seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200890 struct shell_surface *shsurf;
891 struct workspace *from;
892 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200893 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200894
895 if (surface == NULL ||
896 index == shell->workspaces.current)
897 return;
898
899 from = get_current_workspace(shell);
900 to = get_workspace(shell, index);
901
902 wl_list_remove(&surface->layer_link);
903 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
904
Jonas Ådahle9d22502012-08-29 22:13:01 +0200905 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200906 drop_focus_state(shell, from, surface);
907
908 if (shell->workspaces.anim_from == to &&
909 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200910 wl_list_remove(&to->layer.link);
911 wl_list_insert(from->layer.link.prev, &to->layer.link);
912
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200913 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200914 broadcast_current_workspace_state(shell);
915
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200916 return;
917 }
918
919 if (shell->workspaces.anim_to != NULL)
920 finish_workspace_change_animation(shell,
921 shell->workspaces.anim_from,
922 shell->workspaces.anim_to);
923
924 if (workspace_is_empty(from) &&
925 workspace_has_only(to, surface))
926 update_workspace(shell, index, from, to);
927 else {
928 shsurf = get_shell_surface(surface);
929 if (wl_list_empty(&shsurf->workspace_transform.link))
930 wl_list_insert(&shell->workspaces.anim_sticky_list,
931 &shsurf->workspace_transform.link);
932
933 animate_workspace_change(shell, index, from, to);
934 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200935
936 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200937
938 state = ensure_focus_state(shell, seat);
939 if (state != NULL)
940 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200941}
942
943static void
944workspace_manager_move_surface(struct wl_client *client,
945 struct wl_resource *resource,
946 struct wl_resource *surface_resource,
947 uint32_t workspace)
948{
949 struct desktop_shell *shell = resource->data;
950 struct weston_surface *surface =
951 (struct weston_surface *) surface_resource;
952
953 move_surface_to_workspace(shell, surface, workspace);
954}
955
956static const struct workspace_manager_interface workspace_manager_implementation = {
957 workspace_manager_move_surface,
958};
959
960static void
961unbind_resource(struct wl_resource *resource)
962{
963 wl_list_remove(&resource->link);
964 free(resource);
965}
966
967static void
968bind_workspace_manager(struct wl_client *client,
969 void *data, uint32_t version, uint32_t id)
970{
971 struct desktop_shell *shell = data;
972 struct wl_resource *resource;
973
974 resource = wl_client_add_object(client, &workspace_manager_interface,
975 &workspace_manager_implementation,
976 id, shell);
977
978 if (resource == NULL) {
979 weston_log("couldn't add workspace manager object");
980 return;
981 }
982
983 resource->destroy = unbind_resource;
984 wl_list_insert(&shell->workspaces.client_list, &resource->link);
985
986 workspace_manager_send_state(resource,
987 shell->workspaces.current,
988 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200989}
990
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200991static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400992noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100993 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500994{
995 grab->focus = NULL;
996}
997
998static void
Scott Moreau447013d2012-02-18 05:05:29 -0700999move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001000 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001001{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001003 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001004 struct shell_surface *shsurf = move->base.shsurf;
1005 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001006 int dx = wl_fixed_to_int(pointer->x + move->dx);
1007 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001008
1009 if (!shsurf)
1010 return;
1011
1012 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001013
Daniel Stone103db7f2012-05-08 17:17:55 +01001014 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001015 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001016
1017 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001018}
1019
1020static void
Scott Moreau447013d2012-02-18 05:05:29 -07001021move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001022 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001024 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1025 grab);
Daniel Stone37816df2012-05-16 18:45:18 +01001026 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001027 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001028
Daniel Stone4dbadb12012-05-30 16:31:51 +01001029 if (pointer->button_count == 0 &&
1030 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001031 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001032 free(grab);
1033 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001034}
1035
Scott Moreau447013d2012-02-18 05:05:29 -07001036static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001037 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001038 move_grab_motion,
1039 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001040};
1041
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001042static int
1043surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1044{
1045 struct weston_move_grab *move;
1046
1047 if (!shsurf)
1048 return -1;
1049
1050 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1051 return 0;
1052
1053 move = malloc(sizeof *move);
1054 if (!move)
1055 return -1;
1056
1057 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1058 ws->seat.pointer->grab_x;
1059 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1060 ws->seat.pointer->grab_y;
1061
1062 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1063 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1064
1065 return 0;
1066}
1067
1068static void
1069shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1070 struct wl_resource *seat_resource, uint32_t serial)
1071{
1072 struct weston_seat *ws = seat_resource->data;
1073 struct shell_surface *shsurf = resource->data;
1074
1075 if (ws->seat.pointer->button_count == 0 ||
1076 ws->seat.pointer->grab_serial != serial ||
1077 ws->seat.pointer->focus != &shsurf->surface->surface)
1078 return;
1079
1080 if (surface_move(shsurf, ws) < 0)
1081 wl_resource_post_no_memory(resource);
1082}
1083
1084struct weston_resize_grab {
1085 struct shell_grab base;
1086 uint32_t edges;
1087 int32_t width, height;
1088};
1089
1090static void
1091resize_grab_motion(struct wl_pointer_grab *grab,
1092 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1093{
1094 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1095 struct wl_pointer *pointer = grab->pointer;
1096 struct shell_surface *shsurf = resize->base.shsurf;
1097 int32_t width, height;
1098 wl_fixed_t from_x, from_y;
1099 wl_fixed_t to_x, to_y;
1100
1101 if (!shsurf)
1102 return;
1103
1104 weston_surface_from_global_fixed(shsurf->surface,
1105 pointer->grab_x, pointer->grab_y,
1106 &from_x, &from_y);
1107 weston_surface_from_global_fixed(shsurf->surface,
1108 pointer->x, pointer->y, &to_x, &to_y);
1109
1110 width = resize->width;
1111 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1112 width += wl_fixed_to_int(from_x - to_x);
1113 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1114 width += wl_fixed_to_int(to_x - from_x);
1115 }
1116
1117 height = resize->height;
1118 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1119 height += wl_fixed_to_int(from_y - to_y);
1120 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1121 height += wl_fixed_to_int(to_y - from_y);
1122 }
1123
1124 shsurf->client->send_configure(shsurf->surface,
1125 resize->edges, width, height);
1126}
1127
1128static void
1129send_configure(struct weston_surface *surface,
1130 uint32_t edges, int32_t width, int32_t height)
1131{
1132 struct shell_surface *shsurf = get_shell_surface(surface);
1133
1134 wl_shell_surface_send_configure(&shsurf->resource,
1135 edges, width, height);
1136}
1137
1138static const struct weston_shell_client shell_client = {
1139 send_configure
1140};
1141
1142static void
1143resize_grab_button(struct wl_pointer_grab *grab,
1144 uint32_t time, uint32_t button, uint32_t state_w)
1145{
1146 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1147 struct wl_pointer *pointer = grab->pointer;
1148 enum wl_pointer_button_state state = state_w;
1149
1150 if (pointer->button_count == 0 &&
1151 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1152 shell_grab_end(&resize->base);
1153 free(grab);
1154 }
1155}
1156
1157static const struct wl_pointer_grab_interface resize_grab_interface = {
1158 noop_grab_focus,
1159 resize_grab_motion,
1160 resize_grab_button,
1161};
1162
1163static int
1164surface_resize(struct shell_surface *shsurf,
1165 struct weston_seat *ws, uint32_t edges)
1166{
1167 struct weston_resize_grab *resize;
1168
1169 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1170 return 0;
1171
1172 if (edges == 0 || edges > 15 ||
1173 (edges & 3) == 3 || (edges & 12) == 12)
1174 return 0;
1175
1176 resize = malloc(sizeof *resize);
1177 if (!resize)
1178 return -1;
1179
1180 resize->edges = edges;
1181 resize->width = shsurf->surface->geometry.width;
1182 resize->height = shsurf->surface->geometry.height;
1183
1184 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1185 ws->seat.pointer, edges);
1186
1187 return 0;
1188}
1189
1190static void
1191shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1192 struct wl_resource *seat_resource, uint32_t serial,
1193 uint32_t edges)
1194{
1195 struct weston_seat *ws = seat_resource->data;
1196 struct shell_surface *shsurf = resource->data;
1197
1198 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1199 return;
1200
1201 if (ws->seat.pointer->button_count == 0 ||
1202 ws->seat.pointer->grab_serial != serial ||
1203 ws->seat.pointer->focus != &shsurf->surface->surface)
1204 return;
1205
1206 if (surface_resize(shsurf, ws, edges) < 0)
1207 wl_resource_post_no_memory(resource);
1208}
1209
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001210static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001211busy_cursor_grab_focus(struct wl_pointer_grab *base,
1212 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001213{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001214 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001215
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001216 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001217 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001218 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001219 }
1220}
1221
1222static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001223busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1224 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001225{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001226}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001227
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001228static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001229busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001230 uint32_t time, uint32_t button, uint32_t state)
1231{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001232 struct shell_grab *grab = (struct shell_grab *) base;
1233 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001234 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001235 (struct weston_surface *) grab->grab.pointer->current;
1236 struct weston_seat *seat =
1237 (struct weston_seat *) grab->grab.pointer->seat;
1238
1239 shsurf = get_shell_surface(surface);
1240 if (shsurf && button == BTN_LEFT && state) {
1241 activate(shsurf->shell, shsurf->surface, seat);
1242 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001243 } else if (shsurf && button == BTN_RIGHT && state) {
1244 activate(shsurf->shell, shsurf->surface, seat);
1245 surface_rotate(shsurf, &seat->seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001246 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001247}
1248
1249static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1250 busy_cursor_grab_focus,
1251 busy_cursor_grab_motion,
1252 busy_cursor_grab_button,
1253};
1254
1255static void
1256set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1257{
1258 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001259
1260 grab = malloc(sizeof *grab);
1261 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001262 return;
1263
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001264 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1265 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001266}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001267
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001268static void
1269end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1270{
1271 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1272
1273 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001274 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001275 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001276 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001277}
1278
Scott Moreau9521d5e2012-04-19 13:06:17 -06001279static void
1280ping_timer_destroy(struct shell_surface *shsurf)
1281{
1282 if (!shsurf || !shsurf->ping_timer)
1283 return;
1284
1285 if (shsurf->ping_timer->source)
1286 wl_event_source_remove(shsurf->ping_timer->source);
1287
1288 free(shsurf->ping_timer);
1289 shsurf->ping_timer = NULL;
1290}
1291
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001292static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001293ping_timeout_handler(void *data)
1294{
1295 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001296 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001297
Scott Moreau9521d5e2012-04-19 13:06:17 -06001298 /* Client is not responding */
1299 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001300
1301 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1302 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1303 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001304
1305 return 1;
1306}
1307
1308static void
1309ping_handler(struct weston_surface *surface, uint32_t serial)
1310{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001311 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001312 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001313 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001314
1315 if (!shsurf)
1316 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001317 if (!shsurf->resource.client)
1318 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001319
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001320 if (shsurf->surface == shsurf->shell->grab_surface)
1321 return;
1322
Scott Moreauff1db4a2012-04-17 19:06:18 -06001323 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001324 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001325 if (!shsurf->ping_timer)
1326 return;
1327
1328 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001329 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1330 shsurf->ping_timer->source =
1331 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1332 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1333
1334 wl_shell_surface_send_ping(&shsurf->resource, serial);
1335 }
1336}
1337
1338static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001339handle_pointer_focus(struct wl_listener *listener, void *data)
1340{
1341 struct wl_pointer *pointer = data;
1342 struct weston_surface *surface =
1343 (struct weston_surface *) pointer->focus;
1344 struct weston_compositor *compositor;
1345 struct shell_surface *shsurf;
1346 uint32_t serial;
1347
1348 if (!surface)
1349 return;
1350
1351 compositor = surface->compositor;
1352 shsurf = get_shell_surface(surface);
1353
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001354 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001355 set_busy_cursor(shsurf, pointer);
1356 } else {
1357 serial = wl_display_next_serial(compositor->wl_display);
1358 ping_handler(surface, serial);
1359 }
1360}
1361
1362static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001363create_pointer_focus_listener(struct weston_seat *seat)
1364{
1365 struct wl_listener *listener;
1366
1367 if (!seat->seat.pointer)
1368 return;
1369
1370 listener = malloc(sizeof *listener);
1371 listener->notify = handle_pointer_focus;
1372 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1373}
1374
1375static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001376shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1377 uint32_t serial)
1378{
1379 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001380 struct desktop_shell *shell = shsurf->shell;
1381 struct weston_seat *seat;
1382 struct weston_compositor *ec = shsurf->surface->compositor;
1383 struct wl_pointer *pointer;
1384 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001385
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001386 if (shsurf->ping_timer == NULL)
1387 /* Just ignore unsolicited pong. */
1388 return;
1389
Scott Moreauff1db4a2012-04-17 19:06:18 -06001390 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001391 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001392 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001393 if (was_unresponsive) {
1394 /* Received pong from previously unresponsive client */
1395 wl_list_for_each(seat, &ec->seat_list, link) {
1396 pointer = seat->seat.pointer;
1397 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001398 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001399 pointer->current ==
1400 &shsurf->surface->surface)
1401 end_busy_cursor(shsurf, pointer);
1402 }
1403 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001404 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001405 }
1406}
1407
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001408static void
1409shell_surface_set_title(struct wl_client *client,
1410 struct wl_resource *resource, const char *title)
1411{
1412 struct shell_surface *shsurf = resource->data;
1413
1414 free(shsurf->title);
1415 shsurf->title = strdup(title);
1416}
1417
1418static void
1419shell_surface_set_class(struct wl_client *client,
1420 struct wl_resource *resource, const char *class)
1421{
1422 struct shell_surface *shsurf = resource->data;
1423
1424 free(shsurf->class);
1425 shsurf->class = strdup(class);
1426}
1427
Juan Zhao96879df2012-02-07 08:45:41 +08001428static struct weston_output *
1429get_default_output(struct weston_compositor *compositor)
1430{
1431 return container_of(compositor->output_list.next,
1432 struct weston_output, link);
1433}
1434
Alex Wu4539b082012-03-01 12:57:46 +08001435static void
1436shell_unset_fullscreen(struct shell_surface *shsurf)
1437{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001438 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001439 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001440 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1441 shell_surface_is_top_fullscreen(shsurf)) {
1442 weston_output_switch_mode(shsurf->fullscreen_output,
1443 shsurf->fullscreen_output->origin);
1444 }
Alex Wu4539b082012-03-01 12:57:46 +08001445 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1446 shsurf->fullscreen.framerate = 0;
1447 wl_list_remove(&shsurf->fullscreen.transform.link);
1448 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001449 if (shsurf->fullscreen.black_surface)
1450 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001451 shsurf->fullscreen.black_surface = NULL;
1452 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001453 weston_surface_set_position(shsurf->surface,
1454 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001455 if (shsurf->saved_rotation_valid) {
1456 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1457 &shsurf->rotation.transform.link);
1458 shsurf->saved_rotation_valid = false;
1459 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001460
1461 ws = get_current_workspace(shsurf->shell);
1462 wl_list_remove(&shsurf->surface->layer_link);
1463 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001464}
1465
Pekka Paalanen98262232011-12-01 10:42:22 +02001466static int
1467reset_shell_surface_type(struct shell_surface *surface)
1468{
1469 switch (surface->type) {
1470 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001471 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001472 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001473 case SHELL_SURFACE_MAXIMIZED:
1474 surface->output = get_default_output(surface->surface->compositor);
1475 weston_surface_set_position(surface->surface,
1476 surface->saved_x,
1477 surface->saved_y);
1478 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001479 case SHELL_SURFACE_NONE:
1480 case SHELL_SURFACE_TOPLEVEL:
1481 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001482 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001483 break;
1484 }
1485
1486 surface->type = SHELL_SURFACE_NONE;
1487 return 0;
1488}
1489
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001490static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001491set_surface_type(struct shell_surface *shsurf)
1492{
1493 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001494 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001495
1496 reset_shell_surface_type(shsurf);
1497
1498 shsurf->type = shsurf->next_type;
1499 shsurf->next_type = SHELL_SURFACE_NONE;
1500
1501 switch (shsurf->type) {
1502 case SHELL_SURFACE_TOPLEVEL:
1503 break;
1504 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001505 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001506 pes->geometry.x + shsurf->transient.x,
1507 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001508 break;
1509
1510 case SHELL_SURFACE_MAXIMIZED:
1511 shsurf->saved_x = surface->geometry.x;
1512 shsurf->saved_y = surface->geometry.y;
1513 shsurf->saved_position_valid = true;
1514 break;
1515
1516 case SHELL_SURFACE_FULLSCREEN:
1517 shsurf->saved_x = surface->geometry.x;
1518 shsurf->saved_y = surface->geometry.y;
1519 shsurf->saved_position_valid = true;
1520
1521 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1522 wl_list_remove(&shsurf->rotation.transform.link);
1523 wl_list_init(&shsurf->rotation.transform.link);
1524 shsurf->surface->geometry.dirty = 1;
1525 shsurf->saved_rotation_valid = true;
1526 }
1527 break;
1528
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001529 default:
1530 break;
1531 }
1532}
1533
1534static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001535set_toplevel(struct shell_surface *shsurf)
1536{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001537 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001538}
1539
1540static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001541shell_surface_set_toplevel(struct wl_client *client,
1542 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001543{
Pekka Paalanen98262232011-12-01 10:42:22 +02001544 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001545
Tiago Vignattibc052c92012-04-19 16:18:18 +03001546 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001547}
1548
1549static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001550set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001551 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001552{
1553 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001554 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001555 shsurf->transient.x = x;
1556 shsurf->transient.y = y;
1557 shsurf->transient.flags = flags;
1558 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1559}
1560
1561static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001562shell_surface_set_transient(struct wl_client *client,
1563 struct wl_resource *resource,
1564 struct wl_resource *parent_resource,
1565 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001566{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001567 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001568 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001569
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001570 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001571}
1572
Tiago Vignattibe143262012-04-16 17:31:41 +03001573static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001574shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001575{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001576 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001577}
1578
1579static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001580get_output_panel_height(struct desktop_shell *shell,
1581 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001582{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001583 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001584 int panel_height = 0;
1585
1586 if (!output)
1587 return 0;
1588
Juan Zhao4ab94682012-07-09 22:24:09 -07001589 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001590 if (surface->output == output) {
1591 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001592 break;
1593 }
1594 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001595
Juan Zhao96879df2012-02-07 08:45:41 +08001596 return panel_height;
1597}
1598
1599static void
1600shell_surface_set_maximized(struct wl_client *client,
1601 struct wl_resource *resource,
1602 struct wl_resource *output_resource )
1603{
1604 struct shell_surface *shsurf = resource->data;
1605 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001606 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001607 uint32_t edges = 0, panel_height = 0;
1608
1609 /* get the default output, if the client set it as NULL
1610 check whether the ouput is available */
1611 if (output_resource)
1612 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001613 else if (es->output)
1614 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001615 else
1616 shsurf->output = get_default_output(es->compositor);
1617
Tiago Vignattibe143262012-04-16 17:31:41 +03001618 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001619 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001620 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001621
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001622 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001623 shsurf->output->width,
1624 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001625
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001626 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001627}
1628
Alex Wu21858432012-04-01 20:13:08 +08001629static void
1630black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1631
Alex Wu4539b082012-03-01 12:57:46 +08001632static struct weston_surface *
1633create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001634 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001635 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001636{
1637 struct weston_surface *surface = NULL;
1638
1639 surface = weston_surface_create(ec);
1640 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001641 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001642 return NULL;
1643 }
1644
Alex Wu21858432012-04-01 20:13:08 +08001645 surface->configure = black_surface_configure;
1646 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001647 weston_surface_configure(surface, x, y, w, h);
1648 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001649 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001650 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001651 pixman_region32_fini(&surface->input);
1652 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001653
Alex Wu4539b082012-03-01 12:57:46 +08001654 return surface;
1655}
1656
1657/* Create black surface and append it to the associated fullscreen surface.
1658 * Handle size dismatch and positioning according to the method. */
1659static void
1660shell_configure_fullscreen(struct shell_surface *shsurf)
1661{
1662 struct weston_output *output = shsurf->fullscreen_output;
1663 struct weston_surface *surface = shsurf->surface;
1664 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001665 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001666
Alex Wu4539b082012-03-01 12:57:46 +08001667 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001668 shsurf->fullscreen.black_surface =
1669 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001670 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001671 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001672 output->width,
1673 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001674
1675 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1676 wl_list_insert(&surface->layer_link,
1677 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001678 shsurf->fullscreen.black_surface->output = output;
1679
1680 switch (shsurf->fullscreen.type) {
1681 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001682 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001683 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001684 break;
1685 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001686 /* 1:1 mapping between surface and output dimensions */
1687 if (output->width == surface->geometry.width &&
1688 output->height == surface->geometry.height) {
1689 weston_surface_set_position(surface, output->x, output->y);
1690 break;
1691 }
1692
Alex Wu4539b082012-03-01 12:57:46 +08001693 matrix = &shsurf->fullscreen.transform.matrix;
1694 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001695
Scott Moreau1bad5db2012-08-18 01:04:05 -06001696 output_aspect = (float) output->width /
1697 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001698 surface_aspect = (float) surface->geometry.width /
1699 (float) surface->geometry.height;
1700 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001701 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001702 (float) surface->geometry.width;
1703 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001704 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001705 (float) surface->geometry.height;
1706
Alex Wu4539b082012-03-01 12:57:46 +08001707 weston_matrix_scale(matrix, scale, scale, 1);
1708 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001709 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001710 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001711 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1712 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001713 weston_surface_set_position(surface, x, y);
1714
Alex Wu4539b082012-03-01 12:57:46 +08001715 break;
1716 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001717 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001718 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001719 surface->geometry.width,
1720 surface->geometry.height,
1721 shsurf->fullscreen.framerate};
1722
1723 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001724 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001725 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001726 output->width,
1727 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001728 weston_surface_set_position(surface, output->x, output->y);
1729 break;
1730 }
1731 }
Alex Wu4539b082012-03-01 12:57:46 +08001732 break;
1733 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1734 break;
1735 default:
1736 break;
1737 }
1738}
1739
1740/* make the fullscreen and black surface at the top */
1741static void
1742shell_stack_fullscreen(struct shell_surface *shsurf)
1743{
Alex Wubd3354b2012-04-17 17:20:49 +08001744 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001745 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001746 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001747
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001748 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001749 wl_list_insert(&shell->fullscreen_layer.surface_list,
1750 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001751 weston_surface_damage(surface);
1752
1753 if (!shsurf->fullscreen.black_surface)
1754 shsurf->fullscreen.black_surface =
1755 create_black_surface(surface->compositor,
1756 surface,
1757 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001758 output->width,
1759 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001760
1761 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001762 wl_list_insert(&surface->layer_link,
1763 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001764 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001765}
1766
1767static void
1768shell_map_fullscreen(struct shell_surface *shsurf)
1769{
Alex Wu4539b082012-03-01 12:57:46 +08001770 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001771 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001772}
1773
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001774static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001775set_fullscreen(struct shell_surface *shsurf,
1776 uint32_t method,
1777 uint32_t framerate,
1778 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001779{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001780 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001781
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001782 if (output)
1783 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001784 else if (es->output)
1785 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001786 else
1787 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001788
Alex Wu4539b082012-03-01 12:57:46 +08001789 shsurf->fullscreen_output = shsurf->output;
1790 shsurf->fullscreen.type = method;
1791 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001792 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001793
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001794 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001795 shsurf->output->width,
1796 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001797}
1798
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001799static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001800shell_surface_set_fullscreen(struct wl_client *client,
1801 struct wl_resource *resource,
1802 uint32_t method,
1803 uint32_t framerate,
1804 struct wl_resource *output_resource)
1805{
1806 struct shell_surface *shsurf = resource->data;
1807 struct weston_output *output;
1808
1809 if (output_resource)
1810 output = output_resource->data;
1811 else
1812 output = NULL;
1813
1814 set_fullscreen(shsurf, method, framerate, output);
1815}
1816
1817static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001818popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001819 struct wl_surface *surface,
1820 wl_fixed_t x,
1821 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001822{
Daniel Stone37816df2012-05-16 18:45:18 +01001823 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001824 struct shell_surface *priv =
1825 container_of(grab, struct shell_surface, popup.grab);
1826 struct wl_client *client = priv->surface->surface.resource.client;
1827
Pekka Paalanencb108432012-01-19 16:25:40 +02001828 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001829 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001830 grab->focus = surface;
1831 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001832 wl_pointer_set_focus(pointer, NULL,
1833 wl_fixed_from_int(0),
1834 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001835 grab->focus = NULL;
1836 }
1837}
1838
1839static void
Scott Moreau447013d2012-02-18 05:05:29 -07001840popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001841 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001842{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843 struct wl_resource *resource;
1844
Daniel Stone37816df2012-05-16 18:45:18 +01001845 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001846 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001847 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001848}
1849
1850static void
Scott Moreau447013d2012-02-18 05:05:29 -07001851popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001852 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001853{
1854 struct wl_resource *resource;
1855 struct shell_surface *shsurf =
1856 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001857 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001858 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001859 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001860
Daniel Stone37816df2012-05-16 18:45:18 +01001861 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001862 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001863 display = wl_client_get_display(resource->client);
1864 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001865 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001866 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001867 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001868 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001869 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001870 }
1871
Daniel Stone4dbadb12012-05-30 16:31:51 +01001872 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001873 shsurf->popup.initial_up = 1;
1874}
1875
Scott Moreau447013d2012-02-18 05:05:29 -07001876static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001877 popup_grab_focus,
1878 popup_grab_motion,
1879 popup_grab_button,
1880};
1881
1882static void
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001883popup_grab_end(struct wl_pointer *pointer)
1884{
1885 struct wl_pointer_grab *grab = pointer->grab;
1886 struct shell_surface *shsurf =
1887 container_of(grab, struct shell_surface, popup.grab);
1888
1889 if (pointer->grab->interface == &popup_grab_interface) {
1890 wl_shell_surface_send_popup_done(&shsurf->resource);
1891 wl_pointer_end_grab(grab->pointer);
1892 shsurf->popup.grab.pointer = NULL;
1893 }
1894}
1895
1896static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001897shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001898{
Daniel Stone37816df2012-05-16 18:45:18 +01001899 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001900 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001901 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001902
Giulio Camuffo8aa16172013-02-18 22:26:01 +01001903 /* Remove the old transform. We don't want to add it twice
1904 * otherwise Weston will go into an infinite loop when going
1905 * through the transforms. */
1906 if (!wl_list_empty(&shsurf->popup.parent_transform.link)) {
1907 wl_list_remove(&shsurf->popup.parent_transform.link);
1908 wl_list_init(&shsurf->popup.parent_transform.link);
1909 }
1910
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001911 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001912 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001913
Pekka Paalanen938269a2012-02-07 14:19:01 +02001914 weston_surface_update_transform(parent);
1915 if (parent->transform.enabled) {
1916 shsurf->popup.parent_transform.matrix =
1917 parent->transform.matrix;
1918 } else {
1919 /* construct x, y translation matrix */
1920 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001921 shsurf->popup.parent_transform.matrix.type =
1922 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001923 shsurf->popup.parent_transform.matrix.d[12] =
1924 parent->geometry.x;
1925 shsurf->popup.parent_transform.matrix.d[13] =
1926 parent->geometry.y;
1927 }
1928 wl_list_insert(es->geometry.transformation_list.prev,
1929 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001930
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001931 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001932 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1933 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001934
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001935 /* We don't require the grab to still be active, but if another
1936 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001937 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1938 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001939 } else {
1940 wl_shell_surface_send_popup_done(&shsurf->resource);
1941 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001942}
1943
1944static void
1945shell_surface_set_popup(struct wl_client *client,
1946 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001947 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001948 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001949 struct wl_resource *parent_resource,
1950 int32_t x, int32_t y, uint32_t flags)
1951{
1952 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001953
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001954 shsurf->type = SHELL_SURFACE_POPUP;
1955 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001956 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001957 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001958 shsurf->popup.x = x;
1959 shsurf->popup.y = y;
1960}
1961
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001962static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001963 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001964 shell_surface_move,
1965 shell_surface_resize,
1966 shell_surface_set_toplevel,
1967 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001968 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001969 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001970 shell_surface_set_maximized,
1971 shell_surface_set_title,
1972 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001973};
1974
1975static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001976destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001977{
Daniel Stone37816df2012-05-16 18:45:18 +01001978 if (shsurf->popup.grab.pointer)
1979 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001980
Alex Wubd3354b2012-04-17 17:20:49 +08001981 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1982 shell_surface_is_top_fullscreen(shsurf)) {
1983 weston_output_switch_mode(shsurf->fullscreen_output,
1984 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001985 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001986
Alex Wuaa08e2d2012-03-05 11:01:40 +08001987 if (shsurf->fullscreen.black_surface)
1988 weston_surface_destroy(shsurf->fullscreen.black_surface);
1989
Alex Wubd3354b2012-04-17 17:20:49 +08001990 /* As destroy_resource() use wl_list_for_each_safe(),
1991 * we can always remove the listener.
1992 */
1993 wl_list_remove(&shsurf->surface_destroy_listener.link);
1994 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001995 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001996
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001997 wl_list_remove(&shsurf->link);
1998 free(shsurf);
1999}
2000
2001static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002002shell_destroy_shell_surface(struct wl_resource *resource)
2003{
2004 struct shell_surface *shsurf = resource->data;
2005
2006 destroy_shell_surface(shsurf);
2007}
2008
2009static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002010shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002011{
2012 struct shell_surface *shsurf = container_of(listener,
2013 struct shell_surface,
2014 surface_destroy_listener);
2015
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002016 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002017 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002018 } else {
2019 wl_signal_emit(&shsurf->resource.destroy_signal,
2020 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002021 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002022 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002023}
2024
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002025static void
2026shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2027
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002028static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002029get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002030{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002031 if (surface->configure == shell_surface_configure)
2032 return surface->private;
2033 else
2034 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002035}
2036
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002037static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002038create_shell_surface(void *shell, struct weston_surface *surface,
2039 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002040{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002041 struct shell_surface *shsurf;
2042
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002043 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002044 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002045 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002046 }
2047
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002048 shsurf = calloc(1, sizeof *shsurf);
2049 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002050 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002051 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002052 }
2053
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002054 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002055 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002056
Tiago Vignattibc052c92012-04-19 16:18:18 +03002057 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002058 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002059 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002060 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002061 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002062 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2063 shsurf->fullscreen.framerate = 0;
2064 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002065 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002066 wl_list_init(&shsurf->fullscreen.transform.link);
2067
Tiago Vignattibc052c92012-04-19 16:18:18 +03002068 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002069 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2070 wl_signal_add(&surface->surface.resource.destroy_signal,
2071 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002072
2073 /* init link so its safe to always remove it in destroy_shell_surface */
2074 wl_list_init(&shsurf->link);
2075
Pekka Paalanen460099f2012-01-20 16:48:25 +02002076 /* empty when not in use */
2077 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002078 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002079
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002080 wl_list_init(&shsurf->workspace_transform.link);
Giulio Camuffo8aa16172013-02-18 22:26:01 +01002081 wl_list_init(&shsurf->popup.parent_transform.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002082
Pekka Paalanen98262232011-12-01 10:42:22 +02002083 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002084 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002085
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002086 shsurf->client = client;
2087
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002088 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002089}
2090
2091static void
2092shell_get_shell_surface(struct wl_client *client,
2093 struct wl_resource *resource,
2094 uint32_t id,
2095 struct wl_resource *surface_resource)
2096{
2097 struct weston_surface *surface = surface_resource->data;
2098 struct desktop_shell *shell = resource->data;
2099 struct shell_surface *shsurf;
2100
2101 if (get_shell_surface(surface)) {
2102 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002103 WL_DISPLAY_ERROR_INVALID_OBJECT,
2104 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002105 return;
2106 }
2107
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002108 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002109 if (!shsurf) {
2110 wl_resource_post_error(surface_resource,
2111 WL_DISPLAY_ERROR_INVALID_OBJECT,
2112 "surface->configure already set");
2113 return;
2114 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002115
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002116 shsurf->resource.destroy = shell_destroy_shell_surface;
2117 shsurf->resource.object.id = id;
2118 shsurf->resource.object.interface = &wl_shell_surface_interface;
2119 shsurf->resource.object.implementation =
2120 (void (**)(void)) &shell_surface_implementation;
2121 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002122
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002123 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002124}
2125
2126static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002127 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002128};
2129
Kristian Høgsberg07937562011-04-12 17:25:42 -04002130static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002131handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002132{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002133 struct desktop_shell *shell =
2134 container_of(proc, struct desktop_shell, screensaver.process);
2135 struct weston_output *output;
2136
Pekka Paalanen18027e52011-12-02 16:31:49 +02002137 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002138
2139 if (shell->locked)
2140 wl_list_for_each(output, &shell->compositor->output_list, link)
2141 if (output->set_dpms)
2142 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002143}
2144
2145static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002146launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002147{
2148 if (shell->screensaver.binding)
2149 return;
2150
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002151 if (!shell->screensaver.path)
2152 return;
2153
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002154 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002155 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002156 return;
2157 }
2158
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002159 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002160 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002161 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002162 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002163}
2164
2165static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002166terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002167{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002168 if (shell->screensaver.process.pid == 0)
2169 return;
2170
2171 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002172}
2173
2174static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002175configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002176{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002177 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002178
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002179 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2180 if (s->output == es->output && s != es) {
2181 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002182 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002183 }
2184 }
2185
2186 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002187 weston_surface_buffer_width(es),
2188 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002189
2190 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002191 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002192 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002193 }
2194}
2195
2196static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002197background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2198{
2199 struct desktop_shell *shell = es->private;
2200
2201 configure_static_surface(es, &shell->background_layer);
2202}
2203
2204static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002205desktop_shell_set_background(struct wl_client *client,
2206 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002207 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002208 struct wl_resource *surface_resource)
2209{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002210 struct desktop_shell *shell = resource->data;
2211 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002212
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002213 if (surface->configure) {
2214 wl_resource_post_error(surface_resource,
2215 WL_DISPLAY_ERROR_INVALID_OBJECT,
2216 "surface role already assigned");
2217 return;
2218 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002219
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002220 surface->configure = background_configure;
2221 surface->private = shell;
2222 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002223 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002224 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002225 surface->output->width,
2226 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002227}
2228
2229static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002230panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2231{
2232 struct desktop_shell *shell = es->private;
2233
2234 configure_static_surface(es, &shell->panel_layer);
2235}
2236
2237static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002238desktop_shell_set_panel(struct wl_client *client,
2239 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002240 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002241 struct wl_resource *surface_resource)
2242{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002243 struct desktop_shell *shell = resource->data;
2244 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002245
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002246 if (surface->configure) {
2247 wl_resource_post_error(surface_resource,
2248 WL_DISPLAY_ERROR_INVALID_OBJECT,
2249 "surface role already assigned");
2250 return;
2251 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002252
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002253 surface->configure = panel_configure;
2254 surface->private = shell;
2255 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002256 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002257 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002258 surface->output->width,
2259 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002260}
2261
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002262static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002263lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2264{
2265 struct desktop_shell *shell = surface->private;
2266
2267 center_on_output(surface, get_default_output(shell->compositor));
2268
2269 if (!weston_surface_is_mapped(surface)) {
2270 wl_list_insert(&shell->lock_layer.surface_list,
2271 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002272 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002273 weston_compositor_wake(shell->compositor);
2274 }
2275}
2276
2277static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002278handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002279{
Tiago Vignattibe143262012-04-16 17:31:41 +03002280 struct desktop_shell *shell =
2281 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002282
Martin Minarik6d118362012-06-07 18:01:59 +02002283 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002284 shell->lock_surface = NULL;
2285}
2286
2287static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002288desktop_shell_set_lock_surface(struct wl_client *client,
2289 struct wl_resource *resource,
2290 struct wl_resource *surface_resource)
2291{
Tiago Vignattibe143262012-04-16 17:31:41 +03002292 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002293 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002294
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002295 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002296
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002297 if (!shell->locked)
2298 return;
2299
Pekka Paalanen98262232011-12-01 10:42:22 +02002300 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002301
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002302 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2303 wl_signal_add(&surface_resource->destroy_signal,
2304 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002305
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002306 surface->configure = lock_surface_configure;
2307 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002308}
2309
2310static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002311resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002312{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002313 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002314
Pekka Paalanen77346a62011-11-30 16:26:35 +02002315 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002316
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002317 wl_list_remove(&shell->lock_layer.link);
2318 wl_list_insert(&shell->compositor->cursor_layer.link,
2319 &shell->fullscreen_layer.link);
2320 wl_list_insert(&shell->fullscreen_layer.link,
2321 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002322 if (shell->showing_input_panels) {
2323 wl_list_insert(&shell->panel_layer.link,
2324 &shell->input_panel_layer.link);
2325 wl_list_insert(&shell->input_panel_layer.link,
2326 &ws->layer.link);
2327 } else {
2328 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2329 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002330
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002331 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002332
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002333 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002334 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002335 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002336 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002337}
2338
2339static void
2340desktop_shell_unlock(struct wl_client *client,
2341 struct wl_resource *resource)
2342{
Tiago Vignattibe143262012-04-16 17:31:41 +03002343 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002344
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002345 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002346
2347 if (shell->locked)
2348 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002349}
2350
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002351static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002352desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002353 struct wl_resource *resource,
2354 struct wl_resource *surface_resource)
2355{
2356 struct desktop_shell *shell = resource->data;
2357
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002358 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002359}
2360
Kristian Høgsberg75840622011-09-06 13:48:16 -04002361static const struct desktop_shell_interface desktop_shell_implementation = {
2362 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002363 desktop_shell_set_panel,
2364 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002365 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002366 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002367};
2368
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002369static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002370get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002371{
2372 struct shell_surface *shsurf;
2373
2374 shsurf = get_shell_surface(surface);
2375 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002376 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002377 return shsurf->type;
2378}
2379
Kristian Høgsberg75840622011-09-06 13:48:16 -04002380static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002381move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002382{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002383 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002384 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002385 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002386
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002387 if (surface == NULL)
2388 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002389
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002390 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002391 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002392 return;
2393
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002394 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002395}
2396
2397static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002398resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002399{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002400 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002401 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002402 uint32_t edges = 0;
2403 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002404 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002405
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002406 if (surface == NULL)
2407 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002408
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002409 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002410 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002411 return;
2412
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002413 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002414 wl_fixed_to_int(seat->pointer->grab_x),
2415 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002416 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002417
Pekka Paalanen60921e52012-01-25 15:55:43 +02002418 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002419 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002420 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002421 edges |= 0;
2422 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002423 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002424
Pekka Paalanen60921e52012-01-25 15:55:43 +02002425 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002426 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002427 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002428 edges |= 0;
2429 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002430 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002431
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002432 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002433}
2434
2435static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002436surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002437 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002438{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002439 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002440 struct shell_surface *shsurf;
2441 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002442 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002443
2444 if (surface == NULL)
2445 return;
2446
2447 shsurf = get_shell_surface(surface);
2448 if (!shsurf)
2449 return;
2450
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002451 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002452
Scott Moreau02709af2012-05-22 01:54:10 -06002453 if (surface->alpha > 1.0)
2454 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002455 if (surface->alpha < step)
2456 surface->alpha = step;
2457
2458 surface->geometry.dirty = 1;
2459 weston_surface_damage(surface);
2460}
2461
2462static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002463do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002464 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002465{
Daniel Stone37816df2012-05-16 18:45:18 +01002466 struct weston_seat *ws = (struct weston_seat *) seat;
2467 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002468 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002469 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002470
2471 wl_list_for_each(output, &compositor->output_list, link) {
2472 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002473 wl_fixed_to_double(seat->pointer->x),
2474 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002475 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002476 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002477 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002478 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002479 increment = -output->zoom.increment;
2480 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002481 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002482 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002483 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002484 else
2485 increment = 0;
2486
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002487 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002488
Scott Moreaue6603982012-06-11 13:07:51 -06002489 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002490 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002491 else if (output->zoom.level > output->zoom.max_level)
2492 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002493 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002494 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002495 output->disable_planes++;
2496 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002497
Scott Moreaue6603982012-06-11 13:07:51 -06002498 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002499
Scott Moreau8dacaab2012-06-17 18:10:58 -06002500 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002501 }
2502 }
2503}
2504
Scott Moreauccbf29d2012-02-22 14:21:41 -07002505static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002506zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002507 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002508{
2509 do_zoom(seat, time, 0, axis, value);
2510}
2511
2512static void
2513zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2514 void *data)
2515{
2516 do_zoom(seat, time, key, 0, 0);
2517}
2518
2519static void
2520terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2521 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002522{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002523 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002524
Daniel Stone325fc2d2012-05-30 16:31:58 +01002525 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002526}
2527
2528static void
Scott Moreau447013d2012-02-18 05:05:29 -07002529rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002530 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002531{
2532 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002533 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002534 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002535 struct shell_surface *shsurf = rotate->base.shsurf;
2536 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002537 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002538
2539 if (!shsurf)
2540 return;
2541
2542 surface = shsurf->surface;
2543
2544 cx = 0.5f * surface->geometry.width;
2545 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002546
Daniel Stone37816df2012-05-16 18:45:18 +01002547 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2548 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002549 r = sqrtf(dx * dx + dy * dy);
2550
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002551 wl_list_remove(&shsurf->rotation.transform.link);
2552 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002553
2554 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002555 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002556 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002557
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002558 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002559 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002560
2561 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002562 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002563 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002564 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002565 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002566
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002567 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002568 &shsurf->surface->geometry.transformation_list,
2569 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002570 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002571 wl_list_init(&shsurf->rotation.transform.link);
2572 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002573 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002574 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002575
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002576 /* We need to adjust the position of the surface
2577 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002578 cposx = surface->geometry.x + cx;
2579 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002580 dposx = rotate->center.x - cposx;
2581 dposy = rotate->center.y - cposy;
2582 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002583 weston_surface_set_position(surface,
2584 surface->geometry.x + dposx,
2585 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002586 }
2587
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002588 /* Repaint implies weston_surface_update_transform(), which
2589 * lazily applies the damage due to rotation update.
2590 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002591 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002592}
2593
2594static void
Scott Moreau447013d2012-02-18 05:05:29 -07002595rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002596 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002597{
2598 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002599 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002600 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002601 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002602 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002603
Daniel Stone4dbadb12012-05-30 16:31:51 +01002604 if (pointer->button_count == 0 &&
2605 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002606 if (shsurf)
2607 weston_matrix_multiply(&shsurf->rotation.rotation,
2608 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002609 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002610 free(rotate);
2611 }
2612}
2613
Scott Moreau447013d2012-02-18 05:05:29 -07002614static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002615 noop_grab_focus,
2616 rotate_grab_motion,
2617 rotate_grab_button,
2618};
2619
2620static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002621surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002622{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002623 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002624 float dx, dy;
2625 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002626
Pekka Paalanen460099f2012-01-20 16:48:25 +02002627 rotate = malloc(sizeof *rotate);
2628 if (!rotate)
2629 return;
2630
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002631 weston_surface_to_global_float(surface->surface,
2632 surface->surface->geometry.width / 2,
2633 surface->surface->geometry.height / 2,
2634 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002635
Daniel Stone37816df2012-05-16 18:45:18 +01002636 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2637 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002638 r = sqrtf(dx * dx + dy * dy);
2639 if (r > 20.0f) {
2640 struct weston_matrix inverse;
2641
2642 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002643 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002644 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002645
2646 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002647 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002648 } else {
2649 weston_matrix_init(&surface->rotation.rotation);
2650 weston_matrix_init(&rotate->rotation);
2651 }
2652
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002653 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2654 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002655}
2656
2657static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002658rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2659 void *data)
2660{
2661 struct weston_surface *base_surface =
2662 (struct weston_surface *) seat->pointer->focus;
2663 struct shell_surface *surface;
2664
2665 if (base_surface == NULL)
2666 return;
2667
2668 surface = get_shell_surface(base_surface);
2669 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2670 return;
2671
2672 surface_rotate(surface, seat);
2673}
2674
2675static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002676lower_fullscreen_layer(struct desktop_shell *shell)
2677{
2678 struct workspace *ws;
2679 struct weston_surface *surface, *prev;
2680
2681 ws = get_current_workspace(shell);
2682 wl_list_for_each_reverse_safe(surface, prev,
2683 &shell->fullscreen_layer.surface_list,
2684 layer_link)
2685 weston_surface_restack(surface, &ws->layer.surface_list);
2686}
2687
2688static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002689activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002690 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002691{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002692 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002693 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002694
Daniel Stone37816df2012-05-16 18:45:18 +01002695 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002696
Jonas Ådahl8538b222012-08-29 22:13:03 +02002697 state = ensure_focus_state(shell, seat);
2698 if (state == NULL)
2699 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002700
2701 state->keyboard_focus = es;
2702 wl_list_remove(&state->surface_destroy_listener.link);
2703 wl_signal_add(&es->surface.resource.destroy_signal,
2704 &state->surface_destroy_listener);
2705
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002706 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002707 case SHELL_SURFACE_FULLSCREEN:
2708 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002709 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002710 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002711 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002712 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002713 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002714 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002715 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002716 }
2717}
2718
Alex Wu21858432012-04-01 20:13:08 +08002719/* no-op func for checking black surface */
2720static void
2721black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2722{
2723}
2724
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002725static bool
Alex Wu21858432012-04-01 20:13:08 +08002726is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2727{
2728 if (es->configure == black_surface_configure) {
2729 if (fs_surface)
2730 *fs_surface = (struct weston_surface *)es->private;
2731 return true;
2732 }
2733 return false;
2734}
2735
Kristian Høgsberg75840622011-09-06 13:48:16 -04002736static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002737click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002738 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002739{
Daniel Stone37816df2012-05-16 18:45:18 +01002740 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002741 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002742 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002743 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002744
Daniel Stone37816df2012-05-16 18:45:18 +01002745 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002746 if (!focus)
2747 return;
2748
Alex Wu21858432012-04-01 20:13:08 +08002749 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002750 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002751
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002752 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2753 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002754
Daniel Stone325fc2d2012-05-30 16:31:58 +01002755 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002756 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002757}
2758
2759static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002760lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002761{
Tiago Vignattibe143262012-04-16 17:31:41 +03002762 struct desktop_shell *shell =
2763 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002764 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002765 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002766
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002767 if (shell->locked) {
2768 wl_list_for_each(output, &shell->compositor->output_list, link)
2769 /* TODO: find a way to jump to other DPMS levels */
2770 if (output->set_dpms)
2771 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002772 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002773 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002774
2775 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002776
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002777 /* Hide all surfaces by removing the fullscreen, panel and
2778 * toplevel layers. This way nothing else can show or receive
2779 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002780
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002781 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002782 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002783 if (shell->showing_input_panels)
2784 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002785 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002786 wl_list_insert(&shell->compositor->cursor_layer.link,
2787 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002788
Pekka Paalanen77346a62011-11-30 16:26:35 +02002789 launch_screensaver(shell);
2790
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002791 /* TODO: disable bindings that should not work while locked. */
2792
2793 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002794}
2795
2796static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002797unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002798{
Tiago Vignattibe143262012-04-16 17:31:41 +03002799 struct desktop_shell *shell =
2800 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002801
Pekka Paalanend81c2162011-11-16 13:47:34 +02002802 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002803 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002804 return;
2805 }
2806
2807 /* If desktop-shell client has gone away, unlock immediately. */
2808 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002809 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002810 return;
2811 }
2812
2813 if (shell->prepare_event_sent)
2814 return;
2815
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002816 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002817 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002818}
2819
2820static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002821show_input_panels(struct wl_listener *listener, void *data)
2822{
2823 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002824 container_of(listener, struct desktop_shell,
2825 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002826 struct input_panel_surface *surface, *next;
2827 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002828
Jan Arne Petersen451a9712013-02-11 15:10:11 +01002829 if (shell->showing_input_panels)
2830 return;
2831
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002832 shell->showing_input_panels = true;
2833
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002834 if (!shell->locked)
2835 wl_list_insert(&shell->panel_layer.link,
2836 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002837
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002838 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002839 &shell->input_panel.surfaces, link) {
2840 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002841 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002842 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002843 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002844 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002845 weston_surface_damage(ws);
2846 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002847 }
2848}
2849
2850static void
2851hide_input_panels(struct wl_listener *listener, void *data)
2852{
2853 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002854 container_of(listener, struct desktop_shell,
2855 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002856 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002857
Jan Arne Petersen61381972013-01-31 15:52:21 +01002858 if (!shell->showing_input_panels)
2859 return;
2860
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002861 shell->showing_input_panels = false;
2862
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002863 if (!shell->locked)
2864 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002865
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002866 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002867 &shell->input_panel_layer.surface_list, layer_link)
2868 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002869}
2870
2871static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002872center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002873{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002874 int32_t width = weston_surface_buffer_width(surface);
2875 int32_t height = weston_surface_buffer_height(surface);
2876 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002877
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002878 x = output->x + (output->width - width) / 2;
2879 y = output->y + (output->height - height) / 2;
2880
2881 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002882}
2883
2884static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002885weston_surface_set_initial_position (struct weston_surface *surface,
2886 struct desktop_shell *shell)
2887{
2888 struct weston_compositor *compositor = shell->compositor;
2889 int ix = 0, iy = 0;
2890 int range_x, range_y;
2891 int dx, dy, x, y, panel_height;
2892 struct weston_output *output, *target_output = NULL;
2893 struct weston_seat *seat;
2894
2895 /* As a heuristic place the new window on the same output as the
2896 * pointer. Falling back to the output containing 0, 0.
2897 *
2898 * TODO: Do something clever for touch too?
2899 */
2900 wl_list_for_each(seat, &compositor->seat_list, link) {
2901 if (seat->has_pointer) {
2902 ix = wl_fixed_to_int(seat->pointer.x);
2903 iy = wl_fixed_to_int(seat->pointer.y);
2904 break;
2905 }
2906 }
2907
2908 wl_list_for_each(output, &compositor->output_list, link) {
2909 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2910 target_output = output;
2911 break;
2912 }
2913 }
2914
2915 if (!target_output) {
2916 weston_surface_set_position(surface, 10 + random() % 400,
2917 10 + random() % 400);
2918 return;
2919 }
2920
2921 /* Valid range within output where the surface will still be onscreen.
2922 * If this is negative it means that the surface is bigger than
2923 * output.
2924 */
2925 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002926 range_x = target_output->width - surface->geometry.width;
2927 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002928 surface->geometry.height;
2929
Rob Bradford4cb88c72012-08-13 15:18:44 +01002930 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002931 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002932 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002933 dx = 0;
2934
2935 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002936 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002937 else
2938 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002939
2940 x = target_output->x + dx;
2941 y = target_output->y + dy;
2942
2943 weston_surface_set_position (surface, x, y);
2944}
2945
2946static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002947map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002948 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002949{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002950 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002951 struct shell_surface *shsurf = get_shell_surface(surface);
2952 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002953 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002954 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002955 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002956 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002957
Pekka Paalanen60921e52012-01-25 15:55:43 +02002958 surface->geometry.width = width;
2959 surface->geometry.height = height;
2960 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002961
2962 /* initial positioning, see also configure() */
2963 switch (surface_type) {
2964 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002965 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002966 break;
Alex Wu4539b082012-03-01 12:57:46 +08002967 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002968 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002969 shell_map_fullscreen(shsurf);
2970 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002971 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002972 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002973 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002974 weston_surface_set_position(surface, shsurf->output->x,
2975 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002976 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002977 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002978 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002979 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002980 case SHELL_SURFACE_NONE:
2981 weston_surface_set_position(surface,
2982 surface->geometry.x + sx,
2983 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002984 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002985 default:
2986 ;
2987 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002988
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002989 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002990 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002991 case SHELL_SURFACE_POPUP:
2992 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002993 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002994 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2995 break;
Alex Wu4539b082012-03-01 12:57:46 +08002996 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002997 case SHELL_SURFACE_NONE:
2998 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002999 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003000 ws = get_current_workspace(shell);
3001 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003002 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003003 }
3004
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003005 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003006 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003007 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3008 surface->output = shsurf->output;
3009 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003010
Juan Zhao7bb92f02011-12-15 11:31:51 -05003011 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003012 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003013 if (shsurf->transient.flags ==
3014 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3015 break;
3016 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003017 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003018 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003019 if (!shell->locked) {
3020 wl_list_for_each(seat, &compositor->seat_list, link)
3021 activate(shell, surface, seat);
3022 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003023 break;
3024 default:
3025 break;
3026 }
3027
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003028 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003029 {
3030 switch (shell->win_animation_type) {
3031 case ANIMATION_FADE:
3032 weston_fade_run(surface, NULL, NULL);
3033 break;
3034 case ANIMATION_ZOOM:
3035 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3036 break;
3037 default:
3038 break;
3039 }
3040 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003041}
3042
3043static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003044configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003045 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003046{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003047 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3048 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003049
Pekka Paalanen77346a62011-11-30 16:26:35 +02003050 shsurf = get_shell_surface(surface);
3051 if (shsurf)
3052 surface_type = shsurf->type;
3053
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003054 surface->geometry.x = x;
3055 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003056 surface->geometry.width = width;
3057 surface->geometry.height = height;
3058 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003059
3060 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003061 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003062 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003063 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003064 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003065 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003066 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003067 surface->geometry.x = surface->output->x;
3068 surface->geometry.y = surface->output->y +
3069 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003070 break;
Alex Wu4539b082012-03-01 12:57:46 +08003071 case SHELL_SURFACE_TOPLEVEL:
3072 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003073 default:
3074 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003075 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003076
Alex Wu4539b082012-03-01 12:57:46 +08003077 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003078 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003079 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003080
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003081 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003082 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003083 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003084}
3085
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003086static void
3087shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3088{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003089 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003090 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003091 int32_t width = weston_surface_buffer_width(es);
3092 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003093 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003094
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003095 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003096 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003097 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003098 type_changed = 1;
3099 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003100
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003101 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003102 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003103 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003104 es->geometry.width != width ||
3105 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003106 float from_x, from_y;
3107 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003108
3109 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3110 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3111 configure(shell, es,
3112 es->geometry.x + to_x - from_x,
3113 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003114 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003115 }
3116}
3117
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003118static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003119
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003120static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003121desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003122{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003123 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003124 struct desktop_shell *shell =
3125 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003126
3127 shell->child.process.pid = 0;
3128 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003129
3130 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3131 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003132 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003133 shell->child.deathstamp = time;
3134 shell->child.deathcount = 0;
3135 }
3136
3137 shell->child.deathcount++;
3138 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003139 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003140 return;
3141 }
3142
Martin Minarik6d118362012-06-07 18:01:59 +02003143 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003144 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003145}
3146
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003147static void
3148launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003149{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003150 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003151 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003152
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003153 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003154 &shell->child.process,
3155 shell_exe,
3156 desktop_shell_sigchld);
3157
3158 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003159 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003160}
3161
3162static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003163bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3164{
Tiago Vignattibe143262012-04-16 17:31:41 +03003165 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003166
3167 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003168 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003169}
3170
Kristian Høgsberg75840622011-09-06 13:48:16 -04003171static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003172unbind_desktop_shell(struct wl_resource *resource)
3173{
Tiago Vignattibe143262012-04-16 17:31:41 +03003174 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003175
3176 if (shell->locked)
3177 resume_desktop(shell);
3178
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003179 shell->child.desktop_shell = NULL;
3180 shell->prepare_event_sent = false;
3181 free(resource);
3182}
3183
3184static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003185bind_desktop_shell(struct wl_client *client,
3186 void *data, uint32_t version, uint32_t id)
3187{
Tiago Vignattibe143262012-04-16 17:31:41 +03003188 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003189 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003190
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003191 resource = wl_client_add_object(client, &desktop_shell_interface,
3192 &desktop_shell_implementation,
3193 id, shell);
3194
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003195 if (client == shell->child.client) {
3196 resource->destroy = unbind_desktop_shell;
3197 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003198 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003199 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003200
3201 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3202 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003203 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003204}
3205
Pekka Paalanen6e168112011-11-24 11:34:05 +02003206static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003207screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3208{
3209 struct desktop_shell *shell = surface->private;
3210
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003211 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003212 if (!shell->locked)
3213 return;
3214
3215 center_on_output(surface, surface->output);
3216
3217 if (wl_list_empty(&surface->layer_link)) {
3218 wl_list_insert(shell->lock_layer.surface_list.prev,
3219 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003220 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003221 shell->compositor->idle_time = shell->screensaver.duration;
3222 weston_compositor_wake(shell->compositor);
3223 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3224 }
3225}
3226
3227static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003228screensaver_set_surface(struct wl_client *client,
3229 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003230 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003231 struct wl_resource *output_resource)
3232{
Tiago Vignattibe143262012-04-16 17:31:41 +03003233 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003234 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003235 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003236
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003237 surface->configure = screensaver_configure;
3238 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003239 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003240}
3241
3242static const struct screensaver_interface screensaver_implementation = {
3243 screensaver_set_surface
3244};
3245
3246static void
3247unbind_screensaver(struct wl_resource *resource)
3248{
Tiago Vignattibe143262012-04-16 17:31:41 +03003249 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003250
Pekka Paalanen77346a62011-11-30 16:26:35 +02003251 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003252 free(resource);
3253}
3254
3255static void
3256bind_screensaver(struct wl_client *client,
3257 void *data, uint32_t version, uint32_t id)
3258{
Tiago Vignattibe143262012-04-16 17:31:41 +03003259 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003260 struct wl_resource *resource;
3261
3262 resource = wl_client_add_object(client, &screensaver_interface,
3263 &screensaver_implementation,
3264 id, shell);
3265
Pekka Paalanen77346a62011-11-30 16:26:35 +02003266 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003267 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003268 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003269 return;
3270 }
3271
3272 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3273 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003274 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003275}
3276
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003277static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003278input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3279{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003280 struct weston_mode *mode;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003281 int32_t width = weston_surface_buffer_width(surface);
3282 int32_t height = weston_surface_buffer_height(surface);
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003283 float x, y;
3284
3285 if (!weston_surface_is_mapped(surface))
3286 return;
3287
3288 mode = surface->output->current;
3289 x = (mode->width - width) / 2;
3290 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003291
3292 /* Don't map the input panel here, wait for
3293 * show_input_panels signal. */
3294
3295 weston_surface_configure(surface,
3296 surface->output->x + x,
3297 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003298 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003299}
3300
3301static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003302destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003303{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003304 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003305 wl_list_remove(&input_panel_surface->link);
3306
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003307 input_panel_surface->surface->configure = NULL;
3308
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003309 free(input_panel_surface);
3310}
3311
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003312static struct input_panel_surface *
3313get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003314{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003315 if (surface->configure == input_panel_configure) {
3316 return surface->private;
3317 } else {
3318 return NULL;
3319 }
3320}
3321
3322static void
3323input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3324{
3325 struct input_panel_surface *ipsurface = container_of(listener,
3326 struct input_panel_surface,
3327 surface_destroy_listener);
3328
3329 if (ipsurface->resource.client) {
3330 wl_resource_destroy(&ipsurface->resource);
3331 } else {
3332 wl_signal_emit(&ipsurface->resource.destroy_signal,
3333 &ipsurface->resource);
3334 destroy_input_panel_surface(ipsurface);
3335 }
3336}
3337static struct input_panel_surface *
3338create_input_panel_surface(struct desktop_shell *shell,
3339 struct weston_surface *surface)
3340{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003341 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003342
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003343 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3344 if (!input_panel_surface)
3345 return NULL;
3346
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003347 surface->configure = input_panel_configure;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003348 surface->private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003349
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003350 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003351
3352 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003353
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003354 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3355 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3356 wl_signal_add(&surface->surface.resource.destroy_signal,
3357 &input_panel_surface->surface_destroy_listener);
3358
3359 wl_list_init(&input_panel_surface->link);
3360
3361 return input_panel_surface;
3362}
3363
3364static void
3365input_panel_surface_set_toplevel(struct wl_client *client,
3366 struct wl_resource *resource,
3367 uint32_t position)
3368{
3369 struct input_panel_surface *input_panel_surface = resource->data;
3370 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003371
3372 wl_list_insert(&shell->input_panel.surfaces,
3373 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003374}
3375
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003376static const struct input_panel_surface_interface input_panel_surface_implementation = {
3377 input_panel_surface_set_toplevel
3378};
3379
3380static void
3381destroy_input_panel_surface_resource(struct wl_resource *resource)
3382{
3383 struct input_panel_surface *ipsurf = resource->data;
3384
3385 destroy_input_panel_surface(ipsurf);
3386}
3387
3388static void
3389input_panel_get_input_panel_surface(struct wl_client *client,
3390 struct wl_resource *resource,
3391 uint32_t id,
3392 struct wl_resource *surface_resource)
3393{
3394 struct weston_surface *surface = surface_resource->data;
3395 struct desktop_shell *shell = resource->data;
3396 struct input_panel_surface *ipsurf;
3397
3398 if (get_input_panel_surface(surface)) {
3399 wl_resource_post_error(surface_resource,
3400 WL_DISPLAY_ERROR_INVALID_OBJECT,
3401 "input_panel::get_input_panel_surface already requested");
3402 return;
3403 }
3404
3405 ipsurf = create_input_panel_surface(shell, surface);
3406 if (!ipsurf) {
3407 wl_resource_post_error(surface_resource,
3408 WL_DISPLAY_ERROR_INVALID_OBJECT,
3409 "surface->configure already set");
3410 return;
3411 }
3412
3413 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3414 ipsurf->resource.object.id = id;
3415 ipsurf->resource.object.interface = &input_panel_surface_interface;
3416 ipsurf->resource.object.implementation =
3417 (void (**)(void)) &input_panel_surface_implementation;
3418 ipsurf->resource.data = ipsurf;
3419
3420 wl_client_add_resource(client, &ipsurf->resource);
3421}
3422
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003423static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003424 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003425};
3426
3427static void
3428unbind_input_panel(struct wl_resource *resource)
3429{
3430 struct desktop_shell *shell = resource->data;
3431
3432 shell->input_panel.binding = NULL;
3433 free(resource);
3434}
3435
3436static void
3437bind_input_panel(struct wl_client *client,
3438 void *data, uint32_t version, uint32_t id)
3439{
3440 struct desktop_shell *shell = data;
3441 struct wl_resource *resource;
3442
3443 resource = wl_client_add_object(client, &input_panel_interface,
3444 &input_panel_implementation,
3445 id, shell);
3446
3447 if (shell->input_panel.binding == NULL) {
3448 resource->destroy = unbind_input_panel;
3449 shell->input_panel.binding = resource;
3450 return;
3451 }
3452
3453 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3454 "interface object already bound");
3455 wl_resource_destroy(resource);
3456}
3457
Kristian Høgsberg07045392012-02-19 18:52:44 -05003458struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003459 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003460 struct weston_surface *current;
3461 struct wl_listener listener;
3462 struct wl_keyboard_grab grab;
3463};
3464
3465static void
3466switcher_next(struct switcher *switcher)
3467{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003468 struct weston_surface *surface;
3469 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003470 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003471 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003472
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003473 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003474 switch (get_shell_surface_type(surface)) {
3475 case SHELL_SURFACE_TOPLEVEL:
3476 case SHELL_SURFACE_FULLSCREEN:
3477 case SHELL_SURFACE_MAXIMIZED:
3478 if (first == NULL)
3479 first = surface;
3480 if (prev == switcher->current)
3481 next = surface;
3482 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003483 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003484 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003485 weston_surface_damage(surface);
3486 break;
3487 default:
3488 break;
3489 }
Alex Wu1659daa2012-04-01 20:13:09 +08003490
3491 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003492 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003493 surface->geometry.dirty = 1;
3494 weston_surface_damage(surface);
3495 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003496 }
3497
3498 if (next == NULL)
3499 next = first;
3500
Alex Wu07b26062012-03-12 16:06:01 +08003501 if (next == NULL)
3502 return;
3503
Kristian Høgsberg07045392012-02-19 18:52:44 -05003504 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003505 wl_signal_add(&next->surface.resource.destroy_signal,
3506 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003507
3508 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003509 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003510
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003511 shsurf = get_shell_surface(switcher->current);
3512 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003513 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003514}
3515
3516static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003517switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003518{
3519 struct switcher *switcher =
3520 container_of(listener, struct switcher, listener);
3521
3522 switcher_next(switcher);
3523}
3524
3525static void
Daniel Stone351eb612012-05-31 15:27:47 -04003526switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003527{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003528 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003529 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003530 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003531 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003532
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003533 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003534 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003535 weston_surface_damage(surface);
3536 }
3537
Alex Wu07b26062012-03-12 16:06:01 +08003538 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003539 activate(switcher->shell, switcher->current,
3540 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003541 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003542 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003543 if (weston_keyboard->input_method_resource)
3544 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003545 free(switcher);
3546}
3547
3548static void
3549switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003550 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003551{
3552 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003553 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003554
Daniel Stonec9785ea2012-05-30 16:31:52 +01003555 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003556 switcher_next(switcher);
3557}
3558
3559static void
3560switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3561 uint32_t mods_depressed, uint32_t mods_latched,
3562 uint32_t mods_locked, uint32_t group)
3563{
3564 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003565 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003566
Daniel Stone351eb612012-05-31 15:27:47 -04003567 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3568 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003569}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003570
3571static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003572 switcher_key,
3573 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003574};
3575
3576static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003577switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3578 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003579{
Tiago Vignattibe143262012-04-16 17:31:41 +03003580 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003581 struct switcher *switcher;
3582
3583 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003584 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003585 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003586 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003587 wl_list_init(&switcher->listener.link);
3588
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003589 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003590 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003591 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3592 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003593 switcher_next(switcher);
3594}
3595
Pekka Paalanen3c647232011-12-22 13:43:43 +02003596static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003597backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3598 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003599{
3600 struct weston_compositor *compositor = data;
3601 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003602 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003603
3604 /* TODO: we're limiting to simple use cases, where we assume just
3605 * control on the primary display. We'd have to extend later if we
3606 * ever get support for setting backlights on random desktop LCD
3607 * panels though */
3608 output = get_default_output(compositor);
3609 if (!output)
3610 return;
3611
3612 if (!output->set_backlight)
3613 return;
3614
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003615 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3616 backlight_new = output->backlight_current - 25;
3617 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3618 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003619
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003620 if (backlight_new < 5)
3621 backlight_new = 5;
3622 if (backlight_new > 255)
3623 backlight_new = 255;
3624
3625 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003626 output->set_backlight(output, output->backlight_current);
3627}
3628
3629static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003630fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3631 void *data)
3632{
3633 struct desktop_shell *shell = data;
3634 struct weston_compositor *compositor = shell->compositor;
3635 compositor->fan_debug = !compositor->fan_debug;
3636 weston_compositor_damage_all(compositor);
3637}
3638
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003639struct debug_binding_grab {
3640 struct wl_keyboard_grab grab;
3641 struct weston_seat *seat;
3642 uint32_t key[2];
3643 int key_released[2];
3644};
3645
3646static void
3647debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3648 uint32_t key, uint32_t state)
3649{
3650 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3651 struct wl_resource *resource;
3652 struct wl_display *display;
3653 uint32_t serial;
3654 int send = 0, terminate = 0;
3655 int check_binding = 1;
3656 int i;
3657
3658 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3659 /* Do not run bindings on key releases */
3660 check_binding = 0;
3661
3662 for (i = 0; i < 2; i++)
3663 if (key == db->key[i])
3664 db->key_released[i] = 1;
3665
3666 if (db->key_released[0] && db->key_released[1]) {
3667 /* All key releases been swalled so end the grab */
3668 terminate = 1;
3669 } else if (key != db->key[0] && key != db->key[1]) {
3670 /* Should not swallow release of other keys */
3671 send = 1;
3672 }
3673 } else if (key == db->key[0] && !db->key_released[0]) {
3674 /* Do not check bindings for the first press of the binding
3675 * key. This allows it to be used as a debug shortcut.
3676 * We still need to swallow this event. */
3677 check_binding = 0;
3678 } else if (db->key[1]) {
3679 /* If we already ran a binding don't process another one since
3680 * we can't keep track of all the binding keys that were
3681 * pressed in order to swallow the release events. */
3682 send = 1;
3683 check_binding = 0;
3684 }
3685
3686 if (check_binding) {
3687 struct weston_compositor *ec = db->seat->compositor;
3688
3689 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3690 key, state)) {
3691 /* We ran a binding so swallow the press and keep the
3692 * grab to swallow the released too. */
3693 send = 0;
3694 terminate = 0;
3695 db->key[1] = key;
3696 } else {
3697 /* Terminate the grab since the key pressed is not a
3698 * debug binding key. */
3699 send = 1;
3700 terminate = 1;
3701 }
3702 }
3703
3704 if (send) {
3705 resource = grab->keyboard->focus_resource;
3706
3707 if (resource) {
3708 display = wl_client_get_display(resource->client);
3709 serial = wl_display_next_serial(display);
3710 wl_keyboard_send_key(resource, serial, time, key, state);
3711 }
3712 }
3713
3714 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003715 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003716 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003717 if (weston_keyboard->input_method_resource)
3718 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003719 free(db);
3720 }
3721}
3722
3723static void
3724debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3725 uint32_t mods_depressed, uint32_t mods_latched,
3726 uint32_t mods_locked, uint32_t group)
3727{
3728 struct wl_resource *resource;
3729
3730 resource = grab->keyboard->focus_resource;
3731 if (!resource)
3732 return;
3733
3734 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3735 mods_latched, mods_locked, group);
3736}
3737
3738struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3739 debug_binding_key,
3740 debug_binding_modifiers
3741};
3742
3743static void
3744debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3745{
3746 struct debug_binding_grab *grab;
3747
3748 grab = calloc(1, sizeof *grab);
3749 if (!grab)
3750 return;
3751
3752 grab->seat = (struct weston_seat *) seat;
3753 grab->key[0] = key;
3754 grab->grab.interface = &debug_binding_keyboard_grab;
3755 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3756}
3757
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003758static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003759force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3760 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003761{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003762 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003763 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003764 struct desktop_shell *shell = data;
3765 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003766 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003767
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003768 focus_surface = seat->keyboard->focus;
3769 if (!focus_surface)
3770 return;
3771
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003772 wl_signal_emit(&compositor->kill_signal, focus_surface);
3773
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003774 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003775 wl_client_get_credentials(client, &pid, NULL, NULL);
3776
3777 /* Skip clients that we launched ourselves (the credentials of
3778 * the socketpair is ours) */
3779 if (pid == getpid())
3780 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003781
Daniel Stone325fc2d2012-05-30 16:31:58 +01003782 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003783}
3784
3785static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003786workspace_up_binding(struct wl_seat *seat, uint32_t time,
3787 uint32_t key, void *data)
3788{
3789 struct desktop_shell *shell = data;
3790 unsigned int new_index = shell->workspaces.current;
3791
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003792 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003793 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003794 if (new_index != 0)
3795 new_index--;
3796
3797 change_workspace(shell, new_index);
3798}
3799
3800static void
3801workspace_down_binding(struct wl_seat *seat, uint32_t time,
3802 uint32_t key, void *data)
3803{
3804 struct desktop_shell *shell = data;
3805 unsigned int new_index = shell->workspaces.current;
3806
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003807 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003808 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003809 if (new_index < shell->workspaces.num - 1)
3810 new_index++;
3811
3812 change_workspace(shell, new_index);
3813}
3814
3815static void
3816workspace_f_binding(struct wl_seat *seat, uint32_t time,
3817 uint32_t key, void *data)
3818{
3819 struct desktop_shell *shell = data;
3820 unsigned int new_index;
3821
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003822 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003823 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003824 new_index = key - KEY_F1;
3825 if (new_index >= shell->workspaces.num)
3826 new_index = shell->workspaces.num - 1;
3827
3828 change_workspace(shell, new_index);
3829}
3830
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003831static void
3832workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3833 uint32_t key, void *data)
3834{
3835 struct desktop_shell *shell = data;
3836 unsigned int new_index = shell->workspaces.current;
3837
3838 if (shell->locked)
3839 return;
3840
3841 if (new_index != 0)
3842 new_index--;
3843
3844 take_surface_to_workspace_by_seat(shell, seat, new_index);
3845}
3846
3847static void
3848workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3849 uint32_t key, void *data)
3850{
3851 struct desktop_shell *shell = data;
3852 unsigned int new_index = shell->workspaces.current;
3853
3854 if (shell->locked)
3855 return;
3856
3857 if (new_index < shell->workspaces.num - 1)
3858 new_index++;
3859
3860 take_surface_to_workspace_by_seat(shell, seat, new_index);
3861}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003862
3863static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003864shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003865{
Tiago Vignattibe143262012-04-16 17:31:41 +03003866 struct desktop_shell *shell =
3867 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003868 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003869
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003870 if (shell->child.client)
3871 wl_client_destroy(shell->child.client);
3872
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003873 wl_list_remove(&shell->lock_listener.link);
3874 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003875 wl_list_remove(&shell->show_input_panel_listener.link);
3876 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003877
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003878 wl_array_for_each(ws, &shell->workspaces.array)
3879 workspace_destroy(*ws);
3880 wl_array_release(&shell->workspaces.array);
3881
Pekka Paalanen3c647232011-12-22 13:43:43 +02003882 free(shell->screensaver.path);
3883 free(shell);
3884}
3885
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003886static void
3887shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3888{
3889 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003890 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003891
3892 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003893 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3894 MODIFIER_CTRL | MODIFIER_ALT,
3895 terminate_binding, ec);
3896 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3897 click_to_activate_binding,
3898 shell);
3899 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3900 MODIFIER_SUPER | MODIFIER_ALT,
3901 surface_opacity_binding, NULL);
3902 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3903 MODIFIER_SUPER, zoom_axis_binding,
3904 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003905
3906 /* configurable bindings */
3907 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003908 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3909 zoom_key_binding, NULL);
3910 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3911 zoom_key_binding, NULL);
3912 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3913 shell);
3914 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3915 resize_binding, shell);
3916 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3917 rotate_binding, NULL);
3918 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3919 shell);
3920 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3921 ec);
3922 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3923 backlight_binding, ec);
3924 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3925 ec);
3926 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3927 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003928 weston_compositor_add_key_binding(ec, KEY_K, mod,
3929 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003930 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3931 workspace_up_binding, shell);
3932 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3933 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003934 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3935 workspace_move_surface_up_binding,
3936 shell);
3937 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3938 workspace_move_surface_down_binding,
3939 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003940
3941 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3942 if (shell->workspaces.num > 1) {
3943 num_workspace_bindings = shell->workspaces.num;
3944 if (num_workspace_bindings > 6)
3945 num_workspace_bindings = 6;
3946 for (i = 0; i < num_workspace_bindings; i++)
3947 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3948 workspace_f_binding,
3949 shell);
3950 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003951
3952 /* Debug bindings */
3953 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3954 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003955 weston_compositor_add_debug_binding(ec, KEY_F,
3956 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003957}
3958
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003959WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003960module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003961{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003962 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003963 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003964 struct workspace **pws;
3965 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003966 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003967
3968 shell = malloc(sizeof *shell);
3969 if (shell == NULL)
3970 return -1;
3971
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003972 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003973 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003974
3975 shell->destroy_listener.notify = shell_destroy;
3976 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3977 shell->lock_listener.notify = lock;
3978 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3979 shell->unlock_listener.notify = unlock;
3980 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003981 shell->show_input_panel_listener.notify = show_input_panels;
3982 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3983 shell->hide_input_panel_listener.notify = hide_input_panels;
3984 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003985 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003986 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003987 ec->shell_interface.create_shell_surface = create_shell_surface;
3988 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003989 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003990 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003991 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003992 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003993
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003994 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003995
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003996 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3997 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003998 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3999 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004000 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004001
4002 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004003 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004004
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05004005 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004006
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004007 for (i = 0; i < shell->workspaces.num; i++) {
4008 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4009 if (pws == NULL)
4010 return -1;
4011
4012 *pws = workspace_create();
4013 if (*pws == NULL)
4014 return -1;
4015 }
4016 activate_workspace(shell, 0);
4017
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004018 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004019 wl_list_init(&shell->workspaces.animation.link);
4020 shell->workspaces.animation.frame = animate_workspace_change_frame;
4021
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004022 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4023 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004024 return -1;
4025
Kristian Høgsberg75840622011-09-06 13:48:16 -04004026 if (wl_display_add_global(ec->wl_display,
4027 &desktop_shell_interface,
4028 shell, bind_desktop_shell) == NULL)
4029 return -1;
4030
Pekka Paalanen6e168112011-11-24 11:34:05 +02004031 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4032 shell, bind_screensaver) == NULL)
4033 return -1;
4034
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004035 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4036 shell, bind_input_panel) == NULL)
4037 return -1;
4038
Jonas Ådahle9d22502012-08-29 22:13:01 +02004039 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4040 shell, bind_workspace_manager) == NULL)
4041 return -1;
4042
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004043 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004044
4045 loop = wl_display_get_event_loop(ec->wl_display);
4046 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004047
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004048 wl_list_for_each(seat, &ec->seat_list, link)
4049 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004050
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004051 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004052
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004053 return 0;
4054}