blob: bbc3d05bf1a9bcdeb250f0a68bfc091cb5488a55 [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
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050026#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040027#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020028#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010040#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020041#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020042#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Juan Zhaoe10d2792012-04-25 19:09:52 +080047enum animation_type {
48 ANIMATION_NONE,
49
50 ANIMATION_ZOOM,
51 ANIMATION_FADE
52};
53
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020054enum fade_type {
55 FADE_IN,
56 FADE_OUT
57};
58
Jonas Ådahl04769742012-06-13 00:01:24 +020059struct focus_state {
60 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040061 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020062 struct weston_surface *keyboard_focus;
63 struct wl_list link;
64 struct wl_listener seat_destroy_listener;
65 struct wl_listener surface_destroy_listener;
66};
67
Jonas Ådahle3cddce2012-06-13 00:01:22 +020068struct workspace {
69 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020070
71 struct wl_list focus_list;
72 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020073};
74
Philipp Brüschweiler88013572012-08-06 13:44:42 +020075struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050076 struct wl_resource *resource;
77 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010078
79 struct desktop_shell *shell;
80
Philipp Brüschweiler88013572012-08-06 13:44:42 +020081 struct wl_list link;
82 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010083 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020084
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020085 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020086 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020087};
88
Tiago Vignattibe143262012-04-16 17:31:41 +030089struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040091
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020092 struct wl_listener idle_listener;
93 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040094 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020095 struct wl_listener show_input_panel_listener;
96 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020097 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020098
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050099 struct weston_layer fullscreen_layer;
100 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500101 struct weston_layer background_layer;
102 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200103 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500104
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400105 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300106 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400107
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200108 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200110 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200111 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200112
113 unsigned deathcount;
114 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200115 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200116
117 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200118 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200119 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200120
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200121 struct {
122 struct weston_surface *surface;
123 pixman_box32_t cursor_rectangle;
124 } text_input;
125
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400126 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500127 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128
Pekka Paalanen77346a62011-11-30 16:26:35 +0200129 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200130 struct wl_array array;
131 unsigned int current;
132 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200133
Jonas Ådahle9d22502012-08-29 22:13:01 +0200134 struct wl_list client_list;
135
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200136 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200137 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200138 int anim_dir;
139 uint32_t anim_timestamp;
140 double anim_current;
141 struct workspace *anim_from;
142 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200143 } workspaces;
144
145 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200146 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200147 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200148 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500149 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200150 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200151 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500152
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200153 struct {
154 struct wl_resource *binding;
155 struct wl_list surfaces;
156 } input_panel;
157
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200158 struct {
159 struct weston_surface *surface;
160 struct weston_surface_animation *animation;
161 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300162 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200163 } fade;
164
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300165 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800166 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400167};
168
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500169enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200170 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500171 SHELL_SURFACE_TOPLEVEL,
172 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800174 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300175 SHELL_SURFACE_POPUP,
176 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200177};
178
Scott Moreauff1db4a2012-04-17 19:06:18 -0600179struct ping_timer {
180 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600181 uint32_t serial;
182};
183
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500185 struct wl_resource *resource;
186 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200189 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400190 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300191 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200192
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400193 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400194 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500195 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800196 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800197 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600198 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100199
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500200 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } rotation;
204
205 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100206 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500207 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100208 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400209 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500210 } popup;
211
Alex Wu4539b082012-03-01 12:57:46 +0800212 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300213 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400214 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300215 } transient;
216
217 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800218 enum wl_shell_surface_fullscreen_method type;
219 struct weston_transform transform; /* matrix from x, y */
220 uint32_t framerate;
221 struct weston_surface *black_surface;
222 } fullscreen;
223
Scott Moreauff1db4a2012-04-17 19:06:18 -0600224 struct ping_timer *ping_timer;
225
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200226 struct weston_transform workspace_transform;
227
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500228 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100230 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400231
232 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200233};
234
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400236 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300237 struct shell_surface *shsurf;
238 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400239 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300240};
241
242struct weston_move_grab {
243 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100244 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245};
246
Pekka Paalanen460099f2012-01-20 16:48:25 +0200247struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300248 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500249 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200250 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200251 float x;
252 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200253 } center;
254};
255
Giulio Camuffo5085a752013-03-25 21:42:45 +0100256struct shell_seat {
257 struct weston_seat *seat;
258 struct wl_listener seat_destroy_listener;
259
260 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400261 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100262 struct wl_list surfaces_list;
263 struct wl_client *client;
264 int32_t initial_up;
265 } popup_grab;
266};
267
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400268static void
269activate(struct desktop_shell *shell, struct weston_surface *es,
270 struct weston_seat *seat);
271
272static struct workspace *
273get_current_workspace(struct desktop_shell *shell);
274
Alex Wubd3354b2012-04-17 17:20:49 +0800275static struct shell_surface *
276get_shell_surface(struct weston_surface *surface);
277
278static struct desktop_shell *
279shell_surface_get_shell(struct shell_surface *shsurf);
280
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500281static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400282surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500283
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300284static void
285shell_fade_startup(struct desktop_shell *shell);
286
Alex Wubd3354b2012-04-17 17:20:49 +0800287static bool
288shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
289{
290 struct desktop_shell *shell;
291 struct weston_surface *top_fs_es;
292
293 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100294
Alex Wubd3354b2012-04-17 17:20:49 +0800295 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
296 return false;
297
298 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100299 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800300 layer_link);
301 return (shsurf == get_shell_surface(top_fs_es));
302}
303
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500304static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400305destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306{
307 struct shell_grab *grab;
308
309 grab = container_of(listener, struct shell_grab,
310 shsurf_destroy_listener);
311
312 grab->shsurf = NULL;
313}
314
315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400337 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400338 if (shell->child.desktop_shell) {
339 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
340 cursor);
341 weston_pointer_set_focus(pointer, shell->grab_surface,
342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
344 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400350 if (grab->shsurf)
351 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300352
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400353 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300354}
355
356static void
Alex Wu4539b082012-03-01 12:57:46 +0800357center_on_output(struct weston_surface *surface,
358 struct weston_output *output);
359
Daniel Stone496ca172012-05-30 16:31:42 +0100360static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300361get_modifier(char *modifier)
362{
363 if (!modifier)
364 return MODIFIER_SUPER;
365
366 if (!strcmp("ctrl", modifier))
367 return MODIFIER_CTRL;
368 else if (!strcmp("alt", modifier))
369 return MODIFIER_ALT;
370 else if (!strcmp("super", modifier))
371 return MODIFIER_SUPER;
372 else
373 return MODIFIER_SUPER;
374}
375
Juan Zhaoe10d2792012-04-25 19:09:52 +0800376static enum animation_type
377get_animation_type(char *animation)
378{
379 if (!animation)
380 return ANIMATION_NONE;
381
382 if (!strcmp("zoom", animation))
383 return ANIMATION_ZOOM;
384 else if (!strcmp("fade", animation))
385 return ANIMATION_FADE;
386 else
387 return ANIMATION_NONE;
388}
389
Alex Wu4539b082012-03-01 12:57:46 +0800390static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400391shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400393 struct weston_config_section *section;
394 int duration;
395 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200396
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400397 section = weston_config_get_section(shell->compositor->config,
398 "screensaver", NULL, NULL);
399 weston_config_section_get_string(section,
400 "path", &shell->screensaver.path, NULL);
401 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200402 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400403
404 section = weston_config_get_section(shell->compositor->config,
405 "shell", NULL, NULL);
406 weston_config_section_get_string(section,
407 "binding-modifier", &s, "super");
408 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200409 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400410 weston_config_section_get_string(section, "animation", &s, "none");
411 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200412 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400413 weston_config_section_get_uint(section, "num-workspaces",
414 &shell->workspaces.num,
415 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200416}
417
418static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200419focus_state_destroy(struct focus_state *state)
420{
421 wl_list_remove(&state->seat_destroy_listener.link);
422 wl_list_remove(&state->surface_destroy_listener.link);
423 free(state);
424}
425
426static void
427focus_state_seat_destroy(struct wl_listener *listener, void *data)
428{
429 struct focus_state *state = container_of(listener,
430 struct focus_state,
431 seat_destroy_listener);
432
433 wl_list_remove(&state->link);
434 focus_state_destroy(state);
435}
436
437static void
438focus_state_surface_destroy(struct wl_listener *listener, void *data)
439{
440 struct focus_state *state = container_of(listener,
441 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400442 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400443 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300444 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400445 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200446
Pekka Paalanen01388e22013-04-25 13:57:44 +0300447 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
448
Kristian Høgsberge3778222012-07-31 17:29:30 -0400449 next = NULL;
450 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300451 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400452 continue;
453
454 next = surface;
455 break;
456 }
457
Pekka Paalanen01388e22013-04-25 13:57:44 +0300458 /* if the focus was a sub-surface, activate its main surface */
459 if (main_surface != state->keyboard_focus)
460 next = main_surface;
461
Kristian Høgsberge3778222012-07-31 17:29:30 -0400462 if (next) {
463 shell = state->seat->compositor->shell_interface.shell;
464 activate(shell, next, state->seat);
465 } else {
466 wl_list_remove(&state->link);
467 focus_state_destroy(state);
468 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200469}
470
471static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400472focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200473{
Jonas Ådahl04769742012-06-13 00:01:24 +0200474 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200475
476 state = malloc(sizeof *state);
477 if (state == NULL)
478 return NULL;
479
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400480 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200481 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400482 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200483
484 state->seat_destroy_listener.notify = focus_state_seat_destroy;
485 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400486 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200487 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400488 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200489
490 return state;
491}
492
Jonas Ådahl8538b222012-08-29 22:13:03 +0200493static struct focus_state *
494ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
495{
496 struct workspace *ws = get_current_workspace(shell);
497 struct focus_state *state;
498
499 wl_list_for_each(state, &ws->focus_list, link)
500 if (state->seat == seat)
501 break;
502
503 if (&state->link == &ws->focus_list)
504 state = focus_state_create(seat, ws);
505
506 return state;
507}
508
Jonas Ådahl04769742012-06-13 00:01:24 +0200509static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400510restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200511{
512 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400513 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200514
515 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400516 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200517
Kristian Høgsberge3148752013-05-06 23:19:49 -0400518 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200519 }
520}
521
522static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200523replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
524 struct weston_seat *seat)
525{
526 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400527 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200528
529 wl_list_for_each(state, &ws->focus_list, link) {
530 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400531 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500532 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200533 return;
534 }
535 }
536}
537
538static void
539drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
540 struct weston_surface *surface)
541{
542 struct focus_state *state;
543
544 wl_list_for_each(state, &ws->focus_list, link)
545 if (state->keyboard_focus == surface)
546 state->keyboard_focus = NULL;
547}
548
549static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550workspace_destroy(struct workspace *ws)
551{
Jonas Ådahl04769742012-06-13 00:01:24 +0200552 struct focus_state *state, *next;
553
554 wl_list_for_each_safe(state, next, &ws->focus_list, link)
555 focus_state_destroy(state);
556
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200557 free(ws);
558}
559
Jonas Ådahl04769742012-06-13 00:01:24 +0200560static void
561seat_destroyed(struct wl_listener *listener, void *data)
562{
563 struct weston_seat *seat = data;
564 struct focus_state *state, *next;
565 struct workspace *ws = container_of(listener,
566 struct workspace,
567 seat_destroyed_listener);
568
569 wl_list_for_each_safe(state, next, &ws->focus_list, link)
570 if (state->seat == seat)
571 wl_list_remove(&state->link);
572}
573
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200574static struct workspace *
575workspace_create(void)
576{
577 struct workspace *ws = malloc(sizeof *ws);
578 if (ws == NULL)
579 return NULL;
580
581 weston_layer_init(&ws->layer, NULL);
582
Jonas Ådahl04769742012-06-13 00:01:24 +0200583 wl_list_init(&ws->focus_list);
584 wl_list_init(&ws->seat_destroyed_listener.link);
585 ws->seat_destroyed_listener.notify = seat_destroyed;
586
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200587 return ws;
588}
589
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200590static int
591workspace_is_empty(struct workspace *ws)
592{
593 return wl_list_empty(&ws->layer.surface_list);
594}
595
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200596static struct workspace *
597get_workspace(struct desktop_shell *shell, unsigned int index)
598{
599 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200600 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200601 pws += index;
602 return *pws;
603}
604
605static struct workspace *
606get_current_workspace(struct desktop_shell *shell)
607{
608 return get_workspace(shell, shell->workspaces.current);
609}
610
611static void
612activate_workspace(struct desktop_shell *shell, unsigned int index)
613{
614 struct workspace *ws;
615
616 ws = get_workspace(shell, index);
617 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
618
619 shell->workspaces.current = index;
620}
621
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200622static unsigned int
623get_output_height(struct weston_output *output)
624{
625 return abs(output->region.extents.y1 - output->region.extents.y2);
626}
627
628static void
629surface_translate(struct weston_surface *surface, double d)
630{
631 struct shell_surface *shsurf = get_shell_surface(surface);
632 struct weston_transform *transform;
633
634 transform = &shsurf->workspace_transform;
635 if (wl_list_empty(&transform->link))
636 wl_list_insert(surface->geometry.transformation_list.prev,
637 &shsurf->workspace_transform.link);
638
639 weston_matrix_init(&shsurf->workspace_transform.matrix);
640 weston_matrix_translate(&shsurf->workspace_transform.matrix,
641 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200642 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200643}
644
645static void
646workspace_translate_out(struct workspace *ws, double fraction)
647{
648 struct weston_surface *surface;
649 unsigned int height;
650 double d;
651
652 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
653 height = get_output_height(surface->output);
654 d = height * fraction;
655
656 surface_translate(surface, d);
657 }
658}
659
660static void
661workspace_translate_in(struct workspace *ws, double fraction)
662{
663 struct weston_surface *surface;
664 unsigned int height;
665 double d;
666
667 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
668 height = get_output_height(surface->output);
669
670 if (fraction > 0)
671 d = -(height - height * fraction);
672 else
673 d = height + height * fraction;
674
675 surface_translate(surface, d);
676 }
677}
678
679static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200680broadcast_current_workspace_state(struct desktop_shell *shell)
681{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500682 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200683
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500684 for (link = shell->workspaces.client_list.next;
685 link != &shell->workspaces.client_list;
686 link = link->next) {
687 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200688 shell->workspaces.current,
689 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500690 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200691}
692
693static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200694reverse_workspace_change_animation(struct desktop_shell *shell,
695 unsigned int index,
696 struct workspace *from,
697 struct workspace *to)
698{
699 shell->workspaces.current = index;
700
701 shell->workspaces.anim_to = to;
702 shell->workspaces.anim_from = from;
703 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
704 shell->workspaces.anim_timestamp = 0;
705
Scott Moreau4272e632012-08-13 09:58:41 -0600706 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200707}
708
709static void
710workspace_deactivate_transforms(struct workspace *ws)
711{
712 struct weston_surface *surface;
713 struct shell_surface *shsurf;
714
715 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
716 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200717 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
718 wl_list_remove(&shsurf->workspace_transform.link);
719 wl_list_init(&shsurf->workspace_transform.link);
720 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200721 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200722 }
723}
724
725static void
726finish_workspace_change_animation(struct desktop_shell *shell,
727 struct workspace *from,
728 struct workspace *to)
729{
Scott Moreau4272e632012-08-13 09:58:41 -0600730 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200731
732 wl_list_remove(&shell->workspaces.animation.link);
733 workspace_deactivate_transforms(from);
734 workspace_deactivate_transforms(to);
735 shell->workspaces.anim_to = NULL;
736
737 wl_list_remove(&shell->workspaces.anim_from->layer.link);
738}
739
740static void
741animate_workspace_change_frame(struct weston_animation *animation,
742 struct weston_output *output, uint32_t msecs)
743{
744 struct desktop_shell *shell =
745 container_of(animation, struct desktop_shell,
746 workspaces.animation);
747 struct workspace *from = shell->workspaces.anim_from;
748 struct workspace *to = shell->workspaces.anim_to;
749 uint32_t t;
750 double x, y;
751
752 if (workspace_is_empty(from) && workspace_is_empty(to)) {
753 finish_workspace_change_animation(shell, from, to);
754 return;
755 }
756
757 if (shell->workspaces.anim_timestamp == 0) {
758 if (shell->workspaces.anim_current == 0.0)
759 shell->workspaces.anim_timestamp = msecs;
760 else
761 shell->workspaces.anim_timestamp =
762 msecs -
763 /* Invers of movement function 'y' below. */
764 (asin(1.0 - shell->workspaces.anim_current) *
765 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
766 M_2_PI);
767 }
768
769 t = msecs - shell->workspaces.anim_timestamp;
770
771 /*
772 * x = [0, π/2]
773 * y(x) = sin(x)
774 */
775 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
776 y = sin(x);
777
778 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600779 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200780
781 workspace_translate_out(from, shell->workspaces.anim_dir * y);
782 workspace_translate_in(to, shell->workspaces.anim_dir * y);
783 shell->workspaces.anim_current = y;
784
Scott Moreau4272e632012-08-13 09:58:41 -0600785 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200786 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200787 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200788 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200789}
790
791static void
792animate_workspace_change(struct desktop_shell *shell,
793 unsigned int index,
794 struct workspace *from,
795 struct workspace *to)
796{
797 struct weston_output *output;
798
799 int dir;
800
801 if (index > shell->workspaces.current)
802 dir = -1;
803 else
804 dir = 1;
805
806 shell->workspaces.current = index;
807
808 shell->workspaces.anim_dir = dir;
809 shell->workspaces.anim_from = from;
810 shell->workspaces.anim_to = to;
811 shell->workspaces.anim_current = 0.0;
812 shell->workspaces.anim_timestamp = 0;
813
814 output = container_of(shell->compositor->output_list.next,
815 struct weston_output, link);
816 wl_list_insert(&output->animation_list,
817 &shell->workspaces.animation.link);
818
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200819 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200820
821 workspace_translate_in(to, 0);
822
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400823 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200824
Scott Moreau4272e632012-08-13 09:58:41 -0600825 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200826}
827
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200829update_workspace(struct desktop_shell *shell, unsigned int index,
830 struct workspace *from, struct workspace *to)
831{
832 shell->workspaces.current = index;
833 wl_list_insert(&from->layer.link, &to->layer.link);
834 wl_list_remove(&from->layer.link);
835}
836
837static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838change_workspace(struct desktop_shell *shell, unsigned int index)
839{
840 struct workspace *from;
841 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200842
843 if (index == shell->workspaces.current)
844 return;
845
846 /* Don't change workspace when there is any fullscreen surfaces. */
847 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
848 return;
849
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200850 from = get_current_workspace(shell);
851 to = get_workspace(shell, index);
852
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200853 if (shell->workspaces.anim_from == to &&
854 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200855 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200856 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200857 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200858 return;
859 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200860
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200861 if (shell->workspaces.anim_to != NULL)
862 finish_workspace_change_animation(shell,
863 shell->workspaces.anim_from,
864 shell->workspaces.anim_to);
865
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200866 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200867
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200868 if (workspace_is_empty(to) && workspace_is_empty(from))
869 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200870 else
871 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200872
873 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200874}
875
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200876static bool
877workspace_has_only(struct workspace *ws, struct weston_surface *surface)
878{
879 struct wl_list *list = &ws->layer.surface_list;
880 struct wl_list *e;
881
882 if (wl_list_empty(list))
883 return false;
884
885 e = list->next;
886
887 if (e->next != list)
888 return false;
889
890 return container_of(e, struct weston_surface, layer_link) == surface;
891}
892
893static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894move_surface_to_workspace(struct desktop_shell *shell,
895 struct weston_surface *surface,
896 uint32_t workspace)
897{
898 struct workspace *from;
899 struct workspace *to;
900 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300901 struct weston_surface *focus;
902
903 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200904
905 if (workspace == shell->workspaces.current)
906 return;
907
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200908 if (workspace >= shell->workspaces.num)
909 workspace = shell->workspaces.num - 1;
910
Jonas Ådahle9d22502012-08-29 22:13:01 +0200911 from = get_current_workspace(shell);
912 to = get_workspace(shell, workspace);
913
914 wl_list_remove(&surface->layer_link);
915 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
916
917 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300918 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
919 if (!seat->keyboard)
920 continue;
921
922 focus = weston_surface_get_main_surface(seat->keyboard->focus);
923 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400924 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300925 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200926
927 weston_surface_damage_below(surface);
928}
929
930static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200931take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400932 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200933 unsigned int index)
934{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300935 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936 struct shell_surface *shsurf;
937 struct workspace *from;
938 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200939 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200940
Pekka Paalanen01388e22013-04-25 13:57:44 +0300941 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200942 if (surface == NULL ||
943 index == shell->workspaces.current)
944 return;
945
946 from = get_current_workspace(shell);
947 to = get_workspace(shell, index);
948
949 wl_list_remove(&surface->layer_link);
950 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
951
Jonas Ådahle9d22502012-08-29 22:13:01 +0200952 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200953 drop_focus_state(shell, from, surface);
954
955 if (shell->workspaces.anim_from == to &&
956 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200957 wl_list_remove(&to->layer.link);
958 wl_list_insert(from->layer.link.prev, &to->layer.link);
959
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200960 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200961 broadcast_current_workspace_state(shell);
962
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200963 return;
964 }
965
966 if (shell->workspaces.anim_to != NULL)
967 finish_workspace_change_animation(shell,
968 shell->workspaces.anim_from,
969 shell->workspaces.anim_to);
970
971 if (workspace_is_empty(from) &&
972 workspace_has_only(to, surface))
973 update_workspace(shell, index, from, to);
974 else {
975 shsurf = get_shell_surface(surface);
976 if (wl_list_empty(&shsurf->workspace_transform.link))
977 wl_list_insert(&shell->workspaces.anim_sticky_list,
978 &shsurf->workspace_transform.link);
979
980 animate_workspace_change(shell, index, from, to);
981 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200982
983 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200984
985 state = ensure_focus_state(shell, seat);
986 if (state != NULL)
987 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200988}
989
990static void
991workspace_manager_move_surface(struct wl_client *client,
992 struct wl_resource *resource,
993 struct wl_resource *surface_resource,
994 uint32_t workspace)
995{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500996 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200997 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500998 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300999 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001000
Pekka Paalanen01388e22013-04-25 13:57:44 +03001001 main_surface = weston_surface_get_main_surface(surface);
1002 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001003}
1004
1005static const struct workspace_manager_interface workspace_manager_implementation = {
1006 workspace_manager_move_surface,
1007};
1008
1009static void
1010unbind_resource(struct wl_resource *resource)
1011{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001012 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001013}
1014
1015static void
1016bind_workspace_manager(struct wl_client *client,
1017 void *data, uint32_t version, uint32_t id)
1018{
1019 struct desktop_shell *shell = data;
1020 struct wl_resource *resource;
1021
Jason Ekstranda85118c2013-06-27 20:17:02 -05001022 resource = wl_resource_create(client,
1023 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001024
1025 if (resource == NULL) {
1026 weston_log("couldn't add workspace manager object");
1027 return;
1028 }
1029
Jason Ekstranda85118c2013-06-27 20:17:02 -05001030 wl_resource_set_implementation(resource,
1031 &workspace_manager_implementation,
1032 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001033 wl_list_insert(&shell->workspaces.client_list,
1034 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001035
1036 workspace_manager_send_state(resource,
1037 shell->workspaces.current,
1038 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001039}
1040
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001041static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001042noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001043{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001044}
1045
1046static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001047move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001048{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001049 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001050 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001051 struct shell_surface *shsurf = move->base.shsurf;
1052 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001053 int dx = wl_fixed_to_int(pointer->x + move->dx);
1054 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001055
1056 if (!shsurf)
1057 return;
1058
1059 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060
Daniel Stone103db7f2012-05-08 17:17:55 +01001061 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001062 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001063
1064 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001065}
1066
1067static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001068move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001069 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001070{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001071 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1072 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001073 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001074 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001075
Daniel Stone4dbadb12012-05-30 16:31:51 +01001076 if (pointer->button_count == 0 &&
1077 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001078 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001079 free(grab);
1080 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001081}
1082
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001083static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001084 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001085 move_grab_motion,
1086 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001087};
1088
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001089static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001090surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001091{
1092 struct weston_move_grab *move;
1093
1094 if (!shsurf)
1095 return -1;
1096
1097 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1098 return 0;
1099
1100 move = malloc(sizeof *move);
1101 if (!move)
1102 return -1;
1103
1104 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001105 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001106 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001107 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001108
1109 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001110 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001111
1112 return 0;
1113}
1114
1115static void
1116shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1117 struct wl_resource *seat_resource, uint32_t serial)
1118{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001119 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001120 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001121 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001122
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001123 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001124 if (seat->pointer->button_count == 0 ||
1125 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001126 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001127 return;
1128
Kristian Høgsberge3148752013-05-06 23:19:49 -04001129 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001130 wl_resource_post_no_memory(resource);
1131}
1132
1133struct weston_resize_grab {
1134 struct shell_grab base;
1135 uint32_t edges;
1136 int32_t width, height;
1137};
1138
1139static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001140resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001141{
1142 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001143 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001144 struct shell_surface *shsurf = resize->base.shsurf;
1145 int32_t width, height;
1146 wl_fixed_t from_x, from_y;
1147 wl_fixed_t to_x, to_y;
1148
1149 if (!shsurf)
1150 return;
1151
1152 weston_surface_from_global_fixed(shsurf->surface,
1153 pointer->grab_x, pointer->grab_y,
1154 &from_x, &from_y);
1155 weston_surface_from_global_fixed(shsurf->surface,
1156 pointer->x, pointer->y, &to_x, &to_y);
1157
1158 width = resize->width;
1159 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1160 width += wl_fixed_to_int(from_x - to_x);
1161 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1162 width += wl_fixed_to_int(to_x - from_x);
1163 }
1164
1165 height = resize->height;
1166 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1167 height += wl_fixed_to_int(from_y - to_y);
1168 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1169 height += wl_fixed_to_int(to_y - from_y);
1170 }
1171
1172 shsurf->client->send_configure(shsurf->surface,
1173 resize->edges, width, height);
1174}
1175
1176static void
1177send_configure(struct weston_surface *surface,
1178 uint32_t edges, int32_t width, int32_t height)
1179{
1180 struct shell_surface *shsurf = get_shell_surface(surface);
1181
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001182 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001183 edges, width, height);
1184}
1185
1186static const struct weston_shell_client shell_client = {
1187 send_configure
1188};
1189
1190static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001191resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001192 uint32_t time, uint32_t button, uint32_t state_w)
1193{
1194 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001195 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001196 enum wl_pointer_button_state state = state_w;
1197
1198 if (pointer->button_count == 0 &&
1199 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1200 shell_grab_end(&resize->base);
1201 free(grab);
1202 }
1203}
1204
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001205static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001206 noop_grab_focus,
1207 resize_grab_motion,
1208 resize_grab_button,
1209};
1210
Giulio Camuffob8366642013-04-25 13:57:46 +03001211/*
1212 * Returns the bounding box of a surface and all its sub-surfaces,
1213 * in the surface coordinates system. */
1214static void
1215surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1216 int32_t *y, int32_t *w, int32_t *h) {
1217 pixman_region32_t region;
1218 pixman_box32_t *box;
1219 struct weston_subsurface *subsurface;
1220
1221 pixman_region32_init_rect(&region, 0, 0,
1222 surface->geometry.width,
1223 surface->geometry.height);
1224
1225 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1226 pixman_region32_union_rect(&region, &region,
1227 subsurface->position.x,
1228 subsurface->position.y,
1229 subsurface->surface->geometry.width,
1230 subsurface->surface->geometry.height);
1231 }
1232
1233 box = pixman_region32_extents(&region);
1234 if (x)
1235 *x = box->x1;
1236 if (y)
1237 *y = box->y1;
1238 if (w)
1239 *w = box->x2 - box->x1;
1240 if (h)
1241 *h = box->y2 - box->y1;
1242
1243 pixman_region32_fini(&region);
1244}
1245
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001246static int
1247surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001248 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001249{
1250 struct weston_resize_grab *resize;
1251
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001252 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1253 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001254 return 0;
1255
1256 if (edges == 0 || edges > 15 ||
1257 (edges & 3) == 3 || (edges & 12) == 12)
1258 return 0;
1259
1260 resize = malloc(sizeof *resize);
1261 if (!resize)
1262 return -1;
1263
1264 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001265 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1266 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001267
1268 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001269 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001270
1271 return 0;
1272}
1273
1274static void
1275shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1276 struct wl_resource *seat_resource, uint32_t serial,
1277 uint32_t edges)
1278{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001279 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001280 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001281 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001282
1283 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1284 return;
1285
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001286 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001287 if (seat->pointer->button_count == 0 ||
1288 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001289 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001290 return;
1291
Kristian Høgsberge3148752013-05-06 23:19:49 -04001292 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001293 wl_resource_post_no_memory(resource);
1294}
1295
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001296static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001297busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001298{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001299 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001300 struct weston_pointer *pointer = base->pointer;
1301 struct weston_surface *surface;
1302 wl_fixed_t sx, sy;
1303
1304 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1305 pointer->x, pointer->y,
1306 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001307
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001308 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001309 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001310 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001311 }
1312}
1313
1314static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001315busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001316{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001317}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001318
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001319static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001320busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001321 uint32_t time, uint32_t button, uint32_t state)
1322{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001323 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001324 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001325 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001326
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001327 if (shsurf && button == BTN_LEFT && state) {
1328 activate(shsurf->shell, shsurf->surface, seat);
1329 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001330 } else if (shsurf && button == BTN_RIGHT && state) {
1331 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001332 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001333 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001334}
1335
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001336static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001337 busy_cursor_grab_focus,
1338 busy_cursor_grab_motion,
1339 busy_cursor_grab_button,
1340};
1341
1342static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001343set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001344{
1345 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001346
1347 grab = malloc(sizeof *grab);
1348 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001349 return;
1350
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001351 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1352 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001353}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001354
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001355static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001356end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001357{
1358 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1359
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001360 if (grab->grab.interface == &busy_cursor_grab_interface &&
1361 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001362 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001363 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001364 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001365}
1366
Scott Moreau9521d5e2012-04-19 13:06:17 -06001367static void
1368ping_timer_destroy(struct shell_surface *shsurf)
1369{
1370 if (!shsurf || !shsurf->ping_timer)
1371 return;
1372
1373 if (shsurf->ping_timer->source)
1374 wl_event_source_remove(shsurf->ping_timer->source);
1375
1376 free(shsurf->ping_timer);
1377 shsurf->ping_timer = NULL;
1378}
1379
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001380static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001381ping_timeout_handler(void *data)
1382{
1383 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001384 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001385
Scott Moreau9521d5e2012-04-19 13:06:17 -06001386 /* Client is not responding */
1387 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001388
1389 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001390 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001391 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001392
1393 return 1;
1394}
1395
1396static void
1397ping_handler(struct weston_surface *surface, uint32_t serial)
1398{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001399 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001401 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001402
1403 if (!shsurf)
1404 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001405 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001406 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001407
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001408 if (shsurf->surface == shsurf->shell->grab_surface)
1409 return;
1410
Scott Moreauff1db4a2012-04-17 19:06:18 -06001411 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001412 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001413 if (!shsurf->ping_timer)
1414 return;
1415
1416 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001417 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1418 shsurf->ping_timer->source =
1419 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1420 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1421
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001422 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001423 }
1424}
1425
1426static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001427handle_pointer_focus(struct wl_listener *listener, void *data)
1428{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001429 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001430 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001431 struct weston_compositor *compositor;
1432 struct shell_surface *shsurf;
1433 uint32_t serial;
1434
1435 if (!surface)
1436 return;
1437
1438 compositor = surface->compositor;
1439 shsurf = get_shell_surface(surface);
1440
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001441 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001442 set_busy_cursor(shsurf, pointer);
1443 } else {
1444 serial = wl_display_next_serial(compositor->wl_display);
1445 ping_handler(surface, serial);
1446 }
1447}
1448
1449static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001450create_pointer_focus_listener(struct weston_seat *seat)
1451{
1452 struct wl_listener *listener;
1453
Kristian Høgsberge3148752013-05-06 23:19:49 -04001454 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001455 return;
1456
1457 listener = malloc(sizeof *listener);
1458 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001459 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001460}
1461
1462static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001463shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1464 uint32_t serial)
1465{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001466 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001467 struct weston_seat *seat;
1468 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001469
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001470 if (shsurf->ping_timer == NULL)
1471 /* Just ignore unsolicited pong. */
1472 return;
1473
Scott Moreauff1db4a2012-04-17 19:06:18 -06001474 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001475 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001476 wl_list_for_each(seat, &ec->seat_list, link) {
1477 if(seat->pointer)
1478 end_busy_cursor(shsurf, seat->pointer);
1479 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001480 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001481 }
1482}
1483
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001484static void
1485shell_surface_set_title(struct wl_client *client,
1486 struct wl_resource *resource, const char *title)
1487{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001488 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001489
1490 free(shsurf->title);
1491 shsurf->title = strdup(title);
1492}
1493
1494static void
1495shell_surface_set_class(struct wl_client *client,
1496 struct wl_resource *resource, const char *class)
1497{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001498 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001499
1500 free(shsurf->class);
1501 shsurf->class = strdup(class);
1502}
1503
Juan Zhao96879df2012-02-07 08:45:41 +08001504static struct weston_output *
1505get_default_output(struct weston_compositor *compositor)
1506{
1507 return container_of(compositor->output_list.next,
1508 struct weston_output, link);
1509}
1510
Alex Wu4539b082012-03-01 12:57:46 +08001511static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001512restore_output_mode(struct weston_output *output)
1513{
1514 if (output->current != output->origin ||
1515 (int32_t)output->scale != output->origin_scale)
1516 weston_output_switch_mode(output,
1517 output->origin,
1518 output->origin_scale);
1519}
1520
1521static void
1522restore_all_output_modes(struct weston_compositor *compositor)
1523{
1524 struct weston_output *output;
1525
1526 wl_list_for_each(output, &compositor->output_list, link)
1527 restore_output_mode(output);
1528}
1529
1530static void
Alex Wu4539b082012-03-01 12:57:46 +08001531shell_unset_fullscreen(struct shell_surface *shsurf)
1532{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001533 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001534 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001535 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1536 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001537 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001538 }
Alex Wu4539b082012-03-01 12:57:46 +08001539 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1540 shsurf->fullscreen.framerate = 0;
1541 wl_list_remove(&shsurf->fullscreen.transform.link);
1542 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001543 if (shsurf->fullscreen.black_surface)
1544 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001545 shsurf->fullscreen.black_surface = NULL;
1546 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001547 weston_surface_set_position(shsurf->surface,
1548 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001549 if (shsurf->saved_rotation_valid) {
1550 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1551 &shsurf->rotation.transform.link);
1552 shsurf->saved_rotation_valid = false;
1553 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001554
1555 ws = get_current_workspace(shsurf->shell);
1556 wl_list_remove(&shsurf->surface->layer_link);
1557 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001558}
1559
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001560static void
1561shell_unset_maximized(struct shell_surface *shsurf)
1562{
1563 struct workspace *ws;
1564 /* undo all maximized things here */
1565 shsurf->output = get_default_output(shsurf->surface->compositor);
1566 weston_surface_set_position(shsurf->surface,
1567 shsurf->saved_x,
1568 shsurf->saved_y);
1569
1570 if (shsurf->saved_rotation_valid) {
1571 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1572 &shsurf->rotation.transform.link);
1573 shsurf->saved_rotation_valid = false;
1574 }
1575
1576 ws = get_current_workspace(shsurf->shell);
1577 wl_list_remove(&shsurf->surface->layer_link);
1578 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1579}
1580
Pekka Paalanen98262232011-12-01 10:42:22 +02001581static int
1582reset_shell_surface_type(struct shell_surface *surface)
1583{
1584 switch (surface->type) {
1585 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001586 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001587 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001588 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001589 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001590 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001591 case SHELL_SURFACE_NONE:
1592 case SHELL_SURFACE_TOPLEVEL:
1593 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001594 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001595 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001596 break;
1597 }
1598
1599 surface->type = SHELL_SURFACE_NONE;
1600 return 0;
1601}
1602
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001603static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001604set_surface_type(struct shell_surface *shsurf)
1605{
1606 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001607 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001608
1609 reset_shell_surface_type(shsurf);
1610
1611 shsurf->type = shsurf->next_type;
1612 shsurf->next_type = SHELL_SURFACE_NONE;
1613
1614 switch (shsurf->type) {
1615 case SHELL_SURFACE_TOPLEVEL:
1616 break;
1617 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001618 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001619 pes->geometry.x + shsurf->transient.x,
1620 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001621 break;
1622
1623 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001624 case SHELL_SURFACE_FULLSCREEN:
1625 shsurf->saved_x = surface->geometry.x;
1626 shsurf->saved_y = surface->geometry.y;
1627 shsurf->saved_position_valid = true;
1628
1629 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1630 wl_list_remove(&shsurf->rotation.transform.link);
1631 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001632 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001633 shsurf->saved_rotation_valid = true;
1634 }
1635 break;
1636
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001637 case SHELL_SURFACE_XWAYLAND:
1638 weston_surface_set_position(surface, shsurf->transient.x,
1639 shsurf->transient.y);
1640 break;
1641
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001642 default:
1643 break;
1644 }
1645}
1646
1647static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001648set_toplevel(struct shell_surface *shsurf)
1649{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001650 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001651}
1652
1653static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001654shell_surface_set_toplevel(struct wl_client *client,
1655 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001656{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001657 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001658
Tiago Vignattibc052c92012-04-19 16:18:18 +03001659 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001660}
1661
1662static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001663set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001664 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001665{
1666 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001667 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001668 shsurf->transient.x = x;
1669 shsurf->transient.y = y;
1670 shsurf->transient.flags = flags;
1671 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1672}
1673
1674static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001675shell_surface_set_transient(struct wl_client *client,
1676 struct wl_resource *resource,
1677 struct wl_resource *parent_resource,
1678 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001679{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001680 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001681 struct weston_surface *parent =
1682 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001683
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001684 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001685}
1686
Tiago Vignattibe143262012-04-16 17:31:41 +03001687static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001688shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001689{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001690 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001691}
1692
1693static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001694get_output_panel_height(struct desktop_shell *shell,
1695 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001696{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001697 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001698 int panel_height = 0;
1699
1700 if (!output)
1701 return 0;
1702
Juan Zhao4ab94682012-07-09 22:24:09 -07001703 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001704 if (surface->output == output) {
1705 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001706 break;
1707 }
1708 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001709
Juan Zhao96879df2012-02-07 08:45:41 +08001710 return panel_height;
1711}
1712
1713static void
1714shell_surface_set_maximized(struct wl_client *client,
1715 struct wl_resource *resource,
1716 struct wl_resource *output_resource )
1717{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001718 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001719 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001720 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001721 uint32_t edges = 0, panel_height = 0;
1722
1723 /* get the default output, if the client set it as NULL
1724 check whether the ouput is available */
1725 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001726 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001727 else if (es->output)
1728 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001729 else
1730 shsurf->output = get_default_output(es->compositor);
1731
Tiago Vignattibe143262012-04-16 17:31:41 +03001732 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001733 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001734 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001735
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001736 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001737 shsurf->output->width,
1738 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001739
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001740 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001741}
1742
Alex Wu21858432012-04-01 20:13:08 +08001743static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001744black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
Alex Wu21858432012-04-01 20:13:08 +08001745
Alex Wu4539b082012-03-01 12:57:46 +08001746static struct weston_surface *
1747create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001748 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001749 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001750{
1751 struct weston_surface *surface = NULL;
1752
1753 surface = weston_surface_create(ec);
1754 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001755 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001756 return NULL;
1757 }
1758
Alex Wu21858432012-04-01 20:13:08 +08001759 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001760 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001761 weston_surface_configure(surface, x, y, w, h);
1762 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001763 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001764 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001765 pixman_region32_fini(&surface->input);
1766 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001767
Alex Wu4539b082012-03-01 12:57:46 +08001768 return surface;
1769}
1770
1771/* Create black surface and append it to the associated fullscreen surface.
1772 * Handle size dismatch and positioning according to the method. */
1773static void
1774shell_configure_fullscreen(struct shell_surface *shsurf)
1775{
1776 struct weston_output *output = shsurf->fullscreen_output;
1777 struct weston_surface *surface = shsurf->surface;
1778 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001779 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001780 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001781
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001782 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1783 restore_output_mode(output);
1784
Alex Wu4539b082012-03-01 12:57:46 +08001785 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001786 shsurf->fullscreen.black_surface =
1787 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001788 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001789 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001790 output->width,
1791 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001792
1793 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1794 wl_list_insert(&surface->layer_link,
1795 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001796 shsurf->fullscreen.black_surface->output = output;
1797
Giulio Camuffob8366642013-04-25 13:57:46 +03001798 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1799 &surf_width, &surf_height);
1800
Alex Wu4539b082012-03-01 12:57:46 +08001801 switch (shsurf->fullscreen.type) {
1802 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001803 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001804 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001805 break;
1806 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001807 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001808 if (output->width == surf_width &&
1809 output->height == surf_height) {
1810 weston_surface_set_position(surface, output->x - surf_x,
1811 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001812 break;
1813 }
1814
Alex Wu4539b082012-03-01 12:57:46 +08001815 matrix = &shsurf->fullscreen.transform.matrix;
1816 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001817
Scott Moreau1bad5db2012-08-18 01:04:05 -06001818 output_aspect = (float) output->width /
1819 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001820 surface_aspect = (float) surface->geometry.width /
1821 (float) surface->geometry.height;
1822 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001823 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001824 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001825 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001826 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001827 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001828
Alex Wu4539b082012-03-01 12:57:46 +08001829 weston_matrix_scale(matrix, scale, scale, 1);
1830 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001831 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001832 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001833 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1834 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001835 weston_surface_set_position(surface, x, y);
1836
Alex Wu4539b082012-03-01 12:57:46 +08001837 break;
1838 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001839 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001840 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001841 surf_width * surface->buffer_scale,
1842 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001843 shsurf->fullscreen.framerate};
1844
Alexander Larsson355748e2013-05-28 16:23:38 +02001845 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001846 weston_surface_set_position(surface,
1847 output->x - surf_x,
1848 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001849 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001850 output->x - surf_x,
1851 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001852 output->width,
1853 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001854 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001855 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001856 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001857 center_on_output(surface, output);
1858 }
Alex Wubd3354b2012-04-17 17:20:49 +08001859 }
Alex Wu4539b082012-03-01 12:57:46 +08001860 break;
1861 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001862 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001863 break;
1864 default:
1865 break;
1866 }
1867}
1868
1869/* make the fullscreen and black surface at the top */
1870static void
1871shell_stack_fullscreen(struct shell_surface *shsurf)
1872{
Alex Wubd3354b2012-04-17 17:20:49 +08001873 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001874 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001875 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001876
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001877 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001878 wl_list_insert(&shell->fullscreen_layer.surface_list,
1879 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001880 weston_surface_damage(surface);
1881
1882 if (!shsurf->fullscreen.black_surface)
1883 shsurf->fullscreen.black_surface =
1884 create_black_surface(surface->compositor,
1885 surface,
1886 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001887 output->width,
1888 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001889
1890 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001891 wl_list_insert(&surface->layer_link,
1892 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001893 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001894}
1895
1896static void
1897shell_map_fullscreen(struct shell_surface *shsurf)
1898{
Alex Wu4539b082012-03-01 12:57:46 +08001899 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001900 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001901}
1902
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001903static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001904set_fullscreen(struct shell_surface *shsurf,
1905 uint32_t method,
1906 uint32_t framerate,
1907 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001908{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001909 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001910
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001911 if (output)
1912 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001913 else if (es->output)
1914 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001915 else
1916 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001917
Alex Wu4539b082012-03-01 12:57:46 +08001918 shsurf->fullscreen_output = shsurf->output;
1919 shsurf->fullscreen.type = method;
1920 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001921 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001922
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001923 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001924 shsurf->output->width,
1925 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001926}
1927
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001928static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001929shell_surface_set_fullscreen(struct wl_client *client,
1930 struct wl_resource *resource,
1931 uint32_t method,
1932 uint32_t framerate,
1933 struct wl_resource *output_resource)
1934{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001935 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001936 struct weston_output *output;
1937
1938 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001939 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001940 else
1941 output = NULL;
1942
1943 set_fullscreen(shsurf, method, framerate, output);
1944}
1945
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001946static void
1947set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1948{
1949 /* XXX: using the same fields for transient type */
1950 shsurf->transient.x = x;
1951 shsurf->transient.y = y;
1952 shsurf->transient.flags = flags;
1953 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1954}
1955
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001956static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001957
1958static void
1959destroy_shell_seat(struct wl_listener *listener, void *data)
1960{
1961 struct shell_seat *shseat =
1962 container_of(listener,
1963 struct shell_seat, seat_destroy_listener);
1964 struct shell_surface *shsurf, *prev = NULL;
1965
1966 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001967 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001968 shseat->popup_grab.client = NULL;
1969
1970 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1971 shsurf->popup.shseat = NULL;
1972 if (prev) {
1973 wl_list_init(&prev->popup.grab_link);
1974 }
1975 prev = shsurf;
1976 }
1977 wl_list_init(&prev->popup.grab_link);
1978 }
1979
1980 wl_list_remove(&shseat->seat_destroy_listener.link);
1981 free(shseat);
1982}
1983
1984static struct shell_seat *
1985create_shell_seat(struct weston_seat *seat)
1986{
1987 struct shell_seat *shseat;
1988
1989 shseat = calloc(1, sizeof *shseat);
1990 if (!shseat) {
1991 weston_log("no memory to allocate shell seat\n");
1992 return NULL;
1993 }
1994
1995 shseat->seat = seat;
1996 wl_list_init(&shseat->popup_grab.surfaces_list);
1997
1998 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1999 wl_signal_add(&seat->destroy_signal,
2000 &shseat->seat_destroy_listener);
2001
2002 return shseat;
2003}
2004
2005static struct shell_seat *
2006get_shell_seat(struct weston_seat *seat)
2007{
2008 struct wl_listener *listener;
2009
2010 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2011 if (listener == NULL)
2012 return create_shell_seat(seat);
2013
2014 return container_of(listener,
2015 struct shell_seat, seat_destroy_listener);
2016}
2017
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002018static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002019popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002020{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002021 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002022 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002023 struct shell_seat *shseat =
2024 container_of(grab, struct shell_seat, popup_grab.grab);
2025 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002026 wl_fixed_t sx, sy;
2027
2028 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2029 pointer->x, pointer->y,
2030 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002031
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002032 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002033 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002034 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002035 weston_pointer_set_focus(pointer, NULL,
2036 wl_fixed_from_int(0),
2037 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002038 }
2039}
2040
2041static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002042popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002043{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002044 struct weston_pointer *pointer = grab->pointer;
2045 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002046
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002047 if (pointer->focus_resource) {
2048 weston_surface_from_global_fixed(pointer->focus,
2049 pointer->x, pointer->y,
2050 &sx, &sy);
2051 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2052 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002053}
2054
2055static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002056popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002057 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002058{
2059 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002060 struct shell_seat *shseat =
2061 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002062 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002063 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002064 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002065
Daniel Stone37816df2012-05-16 18:45:18 +01002066 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002067 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002068 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002069 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002070 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002071 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002072 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002073 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002074 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075 }
2076
Daniel Stone4dbadb12012-05-30 16:31:51 +01002077 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002078 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002079}
2080
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002081static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082 popup_grab_focus,
2083 popup_grab_motion,
2084 popup_grab_button,
2085};
2086
2087static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002088popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002089{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002090 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002091 struct shell_seat *shseat =
2092 container_of(grab, struct shell_seat, popup_grab.grab);
2093 struct shell_surface *shsurf;
2094 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002095
2096 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002097 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002098 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002099 shseat->popup_grab.grab.interface = NULL;
2100 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002101 /* Send the popup_done event to all the popups open */
2102 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002103 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002104 shsurf->popup.shseat = NULL;
2105 if (prev) {
2106 wl_list_init(&prev->popup.grab_link);
2107 }
2108 prev = shsurf;
2109 }
2110 wl_list_init(&prev->popup.grab_link);
2111 wl_list_init(&shseat->popup_grab.surfaces_list);
2112 }
2113}
2114
2115static void
2116add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2117{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002118 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002119
2120 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002121 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002122 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002123 /* We must make sure here that this popup was opened after
2124 * a mouse press, and not just by moving around with other
2125 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002126 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002127 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002128
Rob Bradforddfe31052013-06-26 19:49:11 +01002129 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002130 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002131 } else {
2132 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002133 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002134}
2135
2136static void
2137remove_popup_grab(struct shell_surface *shsurf)
2138{
2139 struct shell_seat *shseat = shsurf->popup.shseat;
2140
2141 wl_list_remove(&shsurf->popup.grab_link);
2142 wl_list_init(&shsurf->popup.grab_link);
2143 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002144 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002145 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002146 }
2147}
2148
2149static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002150shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002152 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002153 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002154 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002155
2156 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002157
Pekka Paalanen483243f2013-03-08 14:56:50 +02002158 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002159 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2160 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002161
Kristian Høgsberge3148752013-05-06 23:19:49 -04002162 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002163 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002164 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002165 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002166 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002167 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002168}
2169
2170static void
2171shell_surface_set_popup(struct wl_client *client,
2172 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002173 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002174 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002175 struct wl_resource *parent_resource,
2176 int32_t x, int32_t y, uint32_t flags)
2177{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002178 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002179
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002180 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002181 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002182 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002183 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002184 shsurf->popup.x = x;
2185 shsurf->popup.y = y;
2186}
2187
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002188static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002189 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002190 shell_surface_move,
2191 shell_surface_resize,
2192 shell_surface_set_toplevel,
2193 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002194 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002195 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002196 shell_surface_set_maximized,
2197 shell_surface_set_title,
2198 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002199};
2200
2201static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002202destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002203{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002204 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2205
Giulio Camuffo5085a752013-03-25 21:42:45 +01002206 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2207 remove_popup_grab(shsurf);
2208 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002209
Alex Wubd3354b2012-04-17 17:20:49 +08002210 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002211 shell_surface_is_top_fullscreen(shsurf))
2212 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002213
Alex Wuaa08e2d2012-03-05 11:01:40 +08002214 if (shsurf->fullscreen.black_surface)
2215 weston_surface_destroy(shsurf->fullscreen.black_surface);
2216
Alex Wubd3354b2012-04-17 17:20:49 +08002217 /* As destroy_resource() use wl_list_for_each_safe(),
2218 * we can always remove the listener.
2219 */
2220 wl_list_remove(&shsurf->surface_destroy_listener.link);
2221 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002222 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002223 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002225 wl_list_remove(&shsurf->link);
2226 free(shsurf);
2227}
2228
2229static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002230shell_destroy_shell_surface(struct wl_resource *resource)
2231{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002232 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002233
2234 destroy_shell_surface(shsurf);
2235}
2236
2237static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002238shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002239{
2240 struct shell_surface *shsurf = container_of(listener,
2241 struct shell_surface,
2242 surface_destroy_listener);
2243
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002244 if (shsurf->resource)
2245 wl_resource_destroy(shsurf->resource);
2246 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002247 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002248}
2249
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002250static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002251shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002252
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002253static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002254get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002255{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002256 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002257 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002258 else
2259 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002260}
2261
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002262static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002263create_shell_surface(void *shell, struct weston_surface *surface,
2264 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002265{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002266 struct shell_surface *shsurf;
2267
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002268 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002269 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002270 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002271 }
2272
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002273 shsurf = calloc(1, sizeof *shsurf);
2274 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002275 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002276 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002277 }
2278
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002279 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002280 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002281
Tiago Vignattibc052c92012-04-19 16:18:18 +03002282 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002283 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002284 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002285 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002286 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002287 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2288 shsurf->fullscreen.framerate = 0;
2289 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002290 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002291 wl_list_init(&shsurf->fullscreen.transform.link);
2292
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002293 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002294 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002295 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002296 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002297
2298 /* init link so its safe to always remove it in destroy_shell_surface */
2299 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002300 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302 /* empty when not in use */
2303 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002304 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002305
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002306 wl_list_init(&shsurf->workspace_transform.link);
2307
Pekka Paalanen98262232011-12-01 10:42:22 +02002308 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002309 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002310
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002311 shsurf->client = client;
2312
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002313 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002314}
2315
2316static void
2317shell_get_shell_surface(struct wl_client *client,
2318 struct wl_resource *resource,
2319 uint32_t id,
2320 struct wl_resource *surface_resource)
2321{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002322 struct weston_surface *surface =
2323 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002324 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002325 struct shell_surface *shsurf;
2326
2327 if (get_shell_surface(surface)) {
2328 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002329 WL_DISPLAY_ERROR_INVALID_OBJECT,
2330 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002331 return;
2332 }
2333
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002334 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002335 if (!shsurf) {
2336 wl_resource_post_error(surface_resource,
2337 WL_DISPLAY_ERROR_INVALID_OBJECT,
2338 "surface->configure already set");
2339 return;
2340 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002341
Jason Ekstranda85118c2013-06-27 20:17:02 -05002342 shsurf->resource =
2343 wl_resource_create(client,
2344 &wl_shell_surface_interface, 1, id);
2345 wl_resource_set_implementation(shsurf->resource,
2346 &shell_surface_implementation,
2347 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002348}
2349
2350static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002351 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002352};
2353
Kristian Høgsberg07937562011-04-12 17:25:42 -04002354static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002355shell_fade(struct desktop_shell *shell, enum fade_type type);
2356
2357static int
2358screensaver_timeout(void *data)
2359{
2360 struct desktop_shell *shell = data;
2361
2362 shell_fade(shell, FADE_OUT);
2363
2364 return 1;
2365}
2366
2367static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002368handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002369{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002370 struct desktop_shell *shell =
2371 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002372
Pekka Paalanen18027e52011-12-02 16:31:49 +02002373 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002374
2375 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002376 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002377}
2378
2379static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002380launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002381{
2382 if (shell->screensaver.binding)
2383 return;
2384
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002385 if (!shell->screensaver.path) {
2386 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002387 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002388 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002389
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002390 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002391 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002392 return;
2393 }
2394
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002395 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002396 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002397 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002398 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002399}
2400
2401static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002402terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002403{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002404 if (shell->screensaver.process.pid == 0)
2405 return;
2406
2407 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002408}
2409
2410static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002411configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002412{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002413 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002414
Giulio Camuffo184df502013-02-21 11:29:21 +01002415 if (width == 0)
2416 return;
2417
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002418 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2419 if (s->output == es->output && s != es) {
2420 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002421 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002422 }
2423 }
2424
Giulio Camuffo184df502013-02-21 11:29:21 +01002425 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002426
2427 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002428 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002429 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002430 }
2431}
2432
2433static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002434background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002435{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002436 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002437
Giulio Camuffo184df502013-02-21 11:29:21 +01002438 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002439}
2440
2441static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002442desktop_shell_set_background(struct wl_client *client,
2443 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002444 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002445 struct wl_resource *surface_resource)
2446{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002447 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002448 struct weston_surface *surface =
2449 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002450
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002451 if (surface->configure) {
2452 wl_resource_post_error(surface_resource,
2453 WL_DISPLAY_ERROR_INVALID_OBJECT,
2454 "surface role already assigned");
2455 return;
2456 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002457
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002458 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002459 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002460 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002461 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002462 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002463 surface->output->width,
2464 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002465}
2466
2467static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002468panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002469{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002470 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002471
Giulio Camuffo184df502013-02-21 11:29:21 +01002472 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002473}
2474
2475static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002476desktop_shell_set_panel(struct wl_client *client,
2477 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002478 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002479 struct wl_resource *surface_resource)
2480{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002481 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002482 struct weston_surface *surface =
2483 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002484
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002485 if (surface->configure) {
2486 wl_resource_post_error(surface_resource,
2487 WL_DISPLAY_ERROR_INVALID_OBJECT,
2488 "surface role already assigned");
2489 return;
2490 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002491
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002492 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002493 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002494 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002495 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002496 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002497 surface->output->width,
2498 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002499}
2500
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002501static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002502lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002503{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002504 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002505
Giulio Camuffo184df502013-02-21 11:29:21 +01002506 if (width == 0)
2507 return;
2508
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002509 surface->geometry.width = width;
2510 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002511 center_on_output(surface, get_default_output(shell->compositor));
2512
2513 if (!weston_surface_is_mapped(surface)) {
2514 wl_list_insert(&shell->lock_layer.surface_list,
2515 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002516 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002517 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002518 }
2519}
2520
2521static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002522handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002523{
Tiago Vignattibe143262012-04-16 17:31:41 +03002524 struct desktop_shell *shell =
2525 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002526
Martin Minarik6d118362012-06-07 18:01:59 +02002527 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002528 shell->lock_surface = NULL;
2529}
2530
2531static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002532desktop_shell_set_lock_surface(struct wl_client *client,
2533 struct wl_resource *resource,
2534 struct wl_resource *surface_resource)
2535{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002536 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002537 struct weston_surface *surface =
2538 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002539
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002540 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002541
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002542 if (!shell->locked)
2543 return;
2544
Pekka Paalanen98262232011-12-01 10:42:22 +02002545 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002546
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002547 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002548 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002549 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002550
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002551 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002552 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002553}
2554
2555static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002556resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002557{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002558 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002559
Pekka Paalanen77346a62011-11-30 16:26:35 +02002560 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002561
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002562 wl_list_remove(&shell->lock_layer.link);
2563 wl_list_insert(&shell->compositor->cursor_layer.link,
2564 &shell->fullscreen_layer.link);
2565 wl_list_insert(&shell->fullscreen_layer.link,
2566 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002567 if (shell->showing_input_panels) {
2568 wl_list_insert(&shell->panel_layer.link,
2569 &shell->input_panel_layer.link);
2570 wl_list_insert(&shell->input_panel_layer.link,
2571 &ws->layer.link);
2572 } else {
2573 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2574 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002575
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002576 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002577
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002578 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002579 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002580 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002581}
2582
2583static void
2584desktop_shell_unlock(struct wl_client *client,
2585 struct wl_resource *resource)
2586{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002587 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002588
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002589 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002590
2591 if (shell->locked)
2592 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002593}
2594
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002595static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002596desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002597 struct wl_resource *resource,
2598 struct wl_resource *surface_resource)
2599{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002600 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002601
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002602 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002603}
2604
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002605static void
2606desktop_shell_desktop_ready(struct wl_client *client,
2607 struct wl_resource *resource)
2608{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002609 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002610
2611 shell_fade_startup(shell);
2612}
2613
Kristian Høgsberg75840622011-09-06 13:48:16 -04002614static const struct desktop_shell_interface desktop_shell_implementation = {
2615 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002616 desktop_shell_set_panel,
2617 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002618 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002619 desktop_shell_set_grab_surface,
2620 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002621};
2622
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002623static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002624get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002625{
2626 struct shell_surface *shsurf;
2627
2628 shsurf = get_shell_surface(surface);
2629 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002630 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002631 return shsurf->type;
2632}
2633
Kristian Høgsberg75840622011-09-06 13:48:16 -04002634static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002635move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002636{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002637 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002638 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002639 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002640 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002641
Pekka Paalanen01388e22013-04-25 13:57:44 +03002642 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002643 if (surface == NULL)
2644 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002645
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002646 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002647 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2648 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002649 return;
2650
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002651 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002652}
2653
2654static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002655resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002656{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002657 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002658 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002659 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002660 uint32_t edges = 0;
2661 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002662 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002663
Pekka Paalanen01388e22013-04-25 13:57:44 +03002664 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002665 if (surface == NULL)
2666 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002667
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002668 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002669 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2670 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002671 return;
2672
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002673 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002674 wl_fixed_to_int(seat->pointer->grab_x),
2675 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002676 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002677
Pekka Paalanen60921e52012-01-25 15:55:43 +02002678 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002679 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002680 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002681 edges |= 0;
2682 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002683 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002684
Pekka Paalanen60921e52012-01-25 15:55:43 +02002685 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002686 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002687 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002688 edges |= 0;
2689 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002690 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002691
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002692 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002693}
2694
2695static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002696surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002697 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002698{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002699 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002700 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002701 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002702 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002703 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002704
Pekka Paalanen01388e22013-04-25 13:57:44 +03002705 /* XXX: broken for windows containing sub-surfaces */
2706 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002707 if (surface == NULL)
2708 return;
2709
2710 shsurf = get_shell_surface(surface);
2711 if (!shsurf)
2712 return;
2713
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002714 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002715
Scott Moreau02709af2012-05-22 01:54:10 -06002716 if (surface->alpha > 1.0)
2717 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002718 if (surface->alpha < step)
2719 surface->alpha = step;
2720
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002721 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002722 weston_surface_damage(surface);
2723}
2724
2725static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002726do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002727 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002728{
Daniel Stone37816df2012-05-16 18:45:18 +01002729 struct weston_seat *ws = (struct weston_seat *) seat;
2730 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002731 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002732 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002733
2734 wl_list_for_each(output, &compositor->output_list, link) {
2735 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002736 wl_fixed_to_double(seat->pointer->x),
2737 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002738 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002739 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002740 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002741 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002742 increment = -output->zoom.increment;
2743 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002744 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002745 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002746 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002747 else
2748 increment = 0;
2749
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002750 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002751
Scott Moreaue6603982012-06-11 13:07:51 -06002752 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002753 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002754 else if (output->zoom.level > output->zoom.max_level)
2755 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002756 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002757 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002758 output->disable_planes++;
2759 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002760
Scott Moreaue6603982012-06-11 13:07:51 -06002761 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002762
Scott Moreau8dacaab2012-06-17 18:10:58 -06002763 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002764 }
2765 }
2766}
2767
Scott Moreauccbf29d2012-02-22 14:21:41 -07002768static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002769zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002770 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002771{
2772 do_zoom(seat, time, 0, axis, value);
2773}
2774
2775static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002776zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002777 void *data)
2778{
2779 do_zoom(seat, time, key, 0, 0);
2780}
2781
2782static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002783terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002784 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002785{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002786 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002787
Daniel Stone325fc2d2012-05-30 16:31:58 +01002788 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002789}
2790
2791static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002792rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002793{
2794 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002795 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002796 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002797 struct shell_surface *shsurf = rotate->base.shsurf;
2798 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002799 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002800
2801 if (!shsurf)
2802 return;
2803
2804 surface = shsurf->surface;
2805
2806 cx = 0.5f * surface->geometry.width;
2807 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002808
Daniel Stone37816df2012-05-16 18:45:18 +01002809 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2810 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002811 r = sqrtf(dx * dx + dy * dy);
2812
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002813 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002814 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002815
2816 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002817 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002818 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002819
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002820 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002821 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002822
2823 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002824 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002825 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002826 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002827 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002828
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002829 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002830 &shsurf->surface->geometry.transformation_list,
2831 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002832 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002833 wl_list_init(&shsurf->rotation.transform.link);
2834 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002835 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002836 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002837
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002838 /* We need to adjust the position of the surface
2839 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002840 cposx = surface->geometry.x + cx;
2841 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002842 dposx = rotate->center.x - cposx;
2843 dposy = rotate->center.y - cposy;
2844 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002845 weston_surface_set_position(surface,
2846 surface->geometry.x + dposx,
2847 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002848 }
2849
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002850 /* Repaint implies weston_surface_update_transform(), which
2851 * lazily applies the damage due to rotation update.
2852 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002853 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002854}
2855
2856static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002857rotate_grab_button(struct weston_pointer_grab *grab,
2858 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002859{
2860 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002861 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002862 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002863 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002864 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002865
Daniel Stone4dbadb12012-05-30 16:31:51 +01002866 if (pointer->button_count == 0 &&
2867 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002868 if (shsurf)
2869 weston_matrix_multiply(&shsurf->rotation.rotation,
2870 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002871 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002872 free(rotate);
2873 }
2874}
2875
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002876static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002877 noop_grab_focus,
2878 rotate_grab_motion,
2879 rotate_grab_button,
2880};
2881
2882static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002883surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002884{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002885 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002886 float dx, dy;
2887 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002888
Pekka Paalanen460099f2012-01-20 16:48:25 +02002889 rotate = malloc(sizeof *rotate);
2890 if (!rotate)
2891 return;
2892
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002893 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02002894 surface->surface->geometry.width * 0.5f,
2895 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002896 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002897
Daniel Stone37816df2012-05-16 18:45:18 +01002898 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2899 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002900 r = sqrtf(dx * dx + dy * dy);
2901 if (r > 20.0f) {
2902 struct weston_matrix inverse;
2903
2904 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002905 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002906 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002907
2908 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002909 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002910 } else {
2911 weston_matrix_init(&surface->rotation.rotation);
2912 weston_matrix_init(&rotate->rotation);
2913 }
2914
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002915 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2916 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002917}
2918
2919static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002920rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002921 void *data)
2922{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002923 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002924 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002925 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002926 struct shell_surface *surface;
2927
Pekka Paalanen01388e22013-04-25 13:57:44 +03002928 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002929 if (base_surface == NULL)
2930 return;
2931
2932 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002933 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2934 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002935 return;
2936
2937 surface_rotate(surface, seat);
2938}
2939
2940static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002941lower_fullscreen_layer(struct desktop_shell *shell)
2942{
2943 struct workspace *ws;
2944 struct weston_surface *surface, *prev;
2945
2946 ws = get_current_workspace(shell);
2947 wl_list_for_each_reverse_safe(surface, prev,
2948 &shell->fullscreen_layer.surface_list,
2949 layer_link)
2950 weston_surface_restack(surface, &ws->layer.surface_list);
2951}
2952
2953static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002954activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002955 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002956{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002957 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002958 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002959 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002960
Pekka Paalanen01388e22013-04-25 13:57:44 +03002961 main_surface = weston_surface_get_main_surface(es);
2962
Daniel Stone37816df2012-05-16 18:45:18 +01002963 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002964
Jonas Ådahl8538b222012-08-29 22:13:03 +02002965 state = ensure_focus_state(shell, seat);
2966 if (state == NULL)
2967 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002968
2969 state->keyboard_focus = es;
2970 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002971 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002972
Pekka Paalanen01388e22013-04-25 13:57:44 +03002973 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002974 case SHELL_SURFACE_FULLSCREEN:
2975 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002976 shell_stack_fullscreen(get_shell_surface(main_surface));
2977 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002978 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002979 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002980 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002981 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002982 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002983 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002984 }
2985}
2986
Alex Wu21858432012-04-01 20:13:08 +08002987/* no-op func for checking black surface */
2988static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002989black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Alex Wu21858432012-04-01 20:13:08 +08002990{
2991}
2992
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002993static bool
Alex Wu21858432012-04-01 20:13:08 +08002994is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2995{
2996 if (es->configure == black_surface_configure) {
2997 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002998 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002999 return true;
3000 }
3001 return false;
3002}
3003
Kristian Høgsberg75840622011-09-06 13:48:16 -04003004static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003005click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003006 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003007{
Daniel Stone37816df2012-05-16 18:45:18 +01003008 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003009 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003010 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003011 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003012
Daniel Stone37816df2012-05-16 18:45:18 +01003013 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003014 if (!focus)
3015 return;
3016
Pekka Paalanen01388e22013-04-25 13:57:44 +03003017 if (is_black_surface(focus, &main_surface))
3018 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003019
Pekka Paalanen01388e22013-04-25 13:57:44 +03003020 main_surface = weston_surface_get_main_surface(focus);
3021 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003022 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003023
Daniel Stone325fc2d2012-05-30 16:31:58 +01003024 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003025 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003026}
3027
3028static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003029lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003030{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003031 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003032
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003033 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003034 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003035 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003036 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003037
3038 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003039
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003040 /* Hide all surfaces by removing the fullscreen, panel and
3041 * toplevel layers. This way nothing else can show or receive
3042 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003043
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003044 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003045 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003046 if (shell->showing_input_panels)
3047 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003048 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003049 wl_list_insert(&shell->compositor->cursor_layer.link,
3050 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003051
Pekka Paalanen77346a62011-11-30 16:26:35 +02003052 launch_screensaver(shell);
3053
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003054 /* TODO: disable bindings that should not work while locked. */
3055
3056 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003057}
3058
3059static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003060unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003061{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003062 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003063 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003064 return;
3065 }
3066
3067 /* If desktop-shell client has gone away, unlock immediately. */
3068 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003069 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003070 return;
3071 }
3072
3073 if (shell->prepare_event_sent)
3074 return;
3075
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003076 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003077 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003078}
3079
3080static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003081shell_fade_done(struct weston_surface_animation *animation, void *data)
3082{
3083 struct desktop_shell *shell = data;
3084
3085 shell->fade.animation = NULL;
3086
3087 switch (shell->fade.type) {
3088 case FADE_IN:
3089 weston_surface_destroy(shell->fade.surface);
3090 shell->fade.surface = NULL;
3091 break;
3092 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003093 lock(shell);
3094 break;
3095 }
3096}
3097
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003098static struct weston_surface *
3099shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003100{
3101 struct weston_compositor *compositor = shell->compositor;
3102 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003103
3104 surface = weston_surface_create(compositor);
3105 if (!surface)
3106 return NULL;
3107
3108 weston_surface_configure(surface, 0, 0, 8192, 8192);
3109 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3110 wl_list_insert(&compositor->fade_layer.surface_list,
3111 &surface->layer_link);
3112 pixman_region32_init(&surface->input);
3113
3114 return surface;
3115}
3116
3117static void
3118shell_fade(struct desktop_shell *shell, enum fade_type type)
3119{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003120 float tint;
3121
3122 switch (type) {
3123 case FADE_IN:
3124 tint = 0.0;
3125 break;
3126 case FADE_OUT:
3127 tint = 1.0;
3128 break;
3129 default:
3130 weston_log("shell: invalid fade type\n");
3131 return;
3132 }
3133
3134 shell->fade.type = type;
3135
3136 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003137 shell->fade.surface = shell_fade_create_surface(shell);
3138 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003139 return;
3140
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003141 shell->fade.surface->alpha = 1.0 - tint;
3142 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003143 }
3144
3145 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003146 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003147 else
3148 shell->fade.animation =
3149 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003150 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003151 shell_fade_done, shell);
3152}
3153
3154static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003155do_shell_fade_startup(void *data)
3156{
3157 struct desktop_shell *shell = data;
3158
3159 shell_fade(shell, FADE_IN);
3160}
3161
3162static void
3163shell_fade_startup(struct desktop_shell *shell)
3164{
3165 struct wl_event_loop *loop;
3166
3167 if (!shell->fade.startup_timer)
3168 return;
3169
3170 wl_event_source_remove(shell->fade.startup_timer);
3171 shell->fade.startup_timer = NULL;
3172
3173 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3174 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3175}
3176
3177static int
3178fade_startup_timeout(void *data)
3179{
3180 struct desktop_shell *shell = data;
3181
3182 shell_fade_startup(shell);
3183 return 0;
3184}
3185
3186static void
3187shell_fade_init(struct desktop_shell *shell)
3188{
3189 /* Make compositor output all black, and wait for the desktop-shell
3190 * client to signal it is ready, then fade in. The timer triggers a
3191 * fade-in, in case the desktop-shell client takes too long.
3192 */
3193
3194 struct wl_event_loop *loop;
3195
3196 if (shell->fade.surface != NULL) {
3197 weston_log("%s: warning: fade surface already exists\n",
3198 __func__);
3199 return;
3200 }
3201
3202 shell->fade.surface = shell_fade_create_surface(shell);
3203 if (!shell->fade.surface)
3204 return;
3205
3206 weston_surface_update_transform(shell->fade.surface);
3207 weston_surface_damage(shell->fade.surface);
3208
3209 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3210 shell->fade.startup_timer =
3211 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3212 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3213}
3214
3215static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003216idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003217{
3218 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003219 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003220
3221 shell_fade(shell, FADE_OUT);
3222 /* lock() is called from shell_fade_done() */
3223}
3224
3225static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003226wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003227{
3228 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003229 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003230
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003231 unlock(shell);
3232}
3233
3234static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003235show_input_panels(struct wl_listener *listener, void *data)
3236{
3237 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003238 container_of(listener, struct desktop_shell,
3239 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003240 struct input_panel_surface *surface, *next;
3241 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003242
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003243 shell->text_input.surface = (struct weston_surface*)data;
3244
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003245 if (shell->showing_input_panels)
3246 return;
3247
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003248 shell->showing_input_panels = true;
3249
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003250 if (!shell->locked)
3251 wl_list_insert(&shell->panel_layer.link,
3252 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003253
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003254 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003255 &shell->input_panel.surfaces, link) {
3256 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003257 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003258 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003259 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003260 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003261 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003262 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003263 weston_surface_damage(ws);
3264 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003265 }
3266}
3267
3268static void
3269hide_input_panels(struct wl_listener *listener, void *data)
3270{
3271 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003272 container_of(listener, struct desktop_shell,
3273 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003274 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003275
Jan Arne Petersen61381972013-01-31 15:52:21 +01003276 if (!shell->showing_input_panels)
3277 return;
3278
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003279 shell->showing_input_panels = false;
3280
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003281 if (!shell->locked)
3282 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003283
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003284 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003285 &shell->input_panel_layer.surface_list, layer_link)
3286 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003287}
3288
3289static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003290update_input_panels(struct wl_listener *listener, void *data)
3291{
3292 struct desktop_shell *shell =
3293 container_of(listener, struct desktop_shell,
3294 update_input_panel_listener);
3295
3296 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3297}
3298
3299static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003300center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003301{
Giulio Camuffob8366642013-04-25 13:57:46 +03003302 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003303 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003304
Giulio Camuffob8366642013-04-25 13:57:46 +03003305 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3306
3307 x = output->x + (output->width - width) / 2 - surf_x / 2;
3308 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003309
3310 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003311}
3312
3313static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003314weston_surface_set_initial_position (struct weston_surface *surface,
3315 struct desktop_shell *shell)
3316{
3317 struct weston_compositor *compositor = shell->compositor;
3318 int ix = 0, iy = 0;
3319 int range_x, range_y;
3320 int dx, dy, x, y, panel_height;
3321 struct weston_output *output, *target_output = NULL;
3322 struct weston_seat *seat;
3323
3324 /* As a heuristic place the new window on the same output as the
3325 * pointer. Falling back to the output containing 0, 0.
3326 *
3327 * TODO: Do something clever for touch too?
3328 */
3329 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003330 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003331 ix = wl_fixed_to_int(seat->pointer->x);
3332 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003333 break;
3334 }
3335 }
3336
3337 wl_list_for_each(output, &compositor->output_list, link) {
3338 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3339 target_output = output;
3340 break;
3341 }
3342 }
3343
3344 if (!target_output) {
3345 weston_surface_set_position(surface, 10 + random() % 400,
3346 10 + random() % 400);
3347 return;
3348 }
3349
3350 /* Valid range within output where the surface will still be onscreen.
3351 * If this is negative it means that the surface is bigger than
3352 * output.
3353 */
3354 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003355 range_x = target_output->width - surface->geometry.width;
3356 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003357 surface->geometry.height;
3358
Rob Bradford4cb88c72012-08-13 15:18:44 +01003359 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003360 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003361 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003362 dx = 0;
3363
3364 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003365 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003366 else
3367 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003368
3369 x = target_output->x + dx;
3370 y = target_output->y + dy;
3371
3372 weston_surface_set_position (surface, x, y);
3373}
3374
3375static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003376map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003377 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003378{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003379 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003380 struct shell_surface *shsurf = get_shell_surface(surface);
3381 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003382 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003383 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003384 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003385 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003386 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003387
Pekka Paalanen60921e52012-01-25 15:55:43 +02003388 surface->geometry.width = width;
3389 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003390 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003391
3392 /* initial positioning, see also configure() */
3393 switch (surface_type) {
3394 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003395 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003396 break;
Alex Wu4539b082012-03-01 12:57:46 +08003397 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003398 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003399 shell_map_fullscreen(shsurf);
3400 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003401 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003402 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003403 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003404 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3405 NULL, NULL);
3406 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3407 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003408 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003409 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003410 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003411 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003412 case SHELL_SURFACE_NONE:
3413 weston_surface_set_position(surface,
3414 surface->geometry.x + sx,
3415 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003416 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003417 default:
3418 ;
3419 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003420
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003421 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003422 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003423 case SHELL_SURFACE_POPUP:
3424 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003425 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003426 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3427 break;
Alex Wu4539b082012-03-01 12:57:46 +08003428 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003429 case SHELL_SURFACE_NONE:
3430 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003431 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003432 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003433 ws = get_current_workspace(shell);
3434 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003435 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003436 }
3437
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003438 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003439 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003440 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3441 surface->output = shsurf->output;
3442 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003443
Juan Zhao7bb92f02011-12-15 11:31:51 -05003444 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003445 /* XXX: xwayland's using the same fields for transient type */
3446 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003447 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003448 if (shsurf->transient.flags ==
3449 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3450 break;
3451 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003452 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003453 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003454 if (!shell->locked) {
3455 wl_list_for_each(seat, &compositor->seat_list, link)
3456 activate(shell, surface, seat);
3457 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003458 break;
3459 default:
3460 break;
3461 }
3462
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003463 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003464 {
3465 switch (shell->win_animation_type) {
3466 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003467 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003468 break;
3469 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003470 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003471 break;
3472 default:
3473 break;
3474 }
3475 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003476}
3477
3478static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003479configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003480 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003481{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003482 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3483 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003484 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003485
Pekka Paalanen77346a62011-11-30 16:26:35 +02003486 shsurf = get_shell_surface(surface);
3487 if (shsurf)
3488 surface_type = shsurf->type;
3489
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003490 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003491
3492 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003493 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003494 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003495 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003496 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003497 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003498 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003499 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3500 NULL, NULL);
3501 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003502 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003503 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003504 break;
Alex Wu4539b082012-03-01 12:57:46 +08003505 case SHELL_SURFACE_TOPLEVEL:
3506 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003507 default:
3508 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003509 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003510
Alex Wu4539b082012-03-01 12:57:46 +08003511 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003512 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003513 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003514
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003515 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003516 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003517 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003518}
3519
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003520static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003521shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003522{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003523 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003524 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003525
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003526 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003527
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003528 if (!weston_surface_is_mapped(es) &&
3529 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003530 remove_popup_grab(shsurf);
3531 }
3532
Giulio Camuffo184df502013-02-21 11:29:21 +01003533 if (width == 0)
3534 return;
3535
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003536 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003537 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003538 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003539 type_changed = 1;
3540 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003541
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003542 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003543 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003544 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003545 es->geometry.width != width ||
3546 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003547 float from_x, from_y;
3548 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003549
3550 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3551 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3552 configure(shell, es,
3553 es->geometry.x + to_x - from_x,
3554 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003555 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003556 }
3557}
3558
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003559static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003560
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003561static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003562desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003563{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003564 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003565 struct desktop_shell *shell =
3566 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003567
3568 shell->child.process.pid = 0;
3569 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003570
3571 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3572 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003573 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003574 shell->child.deathstamp = time;
3575 shell->child.deathcount = 0;
3576 }
3577
3578 shell->child.deathcount++;
3579 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003580 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003581 return;
3582 }
3583
Martin Minarik6d118362012-06-07 18:01:59 +02003584 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003585 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003586 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003587}
3588
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003589static void
3590launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003591{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003592 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003593 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003594
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003595 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003596 &shell->child.process,
3597 shell_exe,
3598 desktop_shell_sigchld);
3599
3600 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003601 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003602}
3603
3604static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003605bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3606{
Tiago Vignattibe143262012-04-16 17:31:41 +03003607 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003608 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003609
Jason Ekstranda85118c2013-06-27 20:17:02 -05003610 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3611 if (resource)
3612 wl_resource_set_implementation(resource, &shell_implementation,
3613 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003614}
3615
Kristian Høgsberg75840622011-09-06 13:48:16 -04003616static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003617unbind_desktop_shell(struct wl_resource *resource)
3618{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003619 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003620
3621 if (shell->locked)
3622 resume_desktop(shell);
3623
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003624 shell->child.desktop_shell = NULL;
3625 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003626}
3627
3628static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003629bind_desktop_shell(struct wl_client *client,
3630 void *data, uint32_t version, uint32_t id)
3631{
Tiago Vignattibe143262012-04-16 17:31:41 +03003632 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003633 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003634
Jason Ekstranda85118c2013-06-27 20:17:02 -05003635 resource = wl_resource_create(client, &desktop_shell_interface,
3636 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003637
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003638 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003639 wl_resource_set_implementation(resource,
3640 &desktop_shell_implementation,
3641 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003642 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003643
3644 if (version < 2)
3645 shell_fade_startup(shell);
3646
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003647 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003648 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003649
3650 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3651 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003652 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003653}
3654
Pekka Paalanen6e168112011-11-24 11:34:05 +02003655static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003656screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003657{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003658 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003659
Giulio Camuffo184df502013-02-21 11:29:21 +01003660 if (width == 0)
3661 return;
3662
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003663 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003664 if (!shell->locked)
3665 return;
3666
3667 center_on_output(surface, surface->output);
3668
3669 if (wl_list_empty(&surface->layer_link)) {
3670 wl_list_insert(shell->lock_layer.surface_list.prev,
3671 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003672 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003673 wl_event_source_timer_update(shell->screensaver.timer,
3674 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003675 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003676 }
3677}
3678
3679static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003680screensaver_set_surface(struct wl_client *client,
3681 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003682 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003683 struct wl_resource *output_resource)
3684{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003685 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003686 struct weston_surface *surface =
3687 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003688 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003689
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003690 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003691 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003692 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003693}
3694
3695static const struct screensaver_interface screensaver_implementation = {
3696 screensaver_set_surface
3697};
3698
3699static void
3700unbind_screensaver(struct wl_resource *resource)
3701{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003702 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003703
Pekka Paalanen77346a62011-11-30 16:26:35 +02003704 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003705}
3706
3707static void
3708bind_screensaver(struct wl_client *client,
3709 void *data, uint32_t version, uint32_t id)
3710{
Tiago Vignattibe143262012-04-16 17:31:41 +03003711 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003712 struct wl_resource *resource;
3713
Jason Ekstranda85118c2013-06-27 20:17:02 -05003714 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003715
Pekka Paalanen77346a62011-11-30 16:26:35 +02003716 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003717 wl_resource_set_implementation(resource,
3718 &screensaver_implementation,
3719 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003720 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003721 return;
3722 }
3723
3724 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3725 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003726 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003727}
3728
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003729static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003730input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003731{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003732 struct input_panel_surface *ip_surface = surface->configure_private;
3733 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003734 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003735 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003736 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003737
Giulio Camuffo184df502013-02-21 11:29:21 +01003738 if (width == 0)
3739 return;
3740
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003741 if (!weston_surface_is_mapped(surface)) {
3742 if (!shell->showing_input_panels)
3743 return;
3744
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003745 show_surface = 1;
3746 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003747
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003748 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003749
3750 if (ip_surface->panel) {
3751 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3752 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3753 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003754 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003755
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003756 x = ip_surface->output->x + (mode->width - width) / 2;
3757 y = ip_surface->output->y + mode->height - height;
3758 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003759
3760 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003761 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003762 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003763
3764 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003765 wl_list_insert(&shell->input_panel_layer.surface_list,
3766 &surface->layer_link);
3767 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003768 weston_surface_damage(surface);
3769 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3770 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003771}
3772
3773static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003774destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003775{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003776 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3777
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003778 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003779 wl_list_remove(&input_panel_surface->link);
3780
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003781 input_panel_surface->surface->configure = NULL;
3782
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003783 free(input_panel_surface);
3784}
3785
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003786static struct input_panel_surface *
3787get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003788{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003789 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003790 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003791 } else {
3792 return NULL;
3793 }
3794}
3795
3796static void
3797input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3798{
3799 struct input_panel_surface *ipsurface = container_of(listener,
3800 struct input_panel_surface,
3801 surface_destroy_listener);
3802
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003803 if (ipsurface->resource) {
3804 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003805 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003806 destroy_input_panel_surface(ipsurface);
3807 }
3808}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003809
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003810static struct input_panel_surface *
3811create_input_panel_surface(struct desktop_shell *shell,
3812 struct weston_surface *surface)
3813{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003814 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003815
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003816 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3817 if (!input_panel_surface)
3818 return NULL;
3819
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003820 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003821 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003822
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003823 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003824
3825 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003826
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003827 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003828 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003829 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003830 &input_panel_surface->surface_destroy_listener);
3831
3832 wl_list_init(&input_panel_surface->link);
3833
3834 return input_panel_surface;
3835}
3836
3837static void
3838input_panel_surface_set_toplevel(struct wl_client *client,
3839 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003840 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003841 uint32_t position)
3842{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003843 struct input_panel_surface *input_panel_surface =
3844 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003845 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003846
3847 wl_list_insert(&shell->input_panel.surfaces,
3848 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003849
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003850 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003851 input_panel_surface->panel = 0;
3852}
3853
3854static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003855input_panel_surface_set_overlay_panel(struct wl_client *client,
3856 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003857{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003858 struct input_panel_surface *input_panel_surface =
3859 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003860 struct desktop_shell *shell = input_panel_surface->shell;
3861
3862 wl_list_insert(&shell->input_panel.surfaces,
3863 &input_panel_surface->link);
3864
3865 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003866}
3867
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003868static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003869 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003870 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003871};
3872
3873static void
3874destroy_input_panel_surface_resource(struct wl_resource *resource)
3875{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003876 struct input_panel_surface *ipsurf =
3877 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003878
3879 destroy_input_panel_surface(ipsurf);
3880}
3881
3882static void
3883input_panel_get_input_panel_surface(struct wl_client *client,
3884 struct wl_resource *resource,
3885 uint32_t id,
3886 struct wl_resource *surface_resource)
3887{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003888 struct weston_surface *surface =
3889 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003890 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003891 struct input_panel_surface *ipsurf;
3892
3893 if (get_input_panel_surface(surface)) {
3894 wl_resource_post_error(surface_resource,
3895 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003896 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003897 return;
3898 }
3899
3900 ipsurf = create_input_panel_surface(shell, surface);
3901 if (!ipsurf) {
3902 wl_resource_post_error(surface_resource,
3903 WL_DISPLAY_ERROR_INVALID_OBJECT,
3904 "surface->configure already set");
3905 return;
3906 }
3907
Jason Ekstranda85118c2013-06-27 20:17:02 -05003908 ipsurf->resource =
3909 wl_resource_create(client,
3910 &wl_input_panel_surface_interface, 1, id);
3911 wl_resource_set_implementation(ipsurf->resource,
3912 &input_panel_surface_implementation,
3913 ipsurf,
3914 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003915}
3916
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003917static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003918 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003919};
3920
3921static void
3922unbind_input_panel(struct wl_resource *resource)
3923{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003924 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003925
3926 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003927}
3928
3929static void
3930bind_input_panel(struct wl_client *client,
3931 void *data, uint32_t version, uint32_t id)
3932{
3933 struct desktop_shell *shell = data;
3934 struct wl_resource *resource;
3935
Jason Ekstranda85118c2013-06-27 20:17:02 -05003936 resource = wl_resource_create(client,
3937 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003938
3939 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003940 wl_resource_set_implementation(resource,
3941 &input_panel_implementation,
3942 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003943 shell->input_panel.binding = resource;
3944 return;
3945 }
3946
3947 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3948 "interface object already bound");
3949 wl_resource_destroy(resource);
3950}
3951
Kristian Høgsberg07045392012-02-19 18:52:44 -05003952struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003953 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003954 struct weston_surface *current;
3955 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003956 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003957};
3958
3959static void
3960switcher_next(struct switcher *switcher)
3961{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003962 struct weston_surface *surface;
3963 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003964 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003965 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003966
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003967 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003968 switch (get_shell_surface_type(surface)) {
3969 case SHELL_SURFACE_TOPLEVEL:
3970 case SHELL_SURFACE_FULLSCREEN:
3971 case SHELL_SURFACE_MAXIMIZED:
3972 if (first == NULL)
3973 first = surface;
3974 if (prev == switcher->current)
3975 next = surface;
3976 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003977 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003978 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003979 weston_surface_damage(surface);
3980 break;
3981 default:
3982 break;
3983 }
Alex Wu1659daa2012-04-01 20:13:09 +08003984
3985 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003986 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003987 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003988 weston_surface_damage(surface);
3989 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003990 }
3991
3992 if (next == NULL)
3993 next = first;
3994
Alex Wu07b26062012-03-12 16:06:01 +08003995 if (next == NULL)
3996 return;
3997
Kristian Høgsberg07045392012-02-19 18:52:44 -05003998 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003999 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004000
4001 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004002 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004003
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004004 shsurf = get_shell_surface(switcher->current);
4005 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004006 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004007}
4008
4009static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004010switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004011{
4012 struct switcher *switcher =
4013 container_of(listener, struct switcher, listener);
4014
4015 switcher_next(switcher);
4016}
4017
4018static void
Daniel Stone351eb612012-05-31 15:27:47 -04004019switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004020{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004021 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004022 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004023 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004024
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004025 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004026 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004027 weston_surface_damage(surface);
4028 }
4029
Alex Wu07b26062012-03-12 16:06:01 +08004030 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004031 activate(switcher->shell, switcher->current,
4032 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004033 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004034 weston_keyboard_end_grab(keyboard);
4035 if (keyboard->input_method_resource)
4036 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004037 free(switcher);
4038}
4039
4040static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004041switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004042 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004043{
4044 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004045 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004046
Daniel Stonec9785ea2012-05-30 16:31:52 +01004047 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004048 switcher_next(switcher);
4049}
4050
4051static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004052switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004053 uint32_t mods_depressed, uint32_t mods_latched,
4054 uint32_t mods_locked, uint32_t group)
4055{
4056 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004057 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004058
Daniel Stone351eb612012-05-31 15:27:47 -04004059 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4060 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004061}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004062
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004063static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004064 switcher_key,
4065 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004066};
4067
4068static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004069switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004070 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004071{
Tiago Vignattibe143262012-04-16 17:31:41 +03004072 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004073 struct switcher *switcher;
4074
4075 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004076 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004077 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004078 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004079 wl_list_init(&switcher->listener.link);
4080
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004081 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004082 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004083 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004084 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4085 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004086 switcher_next(switcher);
4087}
4088
Pekka Paalanen3c647232011-12-22 13:43:43 +02004089static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004090backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004091 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004092{
4093 struct weston_compositor *compositor = data;
4094 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004095 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004096
4097 /* TODO: we're limiting to simple use cases, where we assume just
4098 * control on the primary display. We'd have to extend later if we
4099 * ever get support for setting backlights on random desktop LCD
4100 * panels though */
4101 output = get_default_output(compositor);
4102 if (!output)
4103 return;
4104
4105 if (!output->set_backlight)
4106 return;
4107
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004108 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4109 backlight_new = output->backlight_current - 25;
4110 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4111 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004112
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004113 if (backlight_new < 5)
4114 backlight_new = 5;
4115 if (backlight_new > 255)
4116 backlight_new = 255;
4117
4118 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004119 output->set_backlight(output, output->backlight_current);
4120}
4121
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004122struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004123 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004124 struct weston_seat *seat;
4125 uint32_t key[2];
4126 int key_released[2];
4127};
4128
4129static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004130debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004131 uint32_t key, uint32_t state)
4132{
4133 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4134 struct wl_resource *resource;
4135 struct wl_display *display;
4136 uint32_t serial;
4137 int send = 0, terminate = 0;
4138 int check_binding = 1;
4139 int i;
4140
4141 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4142 /* Do not run bindings on key releases */
4143 check_binding = 0;
4144
4145 for (i = 0; i < 2; i++)
4146 if (key == db->key[i])
4147 db->key_released[i] = 1;
4148
4149 if (db->key_released[0] && db->key_released[1]) {
4150 /* All key releases been swalled so end the grab */
4151 terminate = 1;
4152 } else if (key != db->key[0] && key != db->key[1]) {
4153 /* Should not swallow release of other keys */
4154 send = 1;
4155 }
4156 } else if (key == db->key[0] && !db->key_released[0]) {
4157 /* Do not check bindings for the first press of the binding
4158 * key. This allows it to be used as a debug shortcut.
4159 * We still need to swallow this event. */
4160 check_binding = 0;
4161 } else if (db->key[1]) {
4162 /* If we already ran a binding don't process another one since
4163 * we can't keep track of all the binding keys that were
4164 * pressed in order to swallow the release events. */
4165 send = 1;
4166 check_binding = 0;
4167 }
4168
4169 if (check_binding) {
4170 struct weston_compositor *ec = db->seat->compositor;
4171
4172 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4173 key, state)) {
4174 /* We ran a binding so swallow the press and keep the
4175 * grab to swallow the released too. */
4176 send = 0;
4177 terminate = 0;
4178 db->key[1] = key;
4179 } else {
4180 /* Terminate the grab since the key pressed is not a
4181 * debug binding key. */
4182 send = 1;
4183 terminate = 1;
4184 }
4185 }
4186
4187 if (send) {
4188 resource = grab->keyboard->focus_resource;
4189
4190 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004191 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004192 serial = wl_display_next_serial(display);
4193 wl_keyboard_send_key(resource, serial, time, key, state);
4194 }
4195 }
4196
4197 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004198 weston_keyboard_end_grab(grab->keyboard);
4199 if (grab->keyboard->input_method_resource)
4200 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004201 free(db);
4202 }
4203}
4204
4205static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004206debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004207 uint32_t mods_depressed, uint32_t mods_latched,
4208 uint32_t mods_locked, uint32_t group)
4209{
4210 struct wl_resource *resource;
4211
4212 resource = grab->keyboard->focus_resource;
4213 if (!resource)
4214 return;
4215
4216 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4217 mods_latched, mods_locked, group);
4218}
4219
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004220struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004221 debug_binding_key,
4222 debug_binding_modifiers
4223};
4224
4225static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004226debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004227{
4228 struct debug_binding_grab *grab;
4229
4230 grab = calloc(1, sizeof *grab);
4231 if (!grab)
4232 return;
4233
4234 grab->seat = (struct weston_seat *) seat;
4235 grab->key[0] = key;
4236 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004237 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004238}
4239
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004240static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004241force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004242 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004243{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004244 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004245 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004246 struct desktop_shell *shell = data;
4247 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004248 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004249
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004250 focus_surface = seat->keyboard->focus;
4251 if (!focus_surface)
4252 return;
4253
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004254 wl_signal_emit(&compositor->kill_signal, focus_surface);
4255
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004256 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004257 wl_client_get_credentials(client, &pid, NULL, NULL);
4258
4259 /* Skip clients that we launched ourselves (the credentials of
4260 * the socketpair is ours) */
4261 if (pid == getpid())
4262 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004263
Daniel Stone325fc2d2012-05-30 16:31:58 +01004264 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004265}
4266
4267static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004268workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004269 uint32_t key, void *data)
4270{
4271 struct desktop_shell *shell = data;
4272 unsigned int new_index = shell->workspaces.current;
4273
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004274 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004275 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004276 if (new_index != 0)
4277 new_index--;
4278
4279 change_workspace(shell, new_index);
4280}
4281
4282static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004283workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004284 uint32_t key, void *data)
4285{
4286 struct desktop_shell *shell = data;
4287 unsigned int new_index = shell->workspaces.current;
4288
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004289 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004290 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004291 if (new_index < shell->workspaces.num - 1)
4292 new_index++;
4293
4294 change_workspace(shell, new_index);
4295}
4296
4297static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004298workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004299 uint32_t key, void *data)
4300{
4301 struct desktop_shell *shell = data;
4302 unsigned int new_index;
4303
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004304 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004305 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004306 new_index = key - KEY_F1;
4307 if (new_index >= shell->workspaces.num)
4308 new_index = shell->workspaces.num - 1;
4309
4310 change_workspace(shell, new_index);
4311}
4312
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004313static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004314workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004315 uint32_t key, void *data)
4316{
4317 struct desktop_shell *shell = data;
4318 unsigned int new_index = shell->workspaces.current;
4319
4320 if (shell->locked)
4321 return;
4322
4323 if (new_index != 0)
4324 new_index--;
4325
4326 take_surface_to_workspace_by_seat(shell, seat, new_index);
4327}
4328
4329static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004330workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004331 uint32_t key, void *data)
4332{
4333 struct desktop_shell *shell = data;
4334 unsigned int new_index = shell->workspaces.current;
4335
4336 if (shell->locked)
4337 return;
4338
4339 if (new_index < shell->workspaces.num - 1)
4340 new_index++;
4341
4342 take_surface_to_workspace_by_seat(shell, seat, new_index);
4343}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004344
4345static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004346shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004347{
Tiago Vignattibe143262012-04-16 17:31:41 +03004348 struct desktop_shell *shell =
4349 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004350 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004351
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004352 if (shell->child.client)
4353 wl_client_destroy(shell->child.client);
4354
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004355 wl_list_remove(&shell->idle_listener.link);
4356 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004357 wl_list_remove(&shell->show_input_panel_listener.link);
4358 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004359
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004360 wl_array_for_each(ws, &shell->workspaces.array)
4361 workspace_destroy(*ws);
4362 wl_array_release(&shell->workspaces.array);
4363
Pekka Paalanen3c647232011-12-22 13:43:43 +02004364 free(shell->screensaver.path);
4365 free(shell);
4366}
4367
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004368static void
4369shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4370{
4371 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004372 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004373
4374 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004375 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4376 MODIFIER_CTRL | MODIFIER_ALT,
4377 terminate_binding, ec);
4378 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4379 click_to_activate_binding,
4380 shell);
4381 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4382 MODIFIER_SUPER | MODIFIER_ALT,
4383 surface_opacity_binding, NULL);
4384 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4385 MODIFIER_SUPER, zoom_axis_binding,
4386 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004387
4388 /* configurable bindings */
4389 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004390 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4391 zoom_key_binding, NULL);
4392 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4393 zoom_key_binding, NULL);
4394 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4395 shell);
4396 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4397 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004398
4399 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4400 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4401 rotate_binding, NULL);
4402
Daniel Stone325fc2d2012-05-30 16:31:58 +01004403 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4404 shell);
4405 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4406 ec);
4407 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4408 backlight_binding, ec);
4409 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4410 ec);
4411 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4412 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004413 weston_compositor_add_key_binding(ec, KEY_K, mod,
4414 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004415 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4416 workspace_up_binding, shell);
4417 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4418 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004419 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4420 workspace_move_surface_up_binding,
4421 shell);
4422 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4423 workspace_move_surface_down_binding,
4424 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004425
4426 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4427 if (shell->workspaces.num > 1) {
4428 num_workspace_bindings = shell->workspaces.num;
4429 if (num_workspace_bindings > 6)
4430 num_workspace_bindings = 6;
4431 for (i = 0; i < num_workspace_bindings; i++)
4432 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4433 workspace_f_binding,
4434 shell);
4435 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004436
4437 /* Debug bindings */
4438 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4439 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004440}
4441
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004442WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004443module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004444 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004445{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004446 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004447 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004448 struct workspace **pws;
4449 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004450 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004451
4452 shell = malloc(sizeof *shell);
4453 if (shell == NULL)
4454 return -1;
4455
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004456 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004458
4459 shell->destroy_listener.notify = shell_destroy;
4460 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004461 shell->idle_listener.notify = idle_handler;
4462 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4463 shell->wake_listener.notify = wake_handler;
4464 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004465 shell->show_input_panel_listener.notify = show_input_panels;
4466 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4467 shell->hide_input_panel_listener.notify = hide_input_panels;
4468 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004469 shell->update_input_panel_listener.notify = update_input_panels;
4470 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004471 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004472 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004473 ec->shell_interface.create_shell_surface = create_shell_surface;
4474 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004475 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004476 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004477 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004478 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004479 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004480
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004481 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004482
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004483 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4484 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004485 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4486 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004487 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004488
4489 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004490 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004491
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004492 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004493
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004494 for (i = 0; i < shell->workspaces.num; i++) {
4495 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4496 if (pws == NULL)
4497 return -1;
4498
4499 *pws = workspace_create();
4500 if (*pws == NULL)
4501 return -1;
4502 }
4503 activate_workspace(shell, 0);
4504
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004505 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004506 wl_list_init(&shell->workspaces.animation.link);
4507 shell->workspaces.animation.frame = animate_workspace_change_frame;
4508
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004509 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4510 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004511 return -1;
4512
Kristian Høgsberg75840622011-09-06 13:48:16 -04004513 if (wl_display_add_global(ec->wl_display,
4514 &desktop_shell_interface,
4515 shell, bind_desktop_shell) == NULL)
4516 return -1;
4517
Pekka Paalanen6e168112011-11-24 11:34:05 +02004518 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4519 shell, bind_screensaver) == NULL)
4520 return -1;
4521
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004522 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004523 shell, bind_input_panel) == NULL)
4524 return -1;
4525
Jonas Ådahle9d22502012-08-29 22:13:01 +02004526 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4527 shell, bind_workspace_manager) == NULL)
4528 return -1;
4529
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004530 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004531
4532 loop = wl_display_get_event_loop(ec->wl_display);
4533 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004534
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004535 shell->screensaver.timer =
4536 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4537
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004538 wl_list_for_each(seat, &ec->seat_list, link)
4539 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004540
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004541 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004542
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004543 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004544
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004545 return 0;
4546}