blob: ea4daa89505bc7aea79f2aadd4f0d41be509a1dc [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
1903 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001904 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001905
Pekka Paalanen938269a2012-02-07 14:19:01 +02001906 weston_surface_update_transform(parent);
1907 if (parent->transform.enabled) {
1908 shsurf->popup.parent_transform.matrix =
1909 parent->transform.matrix;
1910 } else {
1911 /* construct x, y translation matrix */
1912 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03001913 shsurf->popup.parent_transform.matrix.type =
1914 WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen938269a2012-02-07 14:19:01 +02001915 shsurf->popup.parent_transform.matrix.d[12] =
1916 parent->geometry.x;
1917 shsurf->popup.parent_transform.matrix.d[13] =
1918 parent->geometry.y;
1919 }
1920 wl_list_insert(es->geometry.transformation_list.prev,
1921 &shsurf->popup.parent_transform.link);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001922
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001923 shsurf->popup.initial_up = 0;
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02001924 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1925 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001926
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001927 /* We don't require the grab to still be active, but if another
1928 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001929 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1930 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001931 } else {
1932 wl_shell_surface_send_popup_done(&shsurf->resource);
1933 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001934}
1935
1936static void
1937shell_surface_set_popup(struct wl_client *client,
1938 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001939 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001940 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941 struct wl_resource *parent_resource,
1942 int32_t x, int32_t y, uint32_t flags)
1943{
1944 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001945
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001946 shsurf->type = SHELL_SURFACE_POPUP;
1947 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001948 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001949 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001950 shsurf->popup.x = x;
1951 shsurf->popup.y = y;
1952}
1953
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001954static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001955 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001956 shell_surface_move,
1957 shell_surface_resize,
1958 shell_surface_set_toplevel,
1959 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001960 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001961 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001962 shell_surface_set_maximized,
1963 shell_surface_set_title,
1964 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001965};
1966
1967static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001968destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001969{
Daniel Stone37816df2012-05-16 18:45:18 +01001970 if (shsurf->popup.grab.pointer)
1971 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972
Alex Wubd3354b2012-04-17 17:20:49 +08001973 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1974 shell_surface_is_top_fullscreen(shsurf)) {
1975 weston_output_switch_mode(shsurf->fullscreen_output,
1976 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001977 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001978
Alex Wuaa08e2d2012-03-05 11:01:40 +08001979 if (shsurf->fullscreen.black_surface)
1980 weston_surface_destroy(shsurf->fullscreen.black_surface);
1981
Alex Wubd3354b2012-04-17 17:20:49 +08001982 /* As destroy_resource() use wl_list_for_each_safe(),
1983 * we can always remove the listener.
1984 */
1985 wl_list_remove(&shsurf->surface_destroy_listener.link);
1986 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001987 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001988
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001989 wl_list_remove(&shsurf->link);
1990 free(shsurf);
1991}
1992
1993static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001994shell_destroy_shell_surface(struct wl_resource *resource)
1995{
1996 struct shell_surface *shsurf = resource->data;
1997
1998 destroy_shell_surface(shsurf);
1999}
2000
2001static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002002shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002003{
2004 struct shell_surface *shsurf = container_of(listener,
2005 struct shell_surface,
2006 surface_destroy_listener);
2007
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002008 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002009 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002010 } else {
2011 wl_signal_emit(&shsurf->resource.destroy_signal,
2012 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002013 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002014 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002015}
2016
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002017static void
2018shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2019
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002020static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002021get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002022{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002023 if (surface->configure == shell_surface_configure)
2024 return surface->private;
2025 else
2026 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002027}
2028
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002029static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002030create_shell_surface(void *shell, struct weston_surface *surface,
2031 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002032{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002033 struct shell_surface *shsurf;
2034
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002035 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002036 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002037 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002038 }
2039
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002040 shsurf = calloc(1, sizeof *shsurf);
2041 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002042 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002043 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002044 }
2045
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002046 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002047 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002048
Tiago Vignattibc052c92012-04-19 16:18:18 +03002049 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002050 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002051 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002052 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002053 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002054 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2055 shsurf->fullscreen.framerate = 0;
2056 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002057 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002058 wl_list_init(&shsurf->fullscreen.transform.link);
2059
Tiago Vignattibc052c92012-04-19 16:18:18 +03002060 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002061 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2062 wl_signal_add(&surface->surface.resource.destroy_signal,
2063 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002064
2065 /* init link so its safe to always remove it in destroy_shell_surface */
2066 wl_list_init(&shsurf->link);
2067
Pekka Paalanen460099f2012-01-20 16:48:25 +02002068 /* empty when not in use */
2069 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002070 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002071
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002072 wl_list_init(&shsurf->workspace_transform.link);
2073
Pekka Paalanen98262232011-12-01 10:42:22 +02002074 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002075 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002076
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002077 shsurf->client = client;
2078
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002079 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002080}
2081
2082static void
2083shell_get_shell_surface(struct wl_client *client,
2084 struct wl_resource *resource,
2085 uint32_t id,
2086 struct wl_resource *surface_resource)
2087{
2088 struct weston_surface *surface = surface_resource->data;
2089 struct desktop_shell *shell = resource->data;
2090 struct shell_surface *shsurf;
2091
2092 if (get_shell_surface(surface)) {
2093 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002094 WL_DISPLAY_ERROR_INVALID_OBJECT,
2095 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002096 return;
2097 }
2098
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002099 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002100 if (!shsurf) {
2101 wl_resource_post_error(surface_resource,
2102 WL_DISPLAY_ERROR_INVALID_OBJECT,
2103 "surface->configure already set");
2104 return;
2105 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002106
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002107 shsurf->resource.destroy = shell_destroy_shell_surface;
2108 shsurf->resource.object.id = id;
2109 shsurf->resource.object.interface = &wl_shell_surface_interface;
2110 shsurf->resource.object.implementation =
2111 (void (**)(void)) &shell_surface_implementation;
2112 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002113
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002114 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002115}
2116
2117static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002118 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002119};
2120
Kristian Høgsberg07937562011-04-12 17:25:42 -04002121static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002122handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002123{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002124 struct desktop_shell *shell =
2125 container_of(proc, struct desktop_shell, screensaver.process);
2126 struct weston_output *output;
2127
Pekka Paalanen18027e52011-12-02 16:31:49 +02002128 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002129
2130 if (shell->locked)
2131 wl_list_for_each(output, &shell->compositor->output_list, link)
2132 if (output->set_dpms)
2133 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002134}
2135
2136static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002137launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002138{
2139 if (shell->screensaver.binding)
2140 return;
2141
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002142 if (!shell->screensaver.path)
2143 return;
2144
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002145 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002146 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002147 return;
2148 }
2149
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002150 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002151 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002152 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002153 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002154}
2155
2156static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002157terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002158{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002159 if (shell->screensaver.process.pid == 0)
2160 return;
2161
2162 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002163}
2164
2165static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002166configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002167{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002168 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002169
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002170 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2171 if (s->output == es->output && s != es) {
2172 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002173 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002174 }
2175 }
2176
2177 weston_surface_configure(es, es->output->x, es->output->y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002178 weston_surface_buffer_width(es),
2179 weston_surface_buffer_height(es));
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002180
2181 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002182 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002183 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002184 }
2185}
2186
2187static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002188background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2189{
2190 struct desktop_shell *shell = es->private;
2191
2192 configure_static_surface(es, &shell->background_layer);
2193}
2194
2195static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002196desktop_shell_set_background(struct wl_client *client,
2197 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002198 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002199 struct wl_resource *surface_resource)
2200{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002201 struct desktop_shell *shell = resource->data;
2202 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002203
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002204 if (surface->configure) {
2205 wl_resource_post_error(surface_resource,
2206 WL_DISPLAY_ERROR_INVALID_OBJECT,
2207 "surface role already assigned");
2208 return;
2209 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002210
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002211 surface->configure = background_configure;
2212 surface->private = shell;
2213 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002214 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002215 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002216 surface->output->width,
2217 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002218}
2219
2220static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002221panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2222{
2223 struct desktop_shell *shell = es->private;
2224
2225 configure_static_surface(es, &shell->panel_layer);
2226}
2227
2228static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002229desktop_shell_set_panel(struct wl_client *client,
2230 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002231 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002232 struct wl_resource *surface_resource)
2233{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002234 struct desktop_shell *shell = resource->data;
2235 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002236
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002237 if (surface->configure) {
2238 wl_resource_post_error(surface_resource,
2239 WL_DISPLAY_ERROR_INVALID_OBJECT,
2240 "surface role already assigned");
2241 return;
2242 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002243
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002244 surface->configure = panel_configure;
2245 surface->private = shell;
2246 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002247 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002248 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002249 surface->output->width,
2250 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002251}
2252
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002253static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002254lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2255{
2256 struct desktop_shell *shell = surface->private;
2257
2258 center_on_output(surface, get_default_output(shell->compositor));
2259
2260 if (!weston_surface_is_mapped(surface)) {
2261 wl_list_insert(&shell->lock_layer.surface_list,
2262 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002263 weston_surface_update_transform(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002264 weston_compositor_wake(shell->compositor);
2265 }
2266}
2267
2268static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002269handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002270{
Tiago Vignattibe143262012-04-16 17:31:41 +03002271 struct desktop_shell *shell =
2272 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002273
Martin Minarik6d118362012-06-07 18:01:59 +02002274 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002275 shell->lock_surface = NULL;
2276}
2277
2278static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002279desktop_shell_set_lock_surface(struct wl_client *client,
2280 struct wl_resource *resource,
2281 struct wl_resource *surface_resource)
2282{
Tiago Vignattibe143262012-04-16 17:31:41 +03002283 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002284 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002285
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002286 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002287
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002288 if (!shell->locked)
2289 return;
2290
Pekka Paalanen98262232011-12-01 10:42:22 +02002291 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002292
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002293 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2294 wl_signal_add(&surface_resource->destroy_signal,
2295 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002296
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002297 surface->configure = lock_surface_configure;
2298 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002299}
2300
2301static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002302resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002303{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002304 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002305
Pekka Paalanen77346a62011-11-30 16:26:35 +02002306 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002307
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002308 wl_list_remove(&shell->lock_layer.link);
2309 wl_list_insert(&shell->compositor->cursor_layer.link,
2310 &shell->fullscreen_layer.link);
2311 wl_list_insert(&shell->fullscreen_layer.link,
2312 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002313 if (shell->showing_input_panels) {
2314 wl_list_insert(&shell->panel_layer.link,
2315 &shell->input_panel_layer.link);
2316 wl_list_insert(&shell->input_panel_layer.link,
2317 &ws->layer.link);
2318 } else {
2319 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2320 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002321
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002322 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002323
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002324 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002325 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002326 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002327 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002328}
2329
2330static void
2331desktop_shell_unlock(struct wl_client *client,
2332 struct wl_resource *resource)
2333{
Tiago Vignattibe143262012-04-16 17:31:41 +03002334 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002335
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002336 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002337
2338 if (shell->locked)
2339 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002340}
2341
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002342static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002343desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002344 struct wl_resource *resource,
2345 struct wl_resource *surface_resource)
2346{
2347 struct desktop_shell *shell = resource->data;
2348
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002349 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002350}
2351
Kristian Høgsberg75840622011-09-06 13:48:16 -04002352static const struct desktop_shell_interface desktop_shell_implementation = {
2353 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002354 desktop_shell_set_panel,
2355 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002356 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002357 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002358};
2359
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002360static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002361get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002362{
2363 struct shell_surface *shsurf;
2364
2365 shsurf = get_shell_surface(surface);
2366 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002367 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002368 return shsurf->type;
2369}
2370
Kristian Høgsberg75840622011-09-06 13:48:16 -04002371static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002372move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002373{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002374 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002375 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002376 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002377
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002378 if (surface == NULL)
2379 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002380
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002381 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002382 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002383 return;
2384
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002385 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002386}
2387
2388static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002389resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002390{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002391 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002392 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002393 uint32_t edges = 0;
2394 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002395 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002396
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002397 if (surface == NULL)
2398 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002399
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002400 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002401 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002402 return;
2403
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002404 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002405 wl_fixed_to_int(seat->pointer->grab_x),
2406 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002407 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002408
Pekka Paalanen60921e52012-01-25 15:55:43 +02002409 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002410 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002411 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002412 edges |= 0;
2413 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002414 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002415
Pekka Paalanen60921e52012-01-25 15:55:43 +02002416 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002417 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002418 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002419 edges |= 0;
2420 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002421 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002422
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002423 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002424}
2425
2426static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002427surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002428 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002429{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002430 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002431 struct shell_surface *shsurf;
2432 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002433 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002434
2435 if (surface == NULL)
2436 return;
2437
2438 shsurf = get_shell_surface(surface);
2439 if (!shsurf)
2440 return;
2441
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002442 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002443
Scott Moreau02709af2012-05-22 01:54:10 -06002444 if (surface->alpha > 1.0)
2445 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002446 if (surface->alpha < step)
2447 surface->alpha = step;
2448
2449 surface->geometry.dirty = 1;
2450 weston_surface_damage(surface);
2451}
2452
2453static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002454do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002455 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002456{
Daniel Stone37816df2012-05-16 18:45:18 +01002457 struct weston_seat *ws = (struct weston_seat *) seat;
2458 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002459 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002460 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002461
2462 wl_list_for_each(output, &compositor->output_list, link) {
2463 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002464 wl_fixed_to_double(seat->pointer->x),
2465 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002466 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002467 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002468 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002469 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002470 increment = -output->zoom.increment;
2471 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002472 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002473 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002474 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002475 else
2476 increment = 0;
2477
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002478 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002479
Scott Moreaue6603982012-06-11 13:07:51 -06002480 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002481 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002482 else if (output->zoom.level > output->zoom.max_level)
2483 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002484 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002485 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002486 output->disable_planes++;
2487 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002488
Scott Moreaue6603982012-06-11 13:07:51 -06002489 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002490
Scott Moreau8dacaab2012-06-17 18:10:58 -06002491 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002492 }
2493 }
2494}
2495
Scott Moreauccbf29d2012-02-22 14:21:41 -07002496static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002497zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002498 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002499{
2500 do_zoom(seat, time, 0, axis, value);
2501}
2502
2503static void
2504zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2505 void *data)
2506{
2507 do_zoom(seat, time, key, 0, 0);
2508}
2509
2510static void
2511terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2512 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002513{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002514 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002515
Daniel Stone325fc2d2012-05-30 16:31:58 +01002516 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002517}
2518
2519static void
Scott Moreau447013d2012-02-18 05:05:29 -07002520rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002521 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002522{
2523 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002524 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002525 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002526 struct shell_surface *shsurf = rotate->base.shsurf;
2527 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002528 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002529
2530 if (!shsurf)
2531 return;
2532
2533 surface = shsurf->surface;
2534
2535 cx = 0.5f * surface->geometry.width;
2536 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002537
Daniel Stone37816df2012-05-16 18:45:18 +01002538 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2539 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002540 r = sqrtf(dx * dx + dy * dy);
2541
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002542 wl_list_remove(&shsurf->rotation.transform.link);
2543 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002544
2545 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002546 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002547 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002548
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002549 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002550 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002551
2552 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002553 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002554 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002555 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002556 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002557
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002558 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002559 &shsurf->surface->geometry.transformation_list,
2560 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002561 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002562 wl_list_init(&shsurf->rotation.transform.link);
2563 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002564 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002565 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002566
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002567 /* We need to adjust the position of the surface
2568 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002569 cposx = surface->geometry.x + cx;
2570 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002571 dposx = rotate->center.x - cposx;
2572 dposy = rotate->center.y - cposy;
2573 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002574 weston_surface_set_position(surface,
2575 surface->geometry.x + dposx,
2576 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002577 }
2578
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002579 /* Repaint implies weston_surface_update_transform(), which
2580 * lazily applies the damage due to rotation update.
2581 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002582 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002583}
2584
2585static void
Scott Moreau447013d2012-02-18 05:05:29 -07002586rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002587 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002588{
2589 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002590 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002591 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002592 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002593 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002594
Daniel Stone4dbadb12012-05-30 16:31:51 +01002595 if (pointer->button_count == 0 &&
2596 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002597 if (shsurf)
2598 weston_matrix_multiply(&shsurf->rotation.rotation,
2599 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002600 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002601 free(rotate);
2602 }
2603}
2604
Scott Moreau447013d2012-02-18 05:05:29 -07002605static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002606 noop_grab_focus,
2607 rotate_grab_motion,
2608 rotate_grab_button,
2609};
2610
2611static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002612surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002613{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002614 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002615 float dx, dy;
2616 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002617
Pekka Paalanen460099f2012-01-20 16:48:25 +02002618 rotate = malloc(sizeof *rotate);
2619 if (!rotate)
2620 return;
2621
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002622 weston_surface_to_global_float(surface->surface,
2623 surface->surface->geometry.width / 2,
2624 surface->surface->geometry.height / 2,
2625 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002626
Daniel Stone37816df2012-05-16 18:45:18 +01002627 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2628 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002629 r = sqrtf(dx * dx + dy * dy);
2630 if (r > 20.0f) {
2631 struct weston_matrix inverse;
2632
2633 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002634 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002635 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002636
2637 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002638 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002639 } else {
2640 weston_matrix_init(&surface->rotation.rotation);
2641 weston_matrix_init(&rotate->rotation);
2642 }
2643
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002644 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2645 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002646}
2647
2648static void
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002649rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2650 void *data)
2651{
2652 struct weston_surface *base_surface =
2653 (struct weston_surface *) seat->pointer->focus;
2654 struct shell_surface *surface;
2655
2656 if (base_surface == NULL)
2657 return;
2658
2659 surface = get_shell_surface(base_surface);
2660 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2661 return;
2662
2663 surface_rotate(surface, seat);
2664}
2665
2666static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002667lower_fullscreen_layer(struct desktop_shell *shell)
2668{
2669 struct workspace *ws;
2670 struct weston_surface *surface, *prev;
2671
2672 ws = get_current_workspace(shell);
2673 wl_list_for_each_reverse_safe(surface, prev,
2674 &shell->fullscreen_layer.surface_list,
2675 layer_link)
2676 weston_surface_restack(surface, &ws->layer.surface_list);
2677}
2678
2679static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002680activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002681 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002682{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002683 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002684 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002685
Daniel Stone37816df2012-05-16 18:45:18 +01002686 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002687
Jonas Ådahl8538b222012-08-29 22:13:03 +02002688 state = ensure_focus_state(shell, seat);
2689 if (state == NULL)
2690 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002691
2692 state->keyboard_focus = es;
2693 wl_list_remove(&state->surface_destroy_listener.link);
2694 wl_signal_add(&es->surface.resource.destroy_signal,
2695 &state->surface_destroy_listener);
2696
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002697 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002698 case SHELL_SURFACE_FULLSCREEN:
2699 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002700 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002701 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002702 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002703 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002704 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002705 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002706 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002707 }
2708}
2709
Alex Wu21858432012-04-01 20:13:08 +08002710/* no-op func for checking black surface */
2711static void
2712black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2713{
2714}
2715
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002716static bool
Alex Wu21858432012-04-01 20:13:08 +08002717is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2718{
2719 if (es->configure == black_surface_configure) {
2720 if (fs_surface)
2721 *fs_surface = (struct weston_surface *)es->private;
2722 return true;
2723 }
2724 return false;
2725}
2726
Kristian Høgsberg75840622011-09-06 13:48:16 -04002727static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002728click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002729 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002730{
Daniel Stone37816df2012-05-16 18:45:18 +01002731 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002732 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002733 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002734 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002735
Daniel Stone37816df2012-05-16 18:45:18 +01002736 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002737 if (!focus)
2738 return;
2739
Alex Wu21858432012-04-01 20:13:08 +08002740 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002741 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002742
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002743 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2744 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002745
Daniel Stone325fc2d2012-05-30 16:31:58 +01002746 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002747 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002748}
2749
2750static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002751lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002752{
Tiago Vignattibe143262012-04-16 17:31:41 +03002753 struct desktop_shell *shell =
2754 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002755 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002756 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002757
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002758 if (shell->locked) {
2759 wl_list_for_each(output, &shell->compositor->output_list, link)
2760 /* TODO: find a way to jump to other DPMS levels */
2761 if (output->set_dpms)
2762 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002763 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002764 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002765
2766 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002767
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002768 /* Hide all surfaces by removing the fullscreen, panel and
2769 * toplevel layers. This way nothing else can show or receive
2770 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002771
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002772 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002773 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002774 if (shell->showing_input_panels)
2775 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002776 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002777 wl_list_insert(&shell->compositor->cursor_layer.link,
2778 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002779
Pekka Paalanen77346a62011-11-30 16:26:35 +02002780 launch_screensaver(shell);
2781
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002782 /* TODO: disable bindings that should not work while locked. */
2783
2784 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002785}
2786
2787static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002788unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002789{
Tiago Vignattibe143262012-04-16 17:31:41 +03002790 struct desktop_shell *shell =
2791 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002792
Pekka Paalanend81c2162011-11-16 13:47:34 +02002793 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002794 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002795 return;
2796 }
2797
2798 /* If desktop-shell client has gone away, unlock immediately. */
2799 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002800 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002801 return;
2802 }
2803
2804 if (shell->prepare_event_sent)
2805 return;
2806
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002807 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002808 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002809}
2810
2811static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002812show_input_panels(struct wl_listener *listener, void *data)
2813{
2814 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002815 container_of(listener, struct desktop_shell,
2816 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002817 struct input_panel_surface *surface, *next;
2818 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002819
Jan Arne Petersen451a9712013-02-11 15:10:11 +01002820 if (shell->showing_input_panels)
2821 return;
2822
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002823 shell->showing_input_panels = true;
2824
Jan Arne Petersencf18a322012-11-07 15:32:54 +01002825 if (!shell->locked)
2826 wl_list_insert(&shell->panel_layer.link,
2827 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002828
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002829 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002830 &shell->input_panel.surfaces, link) {
2831 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002832 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002833 &ws->layer_link);
Jan Arne Petersen1428b8c2012-09-26 14:39:45 +02002834 ws->geometry.dirty = 1;
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002835 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002836 weston_surface_damage(ws);
2837 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002838 }
2839}
2840
2841static void
2842hide_input_panels(struct wl_listener *listener, void *data)
2843{
2844 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002845 container_of(listener, struct desktop_shell,
2846 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002847 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002848
Jan Arne Petersen61381972013-01-31 15:52:21 +01002849 if (!shell->showing_input_panels)
2850 return;
2851
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002852 shell->showing_input_panels = false;
2853
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01002854 if (!shell->locked)
2855 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002856
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002857 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002858 &shell->input_panel_layer.surface_list, layer_link)
2859 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002860}
2861
2862static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002863center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002864{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002865 int32_t width = weston_surface_buffer_width(surface);
2866 int32_t height = weston_surface_buffer_height(surface);
2867 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002868
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002869 x = output->x + (output->width - width) / 2;
2870 y = output->y + (output->height - height) / 2;
2871
2872 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002873}
2874
2875static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002876weston_surface_set_initial_position (struct weston_surface *surface,
2877 struct desktop_shell *shell)
2878{
2879 struct weston_compositor *compositor = shell->compositor;
2880 int ix = 0, iy = 0;
2881 int range_x, range_y;
2882 int dx, dy, x, y, panel_height;
2883 struct weston_output *output, *target_output = NULL;
2884 struct weston_seat *seat;
2885
2886 /* As a heuristic place the new window on the same output as the
2887 * pointer. Falling back to the output containing 0, 0.
2888 *
2889 * TODO: Do something clever for touch too?
2890 */
2891 wl_list_for_each(seat, &compositor->seat_list, link) {
2892 if (seat->has_pointer) {
2893 ix = wl_fixed_to_int(seat->pointer.x);
2894 iy = wl_fixed_to_int(seat->pointer.y);
2895 break;
2896 }
2897 }
2898
2899 wl_list_for_each(output, &compositor->output_list, link) {
2900 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2901 target_output = output;
2902 break;
2903 }
2904 }
2905
2906 if (!target_output) {
2907 weston_surface_set_position(surface, 10 + random() % 400,
2908 10 + random() % 400);
2909 return;
2910 }
2911
2912 /* Valid range within output where the surface will still be onscreen.
2913 * If this is negative it means that the surface is bigger than
2914 * output.
2915 */
2916 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002917 range_x = target_output->width - surface->geometry.width;
2918 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002919 surface->geometry.height;
2920
Rob Bradford4cb88c72012-08-13 15:18:44 +01002921 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002922 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002923 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002924 dx = 0;
2925
2926 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002927 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002928 else
2929 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002930
2931 x = target_output->x + dx;
2932 y = target_output->y + dy;
2933
2934 weston_surface_set_position (surface, x, y);
2935}
2936
2937static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002938map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002939 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002940{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002941 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002942 struct shell_surface *shsurf = get_shell_surface(surface);
2943 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002944 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002945 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002946 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002947 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002948
Pekka Paalanen60921e52012-01-25 15:55:43 +02002949 surface->geometry.width = width;
2950 surface->geometry.height = height;
2951 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002952
2953 /* initial positioning, see also configure() */
2954 switch (surface_type) {
2955 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002956 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002957 break;
Alex Wu4539b082012-03-01 12:57:46 +08002958 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002959 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002960 shell_map_fullscreen(shsurf);
2961 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002962 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002963 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002964 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002965 weston_surface_set_position(surface, shsurf->output->x,
2966 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002967 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002968 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002969 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00002970 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002971 case SHELL_SURFACE_NONE:
2972 weston_surface_set_position(surface,
2973 surface->geometry.x + sx,
2974 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002975 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002976 default:
2977 ;
2978 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002979
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002980 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002981 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002982 case SHELL_SURFACE_POPUP:
2983 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002984 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002985 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2986 break;
Alex Wu4539b082012-03-01 12:57:46 +08002987 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002988 case SHELL_SURFACE_NONE:
2989 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002990 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002991 ws = get_current_workspace(shell);
2992 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002993 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002994 }
2995
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002996 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002997 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002998 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2999 surface->output = shsurf->output;
3000 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003001
Juan Zhao7bb92f02011-12-15 11:31:51 -05003002 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003003 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003004 if (shsurf->transient.flags ==
3005 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3006 break;
3007 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003008 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003009 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003010 if (!shell->locked) {
3011 wl_list_for_each(seat, &compositor->seat_list, link)
3012 activate(shell, surface, seat);
3013 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003014 break;
3015 default:
3016 break;
3017 }
3018
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003019 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003020 {
3021 switch (shell->win_animation_type) {
3022 case ANIMATION_FADE:
3023 weston_fade_run(surface, NULL, NULL);
3024 break;
3025 case ANIMATION_ZOOM:
3026 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3027 break;
3028 default:
3029 break;
3030 }
3031 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003032}
3033
3034static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003035configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003036 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003037{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003038 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3039 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003040
Pekka Paalanen77346a62011-11-30 16:26:35 +02003041 shsurf = get_shell_surface(surface);
3042 if (shsurf)
3043 surface_type = shsurf->type;
3044
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003045 surface->geometry.x = x;
3046 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02003047 surface->geometry.width = width;
3048 surface->geometry.height = height;
3049 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003050
3051 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003052 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003053 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003054 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003055 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003056 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003057 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003058 surface->geometry.x = surface->output->x;
3059 surface->geometry.y = surface->output->y +
3060 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003061 break;
Alex Wu4539b082012-03-01 12:57:46 +08003062 case SHELL_SURFACE_TOPLEVEL:
3063 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003064 default:
3065 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003066 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003067
Alex Wu4539b082012-03-01 12:57:46 +08003068 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003069 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003070 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003071
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003072 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003073 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003074 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003075}
3076
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003077static void
3078shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3079{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003080 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003081 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003082 int32_t width = weston_surface_buffer_width(es);
3083 int32_t height = weston_surface_buffer_height(es);
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003084 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003085
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003086 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003087 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003088 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003089 type_changed = 1;
3090 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003091
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003092 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003093 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003094 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003095 es->geometry.width != width ||
3096 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003097 float from_x, from_y;
3098 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003099
3100 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3101 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3102 configure(shell, es,
3103 es->geometry.x + to_x - from_x,
3104 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003105 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003106 }
3107}
3108
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003109static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003110
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003111static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003112desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003113{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003114 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003115 struct desktop_shell *shell =
3116 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003117
3118 shell->child.process.pid = 0;
3119 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003120
3121 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3122 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003123 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003124 shell->child.deathstamp = time;
3125 shell->child.deathcount = 0;
3126 }
3127
3128 shell->child.deathcount++;
3129 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003130 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003131 return;
3132 }
3133
Martin Minarik6d118362012-06-07 18:01:59 +02003134 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003135 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003136}
3137
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003138static void
3139launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003140{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003141 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003142 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003143
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003144 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003145 &shell->child.process,
3146 shell_exe,
3147 desktop_shell_sigchld);
3148
3149 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003150 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003151}
3152
3153static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003154bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3155{
Tiago Vignattibe143262012-04-16 17:31:41 +03003156 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003157
3158 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003159 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003160}
3161
Kristian Høgsberg75840622011-09-06 13:48:16 -04003162static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003163unbind_desktop_shell(struct wl_resource *resource)
3164{
Tiago Vignattibe143262012-04-16 17:31:41 +03003165 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003166
3167 if (shell->locked)
3168 resume_desktop(shell);
3169
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003170 shell->child.desktop_shell = NULL;
3171 shell->prepare_event_sent = false;
3172 free(resource);
3173}
3174
3175static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003176bind_desktop_shell(struct wl_client *client,
3177 void *data, uint32_t version, uint32_t id)
3178{
Tiago Vignattibe143262012-04-16 17:31:41 +03003179 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003180 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003181
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003182 resource = wl_client_add_object(client, &desktop_shell_interface,
3183 &desktop_shell_implementation,
3184 id, shell);
3185
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003186 if (client == shell->child.client) {
3187 resource->destroy = unbind_desktop_shell;
3188 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003189 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003190 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003191
3192 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3193 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003194 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003195}
3196
Pekka Paalanen6e168112011-11-24 11:34:05 +02003197static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003198screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3199{
3200 struct desktop_shell *shell = surface->private;
3201
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003202 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003203 if (!shell->locked)
3204 return;
3205
3206 center_on_output(surface, surface->output);
3207
3208 if (wl_list_empty(&surface->layer_link)) {
3209 wl_list_insert(shell->lock_layer.surface_list.prev,
3210 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003211 weston_surface_update_transform(surface);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003212 shell->compositor->idle_time = shell->screensaver.duration;
3213 weston_compositor_wake(shell->compositor);
3214 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3215 }
3216}
3217
3218static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003219screensaver_set_surface(struct wl_client *client,
3220 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003221 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003222 struct wl_resource *output_resource)
3223{
Tiago Vignattibe143262012-04-16 17:31:41 +03003224 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003225 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003226 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003227
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003228 surface->configure = screensaver_configure;
3229 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003230 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003231}
3232
3233static const struct screensaver_interface screensaver_implementation = {
3234 screensaver_set_surface
3235};
3236
3237static void
3238unbind_screensaver(struct wl_resource *resource)
3239{
Tiago Vignattibe143262012-04-16 17:31:41 +03003240 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003241
Pekka Paalanen77346a62011-11-30 16:26:35 +02003242 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003243 free(resource);
3244}
3245
3246static void
3247bind_screensaver(struct wl_client *client,
3248 void *data, uint32_t version, uint32_t id)
3249{
Tiago Vignattibe143262012-04-16 17:31:41 +03003250 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003251 struct wl_resource *resource;
3252
3253 resource = wl_client_add_object(client, &screensaver_interface,
3254 &screensaver_implementation,
3255 id, shell);
3256
Pekka Paalanen77346a62011-11-30 16:26:35 +02003257 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003258 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003259 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003260 return;
3261 }
3262
3263 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3264 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003265 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003266}
3267
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003268static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003269input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3270{
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003271 struct weston_mode *mode;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003272 int32_t width = weston_surface_buffer_width(surface);
3273 int32_t height = weston_surface_buffer_height(surface);
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003274 float x, y;
3275
3276 if (!weston_surface_is_mapped(surface))
3277 return;
3278
3279 mode = surface->output->current;
3280 x = (mode->width - width) / 2;
3281 y = mode->height - height;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003282
3283 /* Don't map the input panel here, wait for
3284 * show_input_panels signal. */
3285
3286 weston_surface_configure(surface,
3287 surface->output->x + x,
3288 surface->output->y + y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003289 width, height);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003290}
3291
3292static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003293destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003294{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003295 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003296 wl_list_remove(&input_panel_surface->link);
3297
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003298 input_panel_surface->surface->configure = NULL;
3299
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003300 free(input_panel_surface);
3301}
3302
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003303static struct input_panel_surface *
3304get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003305{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003306 if (surface->configure == input_panel_configure) {
3307 return surface->private;
3308 } else {
3309 return NULL;
3310 }
3311}
3312
3313static void
3314input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3315{
3316 struct input_panel_surface *ipsurface = container_of(listener,
3317 struct input_panel_surface,
3318 surface_destroy_listener);
3319
3320 if (ipsurface->resource.client) {
3321 wl_resource_destroy(&ipsurface->resource);
3322 } else {
3323 wl_signal_emit(&ipsurface->resource.destroy_signal,
3324 &ipsurface->resource);
3325 destroy_input_panel_surface(ipsurface);
3326 }
3327}
3328static struct input_panel_surface *
3329create_input_panel_surface(struct desktop_shell *shell,
3330 struct weston_surface *surface)
3331{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003332 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003333
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003334 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3335 if (!input_panel_surface)
3336 return NULL;
3337
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003338 surface->configure = input_panel_configure;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003339 surface->private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003340
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003341 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003342
3343 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003344
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003345 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3346 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3347 wl_signal_add(&surface->surface.resource.destroy_signal,
3348 &input_panel_surface->surface_destroy_listener);
3349
3350 wl_list_init(&input_panel_surface->link);
3351
3352 return input_panel_surface;
3353}
3354
3355static void
3356input_panel_surface_set_toplevel(struct wl_client *client,
3357 struct wl_resource *resource,
3358 uint32_t position)
3359{
3360 struct input_panel_surface *input_panel_surface = resource->data;
3361 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003362
3363 wl_list_insert(&shell->input_panel.surfaces,
3364 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003365}
3366
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003367static const struct input_panel_surface_interface input_panel_surface_implementation = {
3368 input_panel_surface_set_toplevel
3369};
3370
3371static void
3372destroy_input_panel_surface_resource(struct wl_resource *resource)
3373{
3374 struct input_panel_surface *ipsurf = resource->data;
3375
3376 destroy_input_panel_surface(ipsurf);
3377}
3378
3379static void
3380input_panel_get_input_panel_surface(struct wl_client *client,
3381 struct wl_resource *resource,
3382 uint32_t id,
3383 struct wl_resource *surface_resource)
3384{
3385 struct weston_surface *surface = surface_resource->data;
3386 struct desktop_shell *shell = resource->data;
3387 struct input_panel_surface *ipsurf;
3388
3389 if (get_input_panel_surface(surface)) {
3390 wl_resource_post_error(surface_resource,
3391 WL_DISPLAY_ERROR_INVALID_OBJECT,
3392 "input_panel::get_input_panel_surface already requested");
3393 return;
3394 }
3395
3396 ipsurf = create_input_panel_surface(shell, surface);
3397 if (!ipsurf) {
3398 wl_resource_post_error(surface_resource,
3399 WL_DISPLAY_ERROR_INVALID_OBJECT,
3400 "surface->configure already set");
3401 return;
3402 }
3403
3404 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3405 ipsurf->resource.object.id = id;
3406 ipsurf->resource.object.interface = &input_panel_surface_interface;
3407 ipsurf->resource.object.implementation =
3408 (void (**)(void)) &input_panel_surface_implementation;
3409 ipsurf->resource.data = ipsurf;
3410
3411 wl_client_add_resource(client, &ipsurf->resource);
3412}
3413
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003414static const struct input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003415 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003416};
3417
3418static void
3419unbind_input_panel(struct wl_resource *resource)
3420{
3421 struct desktop_shell *shell = resource->data;
3422
3423 shell->input_panel.binding = NULL;
3424 free(resource);
3425}
3426
3427static void
3428bind_input_panel(struct wl_client *client,
3429 void *data, uint32_t version, uint32_t id)
3430{
3431 struct desktop_shell *shell = data;
3432 struct wl_resource *resource;
3433
3434 resource = wl_client_add_object(client, &input_panel_interface,
3435 &input_panel_implementation,
3436 id, shell);
3437
3438 if (shell->input_panel.binding == NULL) {
3439 resource->destroy = unbind_input_panel;
3440 shell->input_panel.binding = resource;
3441 return;
3442 }
3443
3444 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3445 "interface object already bound");
3446 wl_resource_destroy(resource);
3447}
3448
Kristian Høgsberg07045392012-02-19 18:52:44 -05003449struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003450 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003451 struct weston_surface *current;
3452 struct wl_listener listener;
3453 struct wl_keyboard_grab grab;
3454};
3455
3456static void
3457switcher_next(struct switcher *switcher)
3458{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003459 struct weston_surface *surface;
3460 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003461 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003462 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003463
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003464 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003465 switch (get_shell_surface_type(surface)) {
3466 case SHELL_SURFACE_TOPLEVEL:
3467 case SHELL_SURFACE_FULLSCREEN:
3468 case SHELL_SURFACE_MAXIMIZED:
3469 if (first == NULL)
3470 first = surface;
3471 if (prev == switcher->current)
3472 next = surface;
3473 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003474 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003475 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003476 weston_surface_damage(surface);
3477 break;
3478 default:
3479 break;
3480 }
Alex Wu1659daa2012-04-01 20:13:09 +08003481
3482 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003483 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003484 surface->geometry.dirty = 1;
3485 weston_surface_damage(surface);
3486 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003487 }
3488
3489 if (next == NULL)
3490 next = first;
3491
Alex Wu07b26062012-03-12 16:06:01 +08003492 if (next == NULL)
3493 return;
3494
Kristian Høgsberg07045392012-02-19 18:52:44 -05003495 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003496 wl_signal_add(&next->surface.resource.destroy_signal,
3497 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003498
3499 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003500 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003501
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003502 shsurf = get_shell_surface(switcher->current);
3503 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003504 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003505}
3506
3507static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003508switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003509{
3510 struct switcher *switcher =
3511 container_of(listener, struct switcher, listener);
3512
3513 switcher_next(switcher);
3514}
3515
3516static void
Daniel Stone351eb612012-05-31 15:27:47 -04003517switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003518{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003519 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003520 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003521 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003522 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003523
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003524 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003525 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003526 weston_surface_damage(surface);
3527 }
3528
Alex Wu07b26062012-03-12 16:06:01 +08003529 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003530 activate(switcher->shell, switcher->current,
3531 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003532 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003533 wl_keyboard_end_grab(keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003534 if (weston_keyboard->input_method_resource)
3535 keyboard->grab = &weston_keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003536 free(switcher);
3537}
3538
3539static void
3540switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003541 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003542{
3543 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003544 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003545
Daniel Stonec9785ea2012-05-30 16:31:52 +01003546 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003547 switcher_next(switcher);
3548}
3549
3550static void
3551switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3552 uint32_t mods_depressed, uint32_t mods_latched,
3553 uint32_t mods_locked, uint32_t group)
3554{
3555 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003556 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003557
Daniel Stone351eb612012-05-31 15:27:47 -04003558 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3559 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003560}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003561
3562static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003563 switcher_key,
3564 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003565};
3566
3567static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003568switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3569 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003570{
Tiago Vignattibe143262012-04-16 17:31:41 +03003571 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003572 struct switcher *switcher;
3573
3574 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003575 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003576 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003577 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003578 wl_list_init(&switcher->listener.link);
3579
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003580 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003581 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003582 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3583 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003584 switcher_next(switcher);
3585}
3586
Pekka Paalanen3c647232011-12-22 13:43:43 +02003587static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003588backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3589 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003590{
3591 struct weston_compositor *compositor = data;
3592 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003593 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003594
3595 /* TODO: we're limiting to simple use cases, where we assume just
3596 * control on the primary display. We'd have to extend later if we
3597 * ever get support for setting backlights on random desktop LCD
3598 * panels though */
3599 output = get_default_output(compositor);
3600 if (!output)
3601 return;
3602
3603 if (!output->set_backlight)
3604 return;
3605
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003606 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3607 backlight_new = output->backlight_current - 25;
3608 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3609 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003610
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003611 if (backlight_new < 5)
3612 backlight_new = 5;
3613 if (backlight_new > 255)
3614 backlight_new = 255;
3615
3616 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003617 output->set_backlight(output, output->backlight_current);
3618}
3619
3620static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003621fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3622 void *data)
3623{
3624 struct desktop_shell *shell = data;
3625 struct weston_compositor *compositor = shell->compositor;
3626 compositor->fan_debug = !compositor->fan_debug;
3627 weston_compositor_damage_all(compositor);
3628}
3629
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003630struct debug_binding_grab {
3631 struct wl_keyboard_grab grab;
3632 struct weston_seat *seat;
3633 uint32_t key[2];
3634 int key_released[2];
3635};
3636
3637static void
3638debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3639 uint32_t key, uint32_t state)
3640{
3641 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3642 struct wl_resource *resource;
3643 struct wl_display *display;
3644 uint32_t serial;
3645 int send = 0, terminate = 0;
3646 int check_binding = 1;
3647 int i;
3648
3649 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3650 /* Do not run bindings on key releases */
3651 check_binding = 0;
3652
3653 for (i = 0; i < 2; i++)
3654 if (key == db->key[i])
3655 db->key_released[i] = 1;
3656
3657 if (db->key_released[0] && db->key_released[1]) {
3658 /* All key releases been swalled so end the grab */
3659 terminate = 1;
3660 } else if (key != db->key[0] && key != db->key[1]) {
3661 /* Should not swallow release of other keys */
3662 send = 1;
3663 }
3664 } else if (key == db->key[0] && !db->key_released[0]) {
3665 /* Do not check bindings for the first press of the binding
3666 * key. This allows it to be used as a debug shortcut.
3667 * We still need to swallow this event. */
3668 check_binding = 0;
3669 } else if (db->key[1]) {
3670 /* If we already ran a binding don't process another one since
3671 * we can't keep track of all the binding keys that were
3672 * pressed in order to swallow the release events. */
3673 send = 1;
3674 check_binding = 0;
3675 }
3676
3677 if (check_binding) {
3678 struct weston_compositor *ec = db->seat->compositor;
3679
3680 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3681 key, state)) {
3682 /* We ran a binding so swallow the press and keep the
3683 * grab to swallow the released too. */
3684 send = 0;
3685 terminate = 0;
3686 db->key[1] = key;
3687 } else {
3688 /* Terminate the grab since the key pressed is not a
3689 * debug binding key. */
3690 send = 1;
3691 terminate = 1;
3692 }
3693 }
3694
3695 if (send) {
3696 resource = grab->keyboard->focus_resource;
3697
3698 if (resource) {
3699 display = wl_client_get_display(resource->client);
3700 serial = wl_display_next_serial(display);
3701 wl_keyboard_send_key(resource, serial, time, key, state);
3702 }
3703 }
3704
3705 if (terminate) {
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003706 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003707 wl_keyboard_end_grab(grab->keyboard);
Jan Arne Petersena75a7892013-01-16 21:26:50 +01003708 if (weston_keyboard->input_method_resource)
3709 grab->keyboard->grab = &weston_keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003710 free(db);
3711 }
3712}
3713
3714static void
3715debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3716 uint32_t mods_depressed, uint32_t mods_latched,
3717 uint32_t mods_locked, uint32_t group)
3718{
3719 struct wl_resource *resource;
3720
3721 resource = grab->keyboard->focus_resource;
3722 if (!resource)
3723 return;
3724
3725 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3726 mods_latched, mods_locked, group);
3727}
3728
3729struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3730 debug_binding_key,
3731 debug_binding_modifiers
3732};
3733
3734static void
3735debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3736{
3737 struct debug_binding_grab *grab;
3738
3739 grab = calloc(1, sizeof *grab);
3740 if (!grab)
3741 return;
3742
3743 grab->seat = (struct weston_seat *) seat;
3744 grab->key[0] = key;
3745 grab->grab.interface = &debug_binding_keyboard_grab;
3746 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3747}
3748
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003749static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003750force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3751 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003752{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003753 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003754 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003755 struct desktop_shell *shell = data;
3756 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003757 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003758
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003759 focus_surface = seat->keyboard->focus;
3760 if (!focus_surface)
3761 return;
3762
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003763 wl_signal_emit(&compositor->kill_signal, focus_surface);
3764
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003765 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03003766 wl_client_get_credentials(client, &pid, NULL, NULL);
3767
3768 /* Skip clients that we launched ourselves (the credentials of
3769 * the socketpair is ours) */
3770 if (pid == getpid())
3771 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003772
Daniel Stone325fc2d2012-05-30 16:31:58 +01003773 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003774}
3775
3776static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003777workspace_up_binding(struct wl_seat *seat, uint32_t time,
3778 uint32_t key, void *data)
3779{
3780 struct desktop_shell *shell = data;
3781 unsigned int new_index = shell->workspaces.current;
3782
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003783 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003784 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003785 if (new_index != 0)
3786 new_index--;
3787
3788 change_workspace(shell, new_index);
3789}
3790
3791static void
3792workspace_down_binding(struct wl_seat *seat, uint32_t time,
3793 uint32_t key, void *data)
3794{
3795 struct desktop_shell *shell = data;
3796 unsigned int new_index = shell->workspaces.current;
3797
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003798 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003799 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003800 if (new_index < shell->workspaces.num - 1)
3801 new_index++;
3802
3803 change_workspace(shell, new_index);
3804}
3805
3806static void
3807workspace_f_binding(struct wl_seat *seat, uint32_t time,
3808 uint32_t key, void *data)
3809{
3810 struct desktop_shell *shell = data;
3811 unsigned int new_index;
3812
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003813 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003814 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003815 new_index = key - KEY_F1;
3816 if (new_index >= shell->workspaces.num)
3817 new_index = shell->workspaces.num - 1;
3818
3819 change_workspace(shell, new_index);
3820}
3821
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003822static void
3823workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3824 uint32_t key, void *data)
3825{
3826 struct desktop_shell *shell = data;
3827 unsigned int new_index = shell->workspaces.current;
3828
3829 if (shell->locked)
3830 return;
3831
3832 if (new_index != 0)
3833 new_index--;
3834
3835 take_surface_to_workspace_by_seat(shell, seat, new_index);
3836}
3837
3838static void
3839workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3840 uint32_t key, void *data)
3841{
3842 struct desktop_shell *shell = data;
3843 unsigned int new_index = shell->workspaces.current;
3844
3845 if (shell->locked)
3846 return;
3847
3848 if (new_index < shell->workspaces.num - 1)
3849 new_index++;
3850
3851 take_surface_to_workspace_by_seat(shell, seat, new_index);
3852}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003853
3854static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003855shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003856{
Tiago Vignattibe143262012-04-16 17:31:41 +03003857 struct desktop_shell *shell =
3858 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003859 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003860
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003861 if (shell->child.client)
3862 wl_client_destroy(shell->child.client);
3863
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003864 wl_list_remove(&shell->lock_listener.link);
3865 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003866 wl_list_remove(&shell->show_input_panel_listener.link);
3867 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003868
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003869 wl_array_for_each(ws, &shell->workspaces.array)
3870 workspace_destroy(*ws);
3871 wl_array_release(&shell->workspaces.array);
3872
Pekka Paalanen3c647232011-12-22 13:43:43 +02003873 free(shell->screensaver.path);
3874 free(shell);
3875}
3876
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003877static void
3878shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3879{
3880 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003881 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003882
3883 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003884 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3885 MODIFIER_CTRL | MODIFIER_ALT,
3886 terminate_binding, ec);
3887 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3888 click_to_activate_binding,
3889 shell);
3890 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3891 MODIFIER_SUPER | MODIFIER_ALT,
3892 surface_opacity_binding, NULL);
3893 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3894 MODIFIER_SUPER, zoom_axis_binding,
3895 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003896
3897 /* configurable bindings */
3898 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003899 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3900 zoom_key_binding, NULL);
3901 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3902 zoom_key_binding, NULL);
3903 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3904 shell);
3905 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3906 resize_binding, shell);
3907 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3908 rotate_binding, NULL);
3909 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3910 shell);
3911 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3912 ec);
3913 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3914 backlight_binding, ec);
3915 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3916 ec);
3917 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3918 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003919 weston_compositor_add_key_binding(ec, KEY_K, mod,
3920 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003921 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3922 workspace_up_binding, shell);
3923 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3924 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02003925 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3926 workspace_move_surface_up_binding,
3927 shell);
3928 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3929 workspace_move_surface_down_binding,
3930 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003931
3932 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3933 if (shell->workspaces.num > 1) {
3934 num_workspace_bindings = shell->workspaces.num;
3935 if (num_workspace_bindings > 6)
3936 num_workspace_bindings = 6;
3937 for (i = 0; i < num_workspace_bindings; i++)
3938 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3939 workspace_f_binding,
3940 shell);
3941 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003942
3943 /* Debug bindings */
3944 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3945 debug_binding, shell);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003946 weston_compositor_add_debug_binding(ec, KEY_F,
3947 fan_debug_repaint_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003948}
3949
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003950WL_EXPORT int
Kristian Høgsbergb00a9d32012-09-11 14:06:27 -04003951module_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003952{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003953 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003954 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003955 struct workspace **pws;
3956 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003957 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003958
3959 shell = malloc(sizeof *shell);
3960 if (shell == NULL)
3961 return -1;
3962
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003963 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003964 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003965
3966 shell->destroy_listener.notify = shell_destroy;
3967 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3968 shell->lock_listener.notify = lock;
3969 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3970 shell->unlock_listener.notify = unlock;
3971 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003972 shell->show_input_panel_listener.notify = show_input_panels;
3973 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3974 shell->hide_input_panel_listener.notify = hide_input_panels;
3975 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003976 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003977 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003978 ec->shell_interface.create_shell_surface = create_shell_surface;
3979 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003980 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003981 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003982 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003983 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003984
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003985 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003986
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003987 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3988 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003989 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3990 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003991 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003992
3993 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02003994 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003995
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003996 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003997
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003998 for (i = 0; i < shell->workspaces.num; i++) {
3999 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4000 if (pws == NULL)
4001 return -1;
4002
4003 *pws = workspace_create();
4004 if (*pws == NULL)
4005 return -1;
4006 }
4007 activate_workspace(shell, 0);
4008
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004009 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004010 wl_list_init(&shell->workspaces.animation.link);
4011 shell->workspaces.animation.frame = animate_workspace_change_frame;
4012
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004013 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4014 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004015 return -1;
4016
Kristian Høgsberg75840622011-09-06 13:48:16 -04004017 if (wl_display_add_global(ec->wl_display,
4018 &desktop_shell_interface,
4019 shell, bind_desktop_shell) == NULL)
4020 return -1;
4021
Pekka Paalanen6e168112011-11-24 11:34:05 +02004022 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4023 shell, bind_screensaver) == NULL)
4024 return -1;
4025
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004026 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4027 shell, bind_input_panel) == NULL)
4028 return -1;
4029
Jonas Ådahle9d22502012-08-29 22:13:01 +02004030 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4031 shell, bind_workspace_manager) == NULL)
4032 return -1;
4033
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004034 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004035
4036 loop = wl_display_get_event_loop(ec->wl_display);
4037 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004038
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004039 wl_list_for_each(seat, &ec->seat_list, link)
4040 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004041
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004042 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004043
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004044 return 0;
4045}