blob: 69345b0a736eeee4bc40a4c528faf7c0aa46410d [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øgsberg67800732013-07-04 01:12:17 -04001297end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1298
1299static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001300busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001301{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001302 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001303 struct weston_pointer *pointer = base->pointer;
1304 struct weston_surface *surface;
1305 wl_fixed_t sx, sy;
1306
1307 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1308 pointer->x, pointer->y,
1309 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001310
Kristian Høgsberg67800732013-07-04 01:12:17 -04001311 if (!grab->shsurf || grab->shsurf->surface != surface)
1312 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001313}
1314
1315static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001316busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001317{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001318}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001319
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001320static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001321busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001322 uint32_t time, uint32_t button, uint32_t state)
1323{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001324 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001325 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001326 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001327
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001328 if (shsurf && button == BTN_LEFT && state) {
1329 activate(shsurf->shell, shsurf->surface, seat);
1330 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001331 } else if (shsurf && button == BTN_RIGHT && state) {
1332 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001333 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001334 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001335}
1336
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001337static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001338 busy_cursor_grab_focus,
1339 busy_cursor_grab_motion,
1340 busy_cursor_grab_button,
1341};
1342
1343static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001344set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001345{
1346 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001347
1348 grab = malloc(sizeof *grab);
1349 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001350 return;
1351
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001352 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1353 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001354}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001355
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001356static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001357end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001358{
1359 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1360
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001361 if (grab->grab.interface == &busy_cursor_grab_interface &&
1362 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001363 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001364 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001365 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001366}
1367
Scott Moreau9521d5e2012-04-19 13:06:17 -06001368static void
1369ping_timer_destroy(struct shell_surface *shsurf)
1370{
1371 if (!shsurf || !shsurf->ping_timer)
1372 return;
1373
1374 if (shsurf->ping_timer->source)
1375 wl_event_source_remove(shsurf->ping_timer->source);
1376
1377 free(shsurf->ping_timer);
1378 shsurf->ping_timer = NULL;
1379}
1380
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001381static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001382ping_timeout_handler(void *data)
1383{
1384 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001385 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001386
Scott Moreau9521d5e2012-04-19 13:06:17 -06001387 /* Client is not responding */
1388 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001389
1390 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001391 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001392 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001393
1394 return 1;
1395}
1396
1397static void
1398ping_handler(struct weston_surface *surface, uint32_t serial)
1399{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001400 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001401 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001402 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001403
1404 if (!shsurf)
1405 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001406 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001407 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001408
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001409 if (shsurf->surface == shsurf->shell->grab_surface)
1410 return;
1411
Scott Moreauff1db4a2012-04-17 19:06:18 -06001412 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001413 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001414 if (!shsurf->ping_timer)
1415 return;
1416
1417 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001418 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1419 shsurf->ping_timer->source =
1420 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1421 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1422
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001423 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001424 }
1425}
1426
1427static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001428handle_pointer_focus(struct wl_listener *listener, void *data)
1429{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001430 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001431 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001432 struct weston_compositor *compositor;
1433 struct shell_surface *shsurf;
1434 uint32_t serial;
1435
1436 if (!surface)
1437 return;
1438
1439 compositor = surface->compositor;
1440 shsurf = get_shell_surface(surface);
1441
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001442 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001443 set_busy_cursor(shsurf, pointer);
1444 } else {
1445 serial = wl_display_next_serial(compositor->wl_display);
1446 ping_handler(surface, serial);
1447 }
1448}
1449
1450static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001451create_pointer_focus_listener(struct weston_seat *seat)
1452{
1453 struct wl_listener *listener;
1454
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001456 return;
1457
1458 listener = malloc(sizeof *listener);
1459 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001460 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001461}
1462
1463static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001464shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1465 uint32_t serial)
1466{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001467 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001468 struct weston_seat *seat;
1469 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001470
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001471 if (shsurf->ping_timer == NULL)
1472 /* Just ignore unsolicited pong. */
1473 return;
1474
Scott Moreauff1db4a2012-04-17 19:06:18 -06001475 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001476 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001477 wl_list_for_each(seat, &ec->seat_list, link) {
1478 if(seat->pointer)
1479 end_busy_cursor(shsurf, seat->pointer);
1480 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001481 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001482 }
1483}
1484
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001485static void
1486shell_surface_set_title(struct wl_client *client,
1487 struct wl_resource *resource, const char *title)
1488{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001489 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001490
1491 free(shsurf->title);
1492 shsurf->title = strdup(title);
1493}
1494
1495static void
1496shell_surface_set_class(struct wl_client *client,
1497 struct wl_resource *resource, const char *class)
1498{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001499 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001500
1501 free(shsurf->class);
1502 shsurf->class = strdup(class);
1503}
1504
Juan Zhao96879df2012-02-07 08:45:41 +08001505static struct weston_output *
1506get_default_output(struct weston_compositor *compositor)
1507{
1508 return container_of(compositor->output_list.next,
1509 struct weston_output, link);
1510}
1511
Alex Wu4539b082012-03-01 12:57:46 +08001512static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001513restore_output_mode(struct weston_output *output)
1514{
1515 if (output->current != output->origin ||
1516 (int32_t)output->scale != output->origin_scale)
1517 weston_output_switch_mode(output,
1518 output->origin,
1519 output->origin_scale);
1520}
1521
1522static void
1523restore_all_output_modes(struct weston_compositor *compositor)
1524{
1525 struct weston_output *output;
1526
1527 wl_list_for_each(output, &compositor->output_list, link)
1528 restore_output_mode(output);
1529}
1530
1531static void
Alex Wu4539b082012-03-01 12:57:46 +08001532shell_unset_fullscreen(struct shell_surface *shsurf)
1533{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001534 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001535 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001536 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1537 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001538 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001539 }
Alex Wu4539b082012-03-01 12:57:46 +08001540 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1541 shsurf->fullscreen.framerate = 0;
1542 wl_list_remove(&shsurf->fullscreen.transform.link);
1543 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001544 if (shsurf->fullscreen.black_surface)
1545 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001546 shsurf->fullscreen.black_surface = NULL;
1547 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001548 weston_surface_set_position(shsurf->surface,
1549 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001550 if (shsurf->saved_rotation_valid) {
1551 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1552 &shsurf->rotation.transform.link);
1553 shsurf->saved_rotation_valid = false;
1554 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001555
1556 ws = get_current_workspace(shsurf->shell);
1557 wl_list_remove(&shsurf->surface->layer_link);
1558 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001559}
1560
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001561static void
1562shell_unset_maximized(struct shell_surface *shsurf)
1563{
1564 struct workspace *ws;
1565 /* undo all maximized things here */
1566 shsurf->output = get_default_output(shsurf->surface->compositor);
1567 weston_surface_set_position(shsurf->surface,
1568 shsurf->saved_x,
1569 shsurf->saved_y);
1570
1571 if (shsurf->saved_rotation_valid) {
1572 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1573 &shsurf->rotation.transform.link);
1574 shsurf->saved_rotation_valid = false;
1575 }
1576
1577 ws = get_current_workspace(shsurf->shell);
1578 wl_list_remove(&shsurf->surface->layer_link);
1579 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1580}
1581
Pekka Paalanen98262232011-12-01 10:42:22 +02001582static int
1583reset_shell_surface_type(struct shell_surface *surface)
1584{
1585 switch (surface->type) {
1586 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001587 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001588 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001589 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001590 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001591 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001592 case SHELL_SURFACE_NONE:
1593 case SHELL_SURFACE_TOPLEVEL:
1594 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001596 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001597 break;
1598 }
1599
1600 surface->type = SHELL_SURFACE_NONE;
1601 return 0;
1602}
1603
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001604static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001605set_surface_type(struct shell_surface *shsurf)
1606{
1607 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001608 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001609
1610 reset_shell_surface_type(shsurf);
1611
1612 shsurf->type = shsurf->next_type;
1613 shsurf->next_type = SHELL_SURFACE_NONE;
1614
1615 switch (shsurf->type) {
1616 case SHELL_SURFACE_TOPLEVEL:
1617 break;
1618 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001619 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001620 pes->geometry.x + shsurf->transient.x,
1621 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001622 break;
1623
1624 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001625 case SHELL_SURFACE_FULLSCREEN:
1626 shsurf->saved_x = surface->geometry.x;
1627 shsurf->saved_y = surface->geometry.y;
1628 shsurf->saved_position_valid = true;
1629
1630 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1631 wl_list_remove(&shsurf->rotation.transform.link);
1632 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001633 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001634 shsurf->saved_rotation_valid = true;
1635 }
1636 break;
1637
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001638 case SHELL_SURFACE_XWAYLAND:
1639 weston_surface_set_position(surface, shsurf->transient.x,
1640 shsurf->transient.y);
1641 break;
1642
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001643 default:
1644 break;
1645 }
1646}
1647
1648static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001649set_toplevel(struct shell_surface *shsurf)
1650{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001651 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001652}
1653
1654static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655shell_surface_set_toplevel(struct wl_client *client,
1656 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001657{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001658 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001659
Tiago Vignattibc052c92012-04-19 16:18:18 +03001660 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001661}
1662
1663static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001664set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001665 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001666{
1667 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001668 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001669 shsurf->transient.x = x;
1670 shsurf->transient.y = y;
1671 shsurf->transient.flags = flags;
1672 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1673}
1674
1675static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001676shell_surface_set_transient(struct wl_client *client,
1677 struct wl_resource *resource,
1678 struct wl_resource *parent_resource,
1679 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001680{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001681 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001682 struct weston_surface *parent =
1683 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001684
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001685 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001686}
1687
Tiago Vignattibe143262012-04-16 17:31:41 +03001688static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001689shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001690{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001691 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001692}
1693
1694static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001695get_output_panel_height(struct desktop_shell *shell,
1696 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001697{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001698 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001699 int panel_height = 0;
1700
1701 if (!output)
1702 return 0;
1703
Juan Zhao4ab94682012-07-09 22:24:09 -07001704 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001705 if (surface->output == output) {
1706 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001707 break;
1708 }
1709 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001710
Juan Zhao96879df2012-02-07 08:45:41 +08001711 return panel_height;
1712}
1713
1714static void
1715shell_surface_set_maximized(struct wl_client *client,
1716 struct wl_resource *resource,
1717 struct wl_resource *output_resource )
1718{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001719 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001720 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001721 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001722 uint32_t edges = 0, panel_height = 0;
1723
1724 /* get the default output, if the client set it as NULL
1725 check whether the ouput is available */
1726 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001727 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001728 else if (es->output)
1729 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001730 else
1731 shsurf->output = get_default_output(es->compositor);
1732
Tiago Vignattibe143262012-04-16 17:31:41 +03001733 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001734 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001735 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001736
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001737 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001738 shsurf->output->width,
1739 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001740
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001741 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001742}
1743
Alex Wu21858432012-04-01 20:13:08 +08001744static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001745black_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 +08001746
Alex Wu4539b082012-03-01 12:57:46 +08001747static struct weston_surface *
1748create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001749 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001750 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001751{
1752 struct weston_surface *surface = NULL;
1753
1754 surface = weston_surface_create(ec);
1755 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001756 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001757 return NULL;
1758 }
1759
Alex Wu21858432012-04-01 20:13:08 +08001760 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001761 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001762 weston_surface_configure(surface, x, y, w, h);
1763 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001764 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001765 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001766 pixman_region32_fini(&surface->input);
1767 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001768
Alex Wu4539b082012-03-01 12:57:46 +08001769 return surface;
1770}
1771
1772/* Create black surface and append it to the associated fullscreen surface.
1773 * Handle size dismatch and positioning according to the method. */
1774static void
1775shell_configure_fullscreen(struct shell_surface *shsurf)
1776{
1777 struct weston_output *output = shsurf->fullscreen_output;
1778 struct weston_surface *surface = shsurf->surface;
1779 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001780 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001781 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001782
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001783 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1784 restore_output_mode(output);
1785
Alex Wu4539b082012-03-01 12:57:46 +08001786 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001787 shsurf->fullscreen.black_surface =
1788 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001789 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001790 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001791 output->width,
1792 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001793
1794 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1795 wl_list_insert(&surface->layer_link,
1796 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001797 shsurf->fullscreen.black_surface->output = output;
1798
Giulio Camuffob8366642013-04-25 13:57:46 +03001799 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1800 &surf_width, &surf_height);
1801
Alex Wu4539b082012-03-01 12:57:46 +08001802 switch (shsurf->fullscreen.type) {
1803 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001804 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001805 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001806 break;
1807 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001808 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001809 if (output->width == surf_width &&
1810 output->height == surf_height) {
1811 weston_surface_set_position(surface, output->x - surf_x,
1812 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001813 break;
1814 }
1815
Alex Wu4539b082012-03-01 12:57:46 +08001816 matrix = &shsurf->fullscreen.transform.matrix;
1817 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001818
Scott Moreau1bad5db2012-08-18 01:04:05 -06001819 output_aspect = (float) output->width /
1820 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001821 surface_aspect = (float) surface->geometry.width /
1822 (float) surface->geometry.height;
1823 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001824 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001825 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001826 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001827 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001828 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001829
Alex Wu4539b082012-03-01 12:57:46 +08001830 weston_matrix_scale(matrix, scale, scale, 1);
1831 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001832 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001833 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001834 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1835 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001836 weston_surface_set_position(surface, x, y);
1837
Alex Wu4539b082012-03-01 12:57:46 +08001838 break;
1839 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001840 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001841 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001842 surf_width * surface->buffer_scale,
1843 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001844 shsurf->fullscreen.framerate};
1845
Alexander Larsson355748e2013-05-28 16:23:38 +02001846 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001847 weston_surface_set_position(surface,
1848 output->x - surf_x,
1849 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001850 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001851 output->x - surf_x,
1852 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001853 output->width,
1854 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001855 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001856 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001857 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001858 center_on_output(surface, output);
1859 }
Alex Wubd3354b2012-04-17 17:20:49 +08001860 }
Alex Wu4539b082012-03-01 12:57:46 +08001861 break;
1862 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001863 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001864 break;
1865 default:
1866 break;
1867 }
1868}
1869
1870/* make the fullscreen and black surface at the top */
1871static void
1872shell_stack_fullscreen(struct shell_surface *shsurf)
1873{
Alex Wubd3354b2012-04-17 17:20:49 +08001874 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001875 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001876 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001877
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001878 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001879 wl_list_insert(&shell->fullscreen_layer.surface_list,
1880 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001881 weston_surface_damage(surface);
1882
1883 if (!shsurf->fullscreen.black_surface)
1884 shsurf->fullscreen.black_surface =
1885 create_black_surface(surface->compositor,
1886 surface,
1887 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001888 output->width,
1889 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001890
1891 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001892 wl_list_insert(&surface->layer_link,
1893 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001894 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001895}
1896
1897static void
1898shell_map_fullscreen(struct shell_surface *shsurf)
1899{
Alex Wu4539b082012-03-01 12:57:46 +08001900 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001901 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001902}
1903
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001904static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001905set_fullscreen(struct shell_surface *shsurf,
1906 uint32_t method,
1907 uint32_t framerate,
1908 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001909{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001910 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001911
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001912 if (output)
1913 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001914 else if (es->output)
1915 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001916 else
1917 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001918
Alex Wu4539b082012-03-01 12:57:46 +08001919 shsurf->fullscreen_output = shsurf->output;
1920 shsurf->fullscreen.type = method;
1921 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001922 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001923
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001924 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001925 shsurf->output->width,
1926 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001927}
1928
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001929static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001930shell_surface_set_fullscreen(struct wl_client *client,
1931 struct wl_resource *resource,
1932 uint32_t method,
1933 uint32_t framerate,
1934 struct wl_resource *output_resource)
1935{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001936 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001937 struct weston_output *output;
1938
1939 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001940 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001941 else
1942 output = NULL;
1943
1944 set_fullscreen(shsurf, method, framerate, output);
1945}
1946
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001947static void
1948set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1949{
1950 /* XXX: using the same fields for transient type */
1951 shsurf->transient.x = x;
1952 shsurf->transient.y = y;
1953 shsurf->transient.flags = flags;
1954 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1955}
1956
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001957static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001958
1959static void
1960destroy_shell_seat(struct wl_listener *listener, void *data)
1961{
1962 struct shell_seat *shseat =
1963 container_of(listener,
1964 struct shell_seat, seat_destroy_listener);
1965 struct shell_surface *shsurf, *prev = NULL;
1966
1967 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001968 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001969 shseat->popup_grab.client = NULL;
1970
1971 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1972 shsurf->popup.shseat = NULL;
1973 if (prev) {
1974 wl_list_init(&prev->popup.grab_link);
1975 }
1976 prev = shsurf;
1977 }
1978 wl_list_init(&prev->popup.grab_link);
1979 }
1980
1981 wl_list_remove(&shseat->seat_destroy_listener.link);
1982 free(shseat);
1983}
1984
1985static struct shell_seat *
1986create_shell_seat(struct weston_seat *seat)
1987{
1988 struct shell_seat *shseat;
1989
1990 shseat = calloc(1, sizeof *shseat);
1991 if (!shseat) {
1992 weston_log("no memory to allocate shell seat\n");
1993 return NULL;
1994 }
1995
1996 shseat->seat = seat;
1997 wl_list_init(&shseat->popup_grab.surfaces_list);
1998
1999 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2000 wl_signal_add(&seat->destroy_signal,
2001 &shseat->seat_destroy_listener);
2002
2003 return shseat;
2004}
2005
2006static struct shell_seat *
2007get_shell_seat(struct weston_seat *seat)
2008{
2009 struct wl_listener *listener;
2010
2011 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2012 if (listener == NULL)
2013 return create_shell_seat(seat);
2014
2015 return container_of(listener,
2016 struct shell_seat, seat_destroy_listener);
2017}
2018
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002019static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002020popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002021{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002022 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002023 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002024 struct shell_seat *shseat =
2025 container_of(grab, struct shell_seat, popup_grab.grab);
2026 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002027 wl_fixed_t sx, sy;
2028
2029 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2030 pointer->x, pointer->y,
2031 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002032
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002033 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002034 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002035 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002036 weston_pointer_set_focus(pointer, NULL,
2037 wl_fixed_from_int(0),
2038 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002039 }
2040}
2041
2042static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002043popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002044{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002045 struct weston_pointer *pointer = grab->pointer;
2046 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002047
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002048 if (pointer->focus_resource) {
2049 weston_surface_from_global_fixed(pointer->focus,
2050 pointer->x, pointer->y,
2051 &sx, &sy);
2052 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2053 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054}
2055
2056static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002057popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002058 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002059{
2060 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002061 struct shell_seat *shseat =
2062 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002063 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002064 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002065 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002066
Daniel Stone37816df2012-05-16 18:45:18 +01002067 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002069 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002070 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002071 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002072 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002073 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002074 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002075 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002076 }
2077
Daniel Stone4dbadb12012-05-30 16:31:51 +01002078 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002079 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002080}
2081
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002082static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002083 popup_grab_focus,
2084 popup_grab_motion,
2085 popup_grab_button,
2086};
2087
2088static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002089popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002090{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002091 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002092 struct shell_seat *shseat =
2093 container_of(grab, struct shell_seat, popup_grab.grab);
2094 struct shell_surface *shsurf;
2095 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002096
2097 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002098 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002099 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002100 shseat->popup_grab.grab.interface = NULL;
2101 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002102 /* Send the popup_done event to all the popups open */
2103 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002104 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002105 shsurf->popup.shseat = NULL;
2106 if (prev) {
2107 wl_list_init(&prev->popup.grab_link);
2108 }
2109 prev = shsurf;
2110 }
2111 wl_list_init(&prev->popup.grab_link);
2112 wl_list_init(&shseat->popup_grab.surfaces_list);
2113 }
2114}
2115
2116static void
2117add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2118{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002119 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002120
2121 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002122 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002123 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002124 /* We must make sure here that this popup was opened after
2125 * a mouse press, and not just by moving around with other
2126 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002127 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002128 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002129
Rob Bradforddfe31052013-06-26 19:49:11 +01002130 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002131 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002132 } else {
2133 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002134 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002135}
2136
2137static void
2138remove_popup_grab(struct shell_surface *shsurf)
2139{
2140 struct shell_seat *shseat = shsurf->popup.shseat;
2141
2142 wl_list_remove(&shsurf->popup.grab_link);
2143 wl_list_init(&shsurf->popup.grab_link);
2144 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002145 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002146 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002147 }
2148}
2149
2150static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002151shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002152{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002153 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002154 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002155 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002156
2157 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002158
Pekka Paalanen483243f2013-03-08 14:56:50 +02002159 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002160 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2161 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002162
Kristian Høgsberge3148752013-05-06 23:19:49 -04002163 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002164 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002165 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002166 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002167 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002168 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002169}
2170
2171static void
2172shell_surface_set_popup(struct wl_client *client,
2173 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002174 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002175 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176 struct wl_resource *parent_resource,
2177 int32_t x, int32_t y, uint32_t flags)
2178{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002179 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002180
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002181 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002182 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002183 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002184 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002185 shsurf->popup.x = x;
2186 shsurf->popup.y = y;
2187}
2188
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002189static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002190 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002191 shell_surface_move,
2192 shell_surface_resize,
2193 shell_surface_set_toplevel,
2194 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002195 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002196 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002197 shell_surface_set_maximized,
2198 shell_surface_set_title,
2199 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002200};
2201
2202static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002203destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002204{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002205 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2206
Giulio Camuffo5085a752013-03-25 21:42:45 +01002207 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2208 remove_popup_grab(shsurf);
2209 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002210
Alex Wubd3354b2012-04-17 17:20:49 +08002211 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002212 shell_surface_is_top_fullscreen(shsurf))
2213 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002214
Alex Wuaa08e2d2012-03-05 11:01:40 +08002215 if (shsurf->fullscreen.black_surface)
2216 weston_surface_destroy(shsurf->fullscreen.black_surface);
2217
Alex Wubd3354b2012-04-17 17:20:49 +08002218 /* As destroy_resource() use wl_list_for_each_safe(),
2219 * we can always remove the listener.
2220 */
2221 wl_list_remove(&shsurf->surface_destroy_listener.link);
2222 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002223 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002224 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002225
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002226 wl_list_remove(&shsurf->link);
2227 free(shsurf);
2228}
2229
2230static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002231shell_destroy_shell_surface(struct wl_resource *resource)
2232{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002233 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002234
2235 destroy_shell_surface(shsurf);
2236}
2237
2238static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002239shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002240{
2241 struct shell_surface *shsurf = container_of(listener,
2242 struct shell_surface,
2243 surface_destroy_listener);
2244
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002245 if (shsurf->resource)
2246 wl_resource_destroy(shsurf->resource);
2247 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002248 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002249}
2250
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002251static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002252shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002253
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002254static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002255get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002256{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002257 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002258 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002259 else
2260 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002261}
2262
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002263static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002264create_shell_surface(void *shell, struct weston_surface *surface,
2265 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002266{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002267 struct shell_surface *shsurf;
2268
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002269 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002270 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002271 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002272 }
2273
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002274 shsurf = calloc(1, sizeof *shsurf);
2275 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002276 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002277 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002278 }
2279
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002280 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002281 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002282
Tiago Vignattibc052c92012-04-19 16:18:18 +03002283 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002284 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002285 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002286 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002287 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002288 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2289 shsurf->fullscreen.framerate = 0;
2290 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002291 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002292 wl_list_init(&shsurf->fullscreen.transform.link);
2293
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002294 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002295 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002296 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002297 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002298
2299 /* init link so its safe to always remove it in destroy_shell_surface */
2300 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002301 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002302
Pekka Paalanen460099f2012-01-20 16:48:25 +02002303 /* empty when not in use */
2304 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002305 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002306
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002307 wl_list_init(&shsurf->workspace_transform.link);
2308
Pekka Paalanen98262232011-12-01 10:42:22 +02002309 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002310 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002311
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002312 shsurf->client = client;
2313
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002314 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002315}
2316
2317static void
2318shell_get_shell_surface(struct wl_client *client,
2319 struct wl_resource *resource,
2320 uint32_t id,
2321 struct wl_resource *surface_resource)
2322{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002323 struct weston_surface *surface =
2324 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002325 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002326 struct shell_surface *shsurf;
2327
2328 if (get_shell_surface(surface)) {
2329 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002330 WL_DISPLAY_ERROR_INVALID_OBJECT,
2331 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002332 return;
2333 }
2334
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002335 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002336 if (!shsurf) {
2337 wl_resource_post_error(surface_resource,
2338 WL_DISPLAY_ERROR_INVALID_OBJECT,
2339 "surface->configure already set");
2340 return;
2341 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002342
Jason Ekstranda85118c2013-06-27 20:17:02 -05002343 shsurf->resource =
2344 wl_resource_create(client,
2345 &wl_shell_surface_interface, 1, id);
2346 wl_resource_set_implementation(shsurf->resource,
2347 &shell_surface_implementation,
2348 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002349}
2350
2351static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002352 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002353};
2354
Kristian Høgsberg07937562011-04-12 17:25:42 -04002355static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002356shell_fade(struct desktop_shell *shell, enum fade_type type);
2357
2358static int
2359screensaver_timeout(void *data)
2360{
2361 struct desktop_shell *shell = data;
2362
2363 shell_fade(shell, FADE_OUT);
2364
2365 return 1;
2366}
2367
2368static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002369handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002370{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002371 struct desktop_shell *shell =
2372 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002373
Pekka Paalanen18027e52011-12-02 16:31:49 +02002374 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002375
2376 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002377 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002378}
2379
2380static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002381launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002382{
2383 if (shell->screensaver.binding)
2384 return;
2385
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002386 if (!shell->screensaver.path) {
2387 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002388 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002389 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002390
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002391 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002392 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002393 return;
2394 }
2395
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002396 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002397 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002398 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002399 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002400}
2401
2402static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002403terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002404{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002405 if (shell->screensaver.process.pid == 0)
2406 return;
2407
2408 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002409}
2410
2411static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002412configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002413{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002414 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002415
Giulio Camuffo184df502013-02-21 11:29:21 +01002416 if (width == 0)
2417 return;
2418
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002419 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2420 if (s->output == es->output && s != es) {
2421 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002422 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002423 }
2424 }
2425
Giulio Camuffo184df502013-02-21 11:29:21 +01002426 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002427
2428 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002429 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002430 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002431 }
2432}
2433
2434static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002435background_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 -04002436{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002437 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002438
Giulio Camuffo184df502013-02-21 11:29:21 +01002439 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002440}
2441
2442static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002443desktop_shell_set_background(struct wl_client *client,
2444 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002445 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002446 struct wl_resource *surface_resource)
2447{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002448 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002449 struct weston_surface *surface =
2450 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002451
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002452 if (surface->configure) {
2453 wl_resource_post_error(surface_resource,
2454 WL_DISPLAY_ERROR_INVALID_OBJECT,
2455 "surface role already assigned");
2456 return;
2457 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002458
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002459 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002460 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002461 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002462 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002463 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002464 surface->output->width,
2465 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002466}
2467
2468static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002469panel_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 -04002470{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002471 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002472
Giulio Camuffo184df502013-02-21 11:29:21 +01002473 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002474}
2475
2476static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002477desktop_shell_set_panel(struct wl_client *client,
2478 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002479 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002480 struct wl_resource *surface_resource)
2481{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002482 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002483 struct weston_surface *surface =
2484 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002485
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002486 if (surface->configure) {
2487 wl_resource_post_error(surface_resource,
2488 WL_DISPLAY_ERROR_INVALID_OBJECT,
2489 "surface role already assigned");
2490 return;
2491 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002492
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002493 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002494 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002495 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002496 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002497 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002498 surface->output->width,
2499 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002500}
2501
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002502static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002503lock_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 -04002504{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002505 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002506
Giulio Camuffo184df502013-02-21 11:29:21 +01002507 if (width == 0)
2508 return;
2509
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002510 surface->geometry.width = width;
2511 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002512 center_on_output(surface, get_default_output(shell->compositor));
2513
2514 if (!weston_surface_is_mapped(surface)) {
2515 wl_list_insert(&shell->lock_layer.surface_list,
2516 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002517 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002518 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002519 }
2520}
2521
2522static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002523handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002524{
Tiago Vignattibe143262012-04-16 17:31:41 +03002525 struct desktop_shell *shell =
2526 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002527
Martin Minarik6d118362012-06-07 18:01:59 +02002528 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002529 shell->lock_surface = NULL;
2530}
2531
2532static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002533desktop_shell_set_lock_surface(struct wl_client *client,
2534 struct wl_resource *resource,
2535 struct wl_resource *surface_resource)
2536{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002537 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002538 struct weston_surface *surface =
2539 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002540
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002541 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002542
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002543 if (!shell->locked)
2544 return;
2545
Pekka Paalanen98262232011-12-01 10:42:22 +02002546 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002547
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002548 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002549 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002550 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002551
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002552 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002553 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002554}
2555
2556static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002557resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002558{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002559 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002560
Pekka Paalanen77346a62011-11-30 16:26:35 +02002561 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002562
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002563 wl_list_remove(&shell->lock_layer.link);
2564 wl_list_insert(&shell->compositor->cursor_layer.link,
2565 &shell->fullscreen_layer.link);
2566 wl_list_insert(&shell->fullscreen_layer.link,
2567 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002568 if (shell->showing_input_panels) {
2569 wl_list_insert(&shell->panel_layer.link,
2570 &shell->input_panel_layer.link);
2571 wl_list_insert(&shell->input_panel_layer.link,
2572 &ws->layer.link);
2573 } else {
2574 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2575 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002576
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002577 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002578
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002579 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002580 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002581 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002582}
2583
2584static void
2585desktop_shell_unlock(struct wl_client *client,
2586 struct wl_resource *resource)
2587{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002588 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002589
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002590 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002591
2592 if (shell->locked)
2593 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002594}
2595
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002596static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002597desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002598 struct wl_resource *resource,
2599 struct wl_resource *surface_resource)
2600{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002601 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002602
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002603 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002604}
2605
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002606static void
2607desktop_shell_desktop_ready(struct wl_client *client,
2608 struct wl_resource *resource)
2609{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002610 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002611
2612 shell_fade_startup(shell);
2613}
2614
Kristian Høgsberg75840622011-09-06 13:48:16 -04002615static const struct desktop_shell_interface desktop_shell_implementation = {
2616 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002617 desktop_shell_set_panel,
2618 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002619 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002620 desktop_shell_set_grab_surface,
2621 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002622};
2623
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002624static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002625get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002626{
2627 struct shell_surface *shsurf;
2628
2629 shsurf = get_shell_surface(surface);
2630 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002631 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002632 return shsurf->type;
2633}
2634
Kristian Høgsberg75840622011-09-06 13:48:16 -04002635static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002636move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002637{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002638 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002639 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002640 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002641 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002642
Pekka Paalanen01388e22013-04-25 13:57:44 +03002643 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002644 if (surface == NULL)
2645 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002646
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002647 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002648 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2649 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002650 return;
2651
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002652 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002653}
2654
2655static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002656resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002657{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002658 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002659 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002660 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002661 uint32_t edges = 0;
2662 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002663 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002664
Pekka Paalanen01388e22013-04-25 13:57:44 +03002665 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002666 if (surface == NULL)
2667 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002668
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002669 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002670 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2671 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002672 return;
2673
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002674 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002675 wl_fixed_to_int(seat->pointer->grab_x),
2676 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002677 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002678
Pekka Paalanen60921e52012-01-25 15:55:43 +02002679 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002680 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002681 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002682 edges |= 0;
2683 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002684 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002685
Pekka Paalanen60921e52012-01-25 15:55:43 +02002686 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002687 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002688 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002689 edges |= 0;
2690 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002691 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002692
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002693 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002694}
2695
2696static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002697surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002698 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002699{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002700 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002701 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002702 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002703 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002704 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002705
Pekka Paalanen01388e22013-04-25 13:57:44 +03002706 /* XXX: broken for windows containing sub-surfaces */
2707 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002708 if (surface == NULL)
2709 return;
2710
2711 shsurf = get_shell_surface(surface);
2712 if (!shsurf)
2713 return;
2714
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002715 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002716
Scott Moreau02709af2012-05-22 01:54:10 -06002717 if (surface->alpha > 1.0)
2718 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002719 if (surface->alpha < step)
2720 surface->alpha = step;
2721
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002722 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002723 weston_surface_damage(surface);
2724}
2725
2726static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002727do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002728 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002729{
Daniel Stone37816df2012-05-16 18:45:18 +01002730 struct weston_seat *ws = (struct weston_seat *) seat;
2731 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002732 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002733 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002734
2735 wl_list_for_each(output, &compositor->output_list, link) {
2736 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002737 wl_fixed_to_double(seat->pointer->x),
2738 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002739 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002740 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002741 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002742 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002743 increment = -output->zoom.increment;
2744 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002745 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002746 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002747 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002748 else
2749 increment = 0;
2750
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002751 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002752
Scott Moreaue6603982012-06-11 13:07:51 -06002753 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002754 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002755 else if (output->zoom.level > output->zoom.max_level)
2756 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002757 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002758 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002759 output->disable_planes++;
2760 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002761
Scott Moreaue6603982012-06-11 13:07:51 -06002762 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002763
Scott Moreau8dacaab2012-06-17 18:10:58 -06002764 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002765 }
2766 }
2767}
2768
Scott Moreauccbf29d2012-02-22 14:21:41 -07002769static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002770zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002771 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002772{
2773 do_zoom(seat, time, 0, axis, value);
2774}
2775
2776static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002777zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002778 void *data)
2779{
2780 do_zoom(seat, time, key, 0, 0);
2781}
2782
2783static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002784terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002785 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002786{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002787 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002788
Daniel Stone325fc2d2012-05-30 16:31:58 +01002789 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002790}
2791
2792static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002793rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002794{
2795 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002796 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002797 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002798 struct shell_surface *shsurf = rotate->base.shsurf;
2799 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002800 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002801
2802 if (!shsurf)
2803 return;
2804
2805 surface = shsurf->surface;
2806
2807 cx = 0.5f * surface->geometry.width;
2808 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002809
Daniel Stone37816df2012-05-16 18:45:18 +01002810 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2811 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002812 r = sqrtf(dx * dx + dy * dy);
2813
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002814 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002815 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002816
2817 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002818 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002819 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002820
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002821 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002822 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002823
2824 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002825 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002826 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002827 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002828 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002829
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002830 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002831 &shsurf->surface->geometry.transformation_list,
2832 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002833 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002834 wl_list_init(&shsurf->rotation.transform.link);
2835 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002836 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002837 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002838
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002839 /* We need to adjust the position of the surface
2840 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002841 cposx = surface->geometry.x + cx;
2842 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002843 dposx = rotate->center.x - cposx;
2844 dposy = rotate->center.y - cposy;
2845 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002846 weston_surface_set_position(surface,
2847 surface->geometry.x + dposx,
2848 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002849 }
2850
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002851 /* Repaint implies weston_surface_update_transform(), which
2852 * lazily applies the damage due to rotation update.
2853 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002854 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002855}
2856
2857static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002858rotate_grab_button(struct weston_pointer_grab *grab,
2859 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002860{
2861 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002862 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002863 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002864 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002865 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002866
Daniel Stone4dbadb12012-05-30 16:31:51 +01002867 if (pointer->button_count == 0 &&
2868 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002869 if (shsurf)
2870 weston_matrix_multiply(&shsurf->rotation.rotation,
2871 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002872 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002873 free(rotate);
2874 }
2875}
2876
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002877static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002878 noop_grab_focus,
2879 rotate_grab_motion,
2880 rotate_grab_button,
2881};
2882
2883static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002884surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002885{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002886 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002887 float dx, dy;
2888 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002889
Pekka Paalanen460099f2012-01-20 16:48:25 +02002890 rotate = malloc(sizeof *rotate);
2891 if (!rotate)
2892 return;
2893
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002894 weston_surface_to_global_float(surface->surface,
Louis-Francis Ratté-Boulianne9a4f10f2013-07-03 15:58:22 +02002895 surface->surface->geometry.width * 0.5f,
2896 surface->surface->geometry.height * 0.5f,
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002897 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002898
Daniel Stone37816df2012-05-16 18:45:18 +01002899 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2900 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002901 r = sqrtf(dx * dx + dy * dy);
2902 if (r > 20.0f) {
2903 struct weston_matrix inverse;
2904
2905 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002906 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002907 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002908
2909 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002910 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002911 } else {
2912 weston_matrix_init(&surface->rotation.rotation);
2913 weston_matrix_init(&rotate->rotation);
2914 }
2915
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002916 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2917 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002918}
2919
2920static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002921rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002922 void *data)
2923{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002924 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002925 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002926 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002927 struct shell_surface *surface;
2928
Pekka Paalanen01388e22013-04-25 13:57:44 +03002929 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002930 if (base_surface == NULL)
2931 return;
2932
2933 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002934 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2935 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002936 return;
2937
2938 surface_rotate(surface, seat);
2939}
2940
2941static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002942lower_fullscreen_layer(struct desktop_shell *shell)
2943{
2944 struct workspace *ws;
2945 struct weston_surface *surface, *prev;
2946
2947 ws = get_current_workspace(shell);
2948 wl_list_for_each_reverse_safe(surface, prev,
2949 &shell->fullscreen_layer.surface_list,
2950 layer_link)
2951 weston_surface_restack(surface, &ws->layer.surface_list);
2952}
2953
2954static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002955activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002956 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002957{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002958 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002959 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002960 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002961
Pekka Paalanen01388e22013-04-25 13:57:44 +03002962 main_surface = weston_surface_get_main_surface(es);
2963
Daniel Stone37816df2012-05-16 18:45:18 +01002964 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002965
Jonas Ådahl8538b222012-08-29 22:13:03 +02002966 state = ensure_focus_state(shell, seat);
2967 if (state == NULL)
2968 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002969
2970 state->keyboard_focus = es;
2971 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002972 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002973
Pekka Paalanen01388e22013-04-25 13:57:44 +03002974 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002975 case SHELL_SURFACE_FULLSCREEN:
2976 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002977 shell_stack_fullscreen(get_shell_surface(main_surface));
2978 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002979 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002980 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002981 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002982 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002983 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002984 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002985 }
2986}
2987
Alex Wu21858432012-04-01 20:13:08 +08002988/* no-op func for checking black surface */
2989static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002990black_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 +08002991{
2992}
2993
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002994static bool
Alex Wu21858432012-04-01 20:13:08 +08002995is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2996{
2997 if (es->configure == black_surface_configure) {
2998 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002999 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003000 return true;
3001 }
3002 return false;
3003}
3004
Kristian Høgsberg75840622011-09-06 13:48:16 -04003005static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003006click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003007 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003008{
Daniel Stone37816df2012-05-16 18:45:18 +01003009 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003010 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003011 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003012 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003013
Daniel Stone37816df2012-05-16 18:45:18 +01003014 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003015 if (!focus)
3016 return;
3017
Pekka Paalanen01388e22013-04-25 13:57:44 +03003018 if (is_black_surface(focus, &main_surface))
3019 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003020
Pekka Paalanen01388e22013-04-25 13:57:44 +03003021 main_surface = weston_surface_get_main_surface(focus);
3022 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003023 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003024
Daniel Stone325fc2d2012-05-30 16:31:58 +01003025 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003026 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003027}
3028
3029static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003030lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003031{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003032 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003033
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003034 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003035 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003036 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003037 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003038
3039 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003040
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003041 /* Hide all surfaces by removing the fullscreen, panel and
3042 * toplevel layers. This way nothing else can show or receive
3043 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003044
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003045 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003046 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003047 if (shell->showing_input_panels)
3048 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003049 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003050 wl_list_insert(&shell->compositor->cursor_layer.link,
3051 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003052
Pekka Paalanen77346a62011-11-30 16:26:35 +02003053 launch_screensaver(shell);
3054
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003055 /* TODO: disable bindings that should not work while locked. */
3056
3057 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003058}
3059
3060static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003061unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003062{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003063 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003064 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003065 return;
3066 }
3067
3068 /* If desktop-shell client has gone away, unlock immediately. */
3069 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003070 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003071 return;
3072 }
3073
3074 if (shell->prepare_event_sent)
3075 return;
3076
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003077 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003078 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003079}
3080
3081static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003082shell_fade_done(struct weston_surface_animation *animation, void *data)
3083{
3084 struct desktop_shell *shell = data;
3085
3086 shell->fade.animation = NULL;
3087
3088 switch (shell->fade.type) {
3089 case FADE_IN:
3090 weston_surface_destroy(shell->fade.surface);
3091 shell->fade.surface = NULL;
3092 break;
3093 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003094 lock(shell);
3095 break;
3096 }
3097}
3098
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003099static struct weston_surface *
3100shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003101{
3102 struct weston_compositor *compositor = shell->compositor;
3103 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003104
3105 surface = weston_surface_create(compositor);
3106 if (!surface)
3107 return NULL;
3108
3109 weston_surface_configure(surface, 0, 0, 8192, 8192);
3110 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3111 wl_list_insert(&compositor->fade_layer.surface_list,
3112 &surface->layer_link);
3113 pixman_region32_init(&surface->input);
3114
3115 return surface;
3116}
3117
3118static void
3119shell_fade(struct desktop_shell *shell, enum fade_type type)
3120{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003121 float tint;
3122
3123 switch (type) {
3124 case FADE_IN:
3125 tint = 0.0;
3126 break;
3127 case FADE_OUT:
3128 tint = 1.0;
3129 break;
3130 default:
3131 weston_log("shell: invalid fade type\n");
3132 return;
3133 }
3134
3135 shell->fade.type = type;
3136
3137 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003138 shell->fade.surface = shell_fade_create_surface(shell);
3139 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003140 return;
3141
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003142 shell->fade.surface->alpha = 1.0 - tint;
3143 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003144 }
3145
3146 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003147 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003148 else
3149 shell->fade.animation =
3150 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003151 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003152 shell_fade_done, shell);
3153}
3154
3155static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003156do_shell_fade_startup(void *data)
3157{
3158 struct desktop_shell *shell = data;
3159
3160 shell_fade(shell, FADE_IN);
3161}
3162
3163static void
3164shell_fade_startup(struct desktop_shell *shell)
3165{
3166 struct wl_event_loop *loop;
3167
3168 if (!shell->fade.startup_timer)
3169 return;
3170
3171 wl_event_source_remove(shell->fade.startup_timer);
3172 shell->fade.startup_timer = NULL;
3173
3174 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3175 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3176}
3177
3178static int
3179fade_startup_timeout(void *data)
3180{
3181 struct desktop_shell *shell = data;
3182
3183 shell_fade_startup(shell);
3184 return 0;
3185}
3186
3187static void
3188shell_fade_init(struct desktop_shell *shell)
3189{
3190 /* Make compositor output all black, and wait for the desktop-shell
3191 * client to signal it is ready, then fade in. The timer triggers a
3192 * fade-in, in case the desktop-shell client takes too long.
3193 */
3194
3195 struct wl_event_loop *loop;
3196
3197 if (shell->fade.surface != NULL) {
3198 weston_log("%s: warning: fade surface already exists\n",
3199 __func__);
3200 return;
3201 }
3202
3203 shell->fade.surface = shell_fade_create_surface(shell);
3204 if (!shell->fade.surface)
3205 return;
3206
3207 weston_surface_update_transform(shell->fade.surface);
3208 weston_surface_damage(shell->fade.surface);
3209
3210 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3211 shell->fade.startup_timer =
3212 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3213 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3214}
3215
3216static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003217idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003218{
3219 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003220 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003221
3222 shell_fade(shell, FADE_OUT);
3223 /* lock() is called from shell_fade_done() */
3224}
3225
3226static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003227wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003228{
3229 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003230 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003231
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003232 unlock(shell);
3233}
3234
3235static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003236show_input_panels(struct wl_listener *listener, void *data)
3237{
3238 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003239 container_of(listener, struct desktop_shell,
3240 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003241 struct input_panel_surface *surface, *next;
3242 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003243
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003244 shell->text_input.surface = (struct weston_surface*)data;
3245
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003246 if (shell->showing_input_panels)
3247 return;
3248
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003249 shell->showing_input_panels = true;
3250
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003251 if (!shell->locked)
3252 wl_list_insert(&shell->panel_layer.link,
3253 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003254
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003255 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003256 &shell->input_panel.surfaces, link) {
3257 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003258 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003259 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003260 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003261 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003262 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003263 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003264 weston_surface_damage(ws);
3265 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003266 }
3267}
3268
3269static void
3270hide_input_panels(struct wl_listener *listener, void *data)
3271{
3272 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003273 container_of(listener, struct desktop_shell,
3274 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003275 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003276
Jan Arne Petersen61381972013-01-31 15:52:21 +01003277 if (!shell->showing_input_panels)
3278 return;
3279
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003280 shell->showing_input_panels = false;
3281
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003282 if (!shell->locked)
3283 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003284
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003285 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003286 &shell->input_panel_layer.surface_list, layer_link)
3287 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003288}
3289
3290static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003291update_input_panels(struct wl_listener *listener, void *data)
3292{
3293 struct desktop_shell *shell =
3294 container_of(listener, struct desktop_shell,
3295 update_input_panel_listener);
3296
3297 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3298}
3299
3300static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003301center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003302{
Giulio Camuffob8366642013-04-25 13:57:46 +03003303 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003304 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003305
Giulio Camuffob8366642013-04-25 13:57:46 +03003306 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3307
3308 x = output->x + (output->width - width) / 2 - surf_x / 2;
3309 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003310
3311 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003312}
3313
3314static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003315weston_surface_set_initial_position (struct weston_surface *surface,
3316 struct desktop_shell *shell)
3317{
3318 struct weston_compositor *compositor = shell->compositor;
3319 int ix = 0, iy = 0;
3320 int range_x, range_y;
3321 int dx, dy, x, y, panel_height;
3322 struct weston_output *output, *target_output = NULL;
3323 struct weston_seat *seat;
3324
3325 /* As a heuristic place the new window on the same output as the
3326 * pointer. Falling back to the output containing 0, 0.
3327 *
3328 * TODO: Do something clever for touch too?
3329 */
3330 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003331 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003332 ix = wl_fixed_to_int(seat->pointer->x);
3333 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003334 break;
3335 }
3336 }
3337
3338 wl_list_for_each(output, &compositor->output_list, link) {
3339 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3340 target_output = output;
3341 break;
3342 }
3343 }
3344
3345 if (!target_output) {
3346 weston_surface_set_position(surface, 10 + random() % 400,
3347 10 + random() % 400);
3348 return;
3349 }
3350
3351 /* Valid range within output where the surface will still be onscreen.
3352 * If this is negative it means that the surface is bigger than
3353 * output.
3354 */
3355 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003356 range_x = target_output->width - surface->geometry.width;
3357 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003358 surface->geometry.height;
3359
Rob Bradford4cb88c72012-08-13 15:18:44 +01003360 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003361 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003362 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003363 dx = 0;
3364
3365 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003366 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003367 else
3368 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003369
3370 x = target_output->x + dx;
3371 y = target_output->y + dy;
3372
3373 weston_surface_set_position (surface, x, y);
3374}
3375
3376static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003377map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003378 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003379{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003380 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003381 struct shell_surface *shsurf = get_shell_surface(surface);
3382 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003383 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003384 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003385 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003386 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003387 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003388
Pekka Paalanen60921e52012-01-25 15:55:43 +02003389 surface->geometry.width = width;
3390 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003391 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003392
3393 /* initial positioning, see also configure() */
3394 switch (surface_type) {
3395 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003396 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003397 break;
Alex Wu4539b082012-03-01 12:57:46 +08003398 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003399 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003400 shell_map_fullscreen(shsurf);
3401 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003402 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003403 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003404 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003405 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3406 NULL, NULL);
3407 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3408 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003409 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003410 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003411 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003412 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003413 case SHELL_SURFACE_NONE:
3414 weston_surface_set_position(surface,
3415 surface->geometry.x + sx,
3416 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003417 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003418 default:
3419 ;
3420 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003421
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003422 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003423 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003424 case SHELL_SURFACE_POPUP:
3425 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003426 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003427 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3428 break;
Alex Wu4539b082012-03-01 12:57:46 +08003429 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003430 case SHELL_SURFACE_NONE:
3431 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003432 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003433 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003434 ws = get_current_workspace(shell);
3435 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003436 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003437 }
3438
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003439 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003440 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003441 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3442 surface->output = shsurf->output;
3443 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003444
Juan Zhao7bb92f02011-12-15 11:31:51 -05003445 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003446 /* XXX: xwayland's using the same fields for transient type */
3447 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003448 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003449 if (shsurf->transient.flags ==
3450 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3451 break;
3452 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003453 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003454 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003455 if (!shell->locked) {
3456 wl_list_for_each(seat, &compositor->seat_list, link)
3457 activate(shell, surface, seat);
3458 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003459 break;
3460 default:
3461 break;
3462 }
3463
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003464 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003465 {
3466 switch (shell->win_animation_type) {
3467 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003468 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003469 break;
3470 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003471 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003472 break;
3473 default:
3474 break;
3475 }
3476 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003477}
3478
3479static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003480configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003481 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003482{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003483 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3484 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003485 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003486
Pekka Paalanen77346a62011-11-30 16:26:35 +02003487 shsurf = get_shell_surface(surface);
3488 if (shsurf)
3489 surface_type = shsurf->type;
3490
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003491 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003492
3493 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003494 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003495 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003496 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003497 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003498 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003499 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003500 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3501 NULL, NULL);
3502 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003503 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003504 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003505 break;
Alex Wu4539b082012-03-01 12:57:46 +08003506 case SHELL_SURFACE_TOPLEVEL:
3507 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003508 default:
3509 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003510 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003511
Alex Wu4539b082012-03-01 12:57:46 +08003512 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003513 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003514 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003515
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003516 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003517 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003518 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003519}
3520
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003521static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003522shell_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 +03003523{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003524 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003525 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003526
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003527 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003528
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003529 if (!weston_surface_is_mapped(es) &&
3530 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003531 remove_popup_grab(shsurf);
3532 }
3533
Giulio Camuffo184df502013-02-21 11:29:21 +01003534 if (width == 0)
3535 return;
3536
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003537 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003538 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003539 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003540 type_changed = 1;
3541 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003542
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003543 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003544 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003545 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003546 es->geometry.width != width ||
3547 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003548 float from_x, from_y;
3549 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003550
3551 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3552 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3553 configure(shell, es,
3554 es->geometry.x + to_x - from_x,
3555 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003556 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003557 }
3558}
3559
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003560static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003561
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003562static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003563desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003564{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003565 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003566 struct desktop_shell *shell =
3567 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003568
3569 shell->child.process.pid = 0;
3570 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003571
3572 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3573 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003574 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003575 shell->child.deathstamp = time;
3576 shell->child.deathcount = 0;
3577 }
3578
3579 shell->child.deathcount++;
3580 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003581 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003582 return;
3583 }
3584
Martin Minarik6d118362012-06-07 18:01:59 +02003585 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003586 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003587 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003588}
3589
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003590static void
3591launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003592{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003593 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003594 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003595
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003596 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003597 &shell->child.process,
3598 shell_exe,
3599 desktop_shell_sigchld);
3600
3601 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003602 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003603}
3604
3605static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003606bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3607{
Tiago Vignattibe143262012-04-16 17:31:41 +03003608 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003609 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003610
Jason Ekstranda85118c2013-06-27 20:17:02 -05003611 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3612 if (resource)
3613 wl_resource_set_implementation(resource, &shell_implementation,
3614 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003615}
3616
Kristian Høgsberg75840622011-09-06 13:48:16 -04003617static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003618unbind_desktop_shell(struct wl_resource *resource)
3619{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003620 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003621
3622 if (shell->locked)
3623 resume_desktop(shell);
3624
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003625 shell->child.desktop_shell = NULL;
3626 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003627}
3628
3629static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003630bind_desktop_shell(struct wl_client *client,
3631 void *data, uint32_t version, uint32_t id)
3632{
Tiago Vignattibe143262012-04-16 17:31:41 +03003633 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003634 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003635
Jason Ekstranda85118c2013-06-27 20:17:02 -05003636 resource = wl_resource_create(client, &desktop_shell_interface,
3637 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003638
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003639 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003640 wl_resource_set_implementation(resource,
3641 &desktop_shell_implementation,
3642 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003643 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003644
3645 if (version < 2)
3646 shell_fade_startup(shell);
3647
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003648 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003649 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003650
3651 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3652 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003653 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003654}
3655
Pekka Paalanen6e168112011-11-24 11:34:05 +02003656static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003657screensaver_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 -04003658{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003659 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003660
Giulio Camuffo184df502013-02-21 11:29:21 +01003661 if (width == 0)
3662 return;
3663
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003664 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003665 if (!shell->locked)
3666 return;
3667
3668 center_on_output(surface, surface->output);
3669
3670 if (wl_list_empty(&surface->layer_link)) {
3671 wl_list_insert(shell->lock_layer.surface_list.prev,
3672 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003673 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003674 wl_event_source_timer_update(shell->screensaver.timer,
3675 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003676 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003677 }
3678}
3679
3680static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003681screensaver_set_surface(struct wl_client *client,
3682 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003683 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003684 struct wl_resource *output_resource)
3685{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003686 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003687 struct weston_surface *surface =
3688 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003689 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003690
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003691 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003692 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003693 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003694}
3695
3696static const struct screensaver_interface screensaver_implementation = {
3697 screensaver_set_surface
3698};
3699
3700static void
3701unbind_screensaver(struct wl_resource *resource)
3702{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003703 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003704
Pekka Paalanen77346a62011-11-30 16:26:35 +02003705 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003706}
3707
3708static void
3709bind_screensaver(struct wl_client *client,
3710 void *data, uint32_t version, uint32_t id)
3711{
Tiago Vignattibe143262012-04-16 17:31:41 +03003712 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003713 struct wl_resource *resource;
3714
Jason Ekstranda85118c2013-06-27 20:17:02 -05003715 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003716
Pekka Paalanen77346a62011-11-30 16:26:35 +02003717 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003718 wl_resource_set_implementation(resource,
3719 &screensaver_implementation,
3720 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003721 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003722 return;
3723 }
3724
3725 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3726 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003727 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003728}
3729
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003730static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003731input_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 -04003732{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003733 struct input_panel_surface *ip_surface = surface->configure_private;
3734 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003735 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003736 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003737 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003738
Giulio Camuffo184df502013-02-21 11:29:21 +01003739 if (width == 0)
3740 return;
3741
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003742 if (!weston_surface_is_mapped(surface)) {
3743 if (!shell->showing_input_panels)
3744 return;
3745
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003746 show_surface = 1;
3747 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003748
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003749 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003750
3751 if (ip_surface->panel) {
3752 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3753 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3754 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003755 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003756
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003757 x = ip_surface->output->x + (mode->width - width) / 2;
3758 y = ip_surface->output->y + mode->height - height;
3759 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003760
3761 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003762 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003763 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003764
3765 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003766 wl_list_insert(&shell->input_panel_layer.surface_list,
3767 &surface->layer_link);
3768 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003769 weston_surface_damage(surface);
3770 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3771 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003772}
3773
3774static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003775destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003776{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003777 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3778
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003779 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003780 wl_list_remove(&input_panel_surface->link);
3781
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003782 input_panel_surface->surface->configure = NULL;
3783
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003784 free(input_panel_surface);
3785}
3786
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003787static struct input_panel_surface *
3788get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003789{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003790 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003791 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003792 } else {
3793 return NULL;
3794 }
3795}
3796
3797static void
3798input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3799{
3800 struct input_panel_surface *ipsurface = container_of(listener,
3801 struct input_panel_surface,
3802 surface_destroy_listener);
3803
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003804 if (ipsurface->resource) {
3805 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003806 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003807 destroy_input_panel_surface(ipsurface);
3808 }
3809}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003810
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003811static struct input_panel_surface *
3812create_input_panel_surface(struct desktop_shell *shell,
3813 struct weston_surface *surface)
3814{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003815 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003816
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003817 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3818 if (!input_panel_surface)
3819 return NULL;
3820
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003821 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003822 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003823
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003824 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003825
3826 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003827
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003828 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003829 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003830 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003831 &input_panel_surface->surface_destroy_listener);
3832
3833 wl_list_init(&input_panel_surface->link);
3834
3835 return input_panel_surface;
3836}
3837
3838static void
3839input_panel_surface_set_toplevel(struct wl_client *client,
3840 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003841 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003842 uint32_t position)
3843{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003844 struct input_panel_surface *input_panel_surface =
3845 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003846 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003847
3848 wl_list_insert(&shell->input_panel.surfaces,
3849 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003850
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003851 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003852 input_panel_surface->panel = 0;
3853}
3854
3855static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003856input_panel_surface_set_overlay_panel(struct wl_client *client,
3857 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003858{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003859 struct input_panel_surface *input_panel_surface =
3860 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003861 struct desktop_shell *shell = input_panel_surface->shell;
3862
3863 wl_list_insert(&shell->input_panel.surfaces,
3864 &input_panel_surface->link);
3865
3866 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003867}
3868
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003869static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003870 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003871 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003872};
3873
3874static void
3875destroy_input_panel_surface_resource(struct wl_resource *resource)
3876{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003877 struct input_panel_surface *ipsurf =
3878 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003879
3880 destroy_input_panel_surface(ipsurf);
3881}
3882
3883static void
3884input_panel_get_input_panel_surface(struct wl_client *client,
3885 struct wl_resource *resource,
3886 uint32_t id,
3887 struct wl_resource *surface_resource)
3888{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003889 struct weston_surface *surface =
3890 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003891 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003892 struct input_panel_surface *ipsurf;
3893
3894 if (get_input_panel_surface(surface)) {
3895 wl_resource_post_error(surface_resource,
3896 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003897 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003898 return;
3899 }
3900
3901 ipsurf = create_input_panel_surface(shell, surface);
3902 if (!ipsurf) {
3903 wl_resource_post_error(surface_resource,
3904 WL_DISPLAY_ERROR_INVALID_OBJECT,
3905 "surface->configure already set");
3906 return;
3907 }
3908
Jason Ekstranda85118c2013-06-27 20:17:02 -05003909 ipsurf->resource =
3910 wl_resource_create(client,
3911 &wl_input_panel_surface_interface, 1, id);
3912 wl_resource_set_implementation(ipsurf->resource,
3913 &input_panel_surface_implementation,
3914 ipsurf,
3915 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003916}
3917
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003918static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003919 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003920};
3921
3922static void
3923unbind_input_panel(struct wl_resource *resource)
3924{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003925 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003926
3927 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003928}
3929
3930static void
3931bind_input_panel(struct wl_client *client,
3932 void *data, uint32_t version, uint32_t id)
3933{
3934 struct desktop_shell *shell = data;
3935 struct wl_resource *resource;
3936
Jason Ekstranda85118c2013-06-27 20:17:02 -05003937 resource = wl_resource_create(client,
3938 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003939
3940 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003941 wl_resource_set_implementation(resource,
3942 &input_panel_implementation,
3943 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003944 shell->input_panel.binding = resource;
3945 return;
3946 }
3947
3948 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3949 "interface object already bound");
3950 wl_resource_destroy(resource);
3951}
3952
Kristian Høgsberg07045392012-02-19 18:52:44 -05003953struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003954 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003955 struct weston_surface *current;
3956 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003957 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003958};
3959
3960static void
3961switcher_next(struct switcher *switcher)
3962{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003963 struct weston_surface *surface;
3964 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003965 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003966 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003967
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003968 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003969 switch (get_shell_surface_type(surface)) {
3970 case SHELL_SURFACE_TOPLEVEL:
3971 case SHELL_SURFACE_FULLSCREEN:
3972 case SHELL_SURFACE_MAXIMIZED:
3973 if (first == NULL)
3974 first = surface;
3975 if (prev == switcher->current)
3976 next = surface;
3977 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003978 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003979 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003980 weston_surface_damage(surface);
3981 break;
3982 default:
3983 break;
3984 }
Alex Wu1659daa2012-04-01 20:13:09 +08003985
3986 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003987 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003988 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003989 weston_surface_damage(surface);
3990 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003991 }
3992
3993 if (next == NULL)
3994 next = first;
3995
Alex Wu07b26062012-03-12 16:06:01 +08003996 if (next == NULL)
3997 return;
3998
Kristian Høgsberg07045392012-02-19 18:52:44 -05003999 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004000 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004001
4002 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004003 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004004
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004005 shsurf = get_shell_surface(switcher->current);
4006 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004007 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004008}
4009
4010static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004011switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004012{
4013 struct switcher *switcher =
4014 container_of(listener, struct switcher, listener);
4015
4016 switcher_next(switcher);
4017}
4018
4019static void
Daniel Stone351eb612012-05-31 15:27:47 -04004020switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004021{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004022 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004023 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004024 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004025
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004026 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004027 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004028 weston_surface_damage(surface);
4029 }
4030
Alex Wu07b26062012-03-12 16:06:01 +08004031 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004032 activate(switcher->shell, switcher->current,
4033 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004034 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004035 weston_keyboard_end_grab(keyboard);
4036 if (keyboard->input_method_resource)
4037 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004038 free(switcher);
4039}
4040
4041static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004042switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004043 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004044{
4045 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004046 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004047
Daniel Stonec9785ea2012-05-30 16:31:52 +01004048 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004049 switcher_next(switcher);
4050}
4051
4052static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004053switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004054 uint32_t mods_depressed, uint32_t mods_latched,
4055 uint32_t mods_locked, uint32_t group)
4056{
4057 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004058 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004059
Daniel Stone351eb612012-05-31 15:27:47 -04004060 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4061 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004062}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004063
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004064static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004065 switcher_key,
4066 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004067};
4068
4069static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004070switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004071 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004072{
Tiago Vignattibe143262012-04-16 17:31:41 +03004073 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004074 struct switcher *switcher;
4075
4076 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004077 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004078 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004079 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004080 wl_list_init(&switcher->listener.link);
4081
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004082 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004083 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004084 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004085 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4086 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004087 switcher_next(switcher);
4088}
4089
Pekka Paalanen3c647232011-12-22 13:43:43 +02004090static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004091backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004092 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004093{
4094 struct weston_compositor *compositor = data;
4095 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004096 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004097
4098 /* TODO: we're limiting to simple use cases, where we assume just
4099 * control on the primary display. We'd have to extend later if we
4100 * ever get support for setting backlights on random desktop LCD
4101 * panels though */
4102 output = get_default_output(compositor);
4103 if (!output)
4104 return;
4105
4106 if (!output->set_backlight)
4107 return;
4108
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004109 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4110 backlight_new = output->backlight_current - 25;
4111 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4112 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004113
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004114 if (backlight_new < 5)
4115 backlight_new = 5;
4116 if (backlight_new > 255)
4117 backlight_new = 255;
4118
4119 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004120 output->set_backlight(output, output->backlight_current);
4121}
4122
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004123struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004124 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004125 struct weston_seat *seat;
4126 uint32_t key[2];
4127 int key_released[2];
4128};
4129
4130static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004131debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004132 uint32_t key, uint32_t state)
4133{
4134 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4135 struct wl_resource *resource;
4136 struct wl_display *display;
4137 uint32_t serial;
4138 int send = 0, terminate = 0;
4139 int check_binding = 1;
4140 int i;
4141
4142 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4143 /* Do not run bindings on key releases */
4144 check_binding = 0;
4145
4146 for (i = 0; i < 2; i++)
4147 if (key == db->key[i])
4148 db->key_released[i] = 1;
4149
4150 if (db->key_released[0] && db->key_released[1]) {
4151 /* All key releases been swalled so end the grab */
4152 terminate = 1;
4153 } else if (key != db->key[0] && key != db->key[1]) {
4154 /* Should not swallow release of other keys */
4155 send = 1;
4156 }
4157 } else if (key == db->key[0] && !db->key_released[0]) {
4158 /* Do not check bindings for the first press of the binding
4159 * key. This allows it to be used as a debug shortcut.
4160 * We still need to swallow this event. */
4161 check_binding = 0;
4162 } else if (db->key[1]) {
4163 /* If we already ran a binding don't process another one since
4164 * we can't keep track of all the binding keys that were
4165 * pressed in order to swallow the release events. */
4166 send = 1;
4167 check_binding = 0;
4168 }
4169
4170 if (check_binding) {
4171 struct weston_compositor *ec = db->seat->compositor;
4172
4173 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4174 key, state)) {
4175 /* We ran a binding so swallow the press and keep the
4176 * grab to swallow the released too. */
4177 send = 0;
4178 terminate = 0;
4179 db->key[1] = key;
4180 } else {
4181 /* Terminate the grab since the key pressed is not a
4182 * debug binding key. */
4183 send = 1;
4184 terminate = 1;
4185 }
4186 }
4187
4188 if (send) {
4189 resource = grab->keyboard->focus_resource;
4190
4191 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004192 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004193 serial = wl_display_next_serial(display);
4194 wl_keyboard_send_key(resource, serial, time, key, state);
4195 }
4196 }
4197
4198 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004199 weston_keyboard_end_grab(grab->keyboard);
4200 if (grab->keyboard->input_method_resource)
4201 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004202 free(db);
4203 }
4204}
4205
4206static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004207debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004208 uint32_t mods_depressed, uint32_t mods_latched,
4209 uint32_t mods_locked, uint32_t group)
4210{
4211 struct wl_resource *resource;
4212
4213 resource = grab->keyboard->focus_resource;
4214 if (!resource)
4215 return;
4216
4217 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4218 mods_latched, mods_locked, group);
4219}
4220
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004221struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004222 debug_binding_key,
4223 debug_binding_modifiers
4224};
4225
4226static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004227debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004228{
4229 struct debug_binding_grab *grab;
4230
4231 grab = calloc(1, sizeof *grab);
4232 if (!grab)
4233 return;
4234
4235 grab->seat = (struct weston_seat *) seat;
4236 grab->key[0] = key;
4237 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004238 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004239}
4240
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004241static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004242force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004243 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004244{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004245 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004246 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004247 struct desktop_shell *shell = data;
4248 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004249 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004250
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004251 focus_surface = seat->keyboard->focus;
4252 if (!focus_surface)
4253 return;
4254
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004255 wl_signal_emit(&compositor->kill_signal, focus_surface);
4256
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004257 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004258 wl_client_get_credentials(client, &pid, NULL, NULL);
4259
4260 /* Skip clients that we launched ourselves (the credentials of
4261 * the socketpair is ours) */
4262 if (pid == getpid())
4263 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004264
Daniel Stone325fc2d2012-05-30 16:31:58 +01004265 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004266}
4267
4268static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004269workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004270 uint32_t key, void *data)
4271{
4272 struct desktop_shell *shell = data;
4273 unsigned int new_index = shell->workspaces.current;
4274
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004275 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004276 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004277 if (new_index != 0)
4278 new_index--;
4279
4280 change_workspace(shell, new_index);
4281}
4282
4283static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004284workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004285 uint32_t key, void *data)
4286{
4287 struct desktop_shell *shell = data;
4288 unsigned int new_index = shell->workspaces.current;
4289
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004290 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004291 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004292 if (new_index < shell->workspaces.num - 1)
4293 new_index++;
4294
4295 change_workspace(shell, new_index);
4296}
4297
4298static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004299workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004300 uint32_t key, void *data)
4301{
4302 struct desktop_shell *shell = data;
4303 unsigned int new_index;
4304
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004305 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004306 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004307 new_index = key - KEY_F1;
4308 if (new_index >= shell->workspaces.num)
4309 new_index = shell->workspaces.num - 1;
4310
4311 change_workspace(shell, new_index);
4312}
4313
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004314static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004315workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004316 uint32_t key, void *data)
4317{
4318 struct desktop_shell *shell = data;
4319 unsigned int new_index = shell->workspaces.current;
4320
4321 if (shell->locked)
4322 return;
4323
4324 if (new_index != 0)
4325 new_index--;
4326
4327 take_surface_to_workspace_by_seat(shell, seat, new_index);
4328}
4329
4330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004331workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004332 uint32_t key, void *data)
4333{
4334 struct desktop_shell *shell = data;
4335 unsigned int new_index = shell->workspaces.current;
4336
4337 if (shell->locked)
4338 return;
4339
4340 if (new_index < shell->workspaces.num - 1)
4341 new_index++;
4342
4343 take_surface_to_workspace_by_seat(shell, seat, new_index);
4344}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004345
4346static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004347shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004348{
Tiago Vignattibe143262012-04-16 17:31:41 +03004349 struct desktop_shell *shell =
4350 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004351 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004352
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004353 if (shell->child.client)
4354 wl_client_destroy(shell->child.client);
4355
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004356 wl_list_remove(&shell->idle_listener.link);
4357 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004358 wl_list_remove(&shell->show_input_panel_listener.link);
4359 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004360
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004361 wl_array_for_each(ws, &shell->workspaces.array)
4362 workspace_destroy(*ws);
4363 wl_array_release(&shell->workspaces.array);
4364
Pekka Paalanen3c647232011-12-22 13:43:43 +02004365 free(shell->screensaver.path);
4366 free(shell);
4367}
4368
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004369static void
4370shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4371{
4372 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004373 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004374
4375 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004376 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4377 MODIFIER_CTRL | MODIFIER_ALT,
4378 terminate_binding, ec);
4379 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4380 click_to_activate_binding,
4381 shell);
4382 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4383 MODIFIER_SUPER | MODIFIER_ALT,
4384 surface_opacity_binding, NULL);
4385 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4386 MODIFIER_SUPER, zoom_axis_binding,
4387 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004388
4389 /* configurable bindings */
4390 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004391 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4392 zoom_key_binding, NULL);
4393 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4394 zoom_key_binding, NULL);
4395 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4396 shell);
4397 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4398 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004399
4400 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4401 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4402 rotate_binding, NULL);
4403
Daniel Stone325fc2d2012-05-30 16:31:58 +01004404 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4405 shell);
4406 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4407 ec);
4408 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4409 backlight_binding, ec);
4410 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4411 ec);
4412 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4413 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004414 weston_compositor_add_key_binding(ec, KEY_K, mod,
4415 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004416 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4417 workspace_up_binding, shell);
4418 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4419 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004420 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4421 workspace_move_surface_up_binding,
4422 shell);
4423 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4424 workspace_move_surface_down_binding,
4425 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004426
4427 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4428 if (shell->workspaces.num > 1) {
4429 num_workspace_bindings = shell->workspaces.num;
4430 if (num_workspace_bindings > 6)
4431 num_workspace_bindings = 6;
4432 for (i = 0; i < num_workspace_bindings; i++)
4433 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4434 workspace_f_binding,
4435 shell);
4436 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004437
4438 /* Debug bindings */
4439 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4440 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004441}
4442
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004443WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004444module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004445 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004446{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004447 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004448 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004449 struct workspace **pws;
4450 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004451 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004452
4453 shell = malloc(sizeof *shell);
4454 if (shell == NULL)
4455 return -1;
4456
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004457 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004458 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004459
4460 shell->destroy_listener.notify = shell_destroy;
4461 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004462 shell->idle_listener.notify = idle_handler;
4463 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4464 shell->wake_listener.notify = wake_handler;
4465 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004466 shell->show_input_panel_listener.notify = show_input_panels;
4467 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4468 shell->hide_input_panel_listener.notify = hide_input_panels;
4469 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004470 shell->update_input_panel_listener.notify = update_input_panels;
4471 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004472 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004473 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004474 ec->shell_interface.create_shell_surface = create_shell_surface;
4475 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004476 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004477 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004478 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004479 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004480 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004481
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004482 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004483
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004484 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4485 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004486 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4487 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004488 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004489
4490 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004491 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004492
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004493 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004494
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004495 for (i = 0; i < shell->workspaces.num; i++) {
4496 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4497 if (pws == NULL)
4498 return -1;
4499
4500 *pws = workspace_create();
4501 if (*pws == NULL)
4502 return -1;
4503 }
4504 activate_workspace(shell, 0);
4505
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004506 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004507 wl_list_init(&shell->workspaces.animation.link);
4508 shell->workspaces.animation.frame = animate_workspace_change_frame;
4509
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004510 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004511 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004512 return -1;
4513
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004514 if (wl_global_create(ec->wl_display,
4515 &desktop_shell_interface, 2,
4516 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004517 return -1;
4518
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004519 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4520 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004521 return -1;
4522
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004523 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004524 shell, bind_input_panel) == NULL)
4525 return -1;
4526
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004527 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4528 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004529 return -1;
4530
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004531 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004532
4533 loop = wl_display_get_event_loop(ec->wl_display);
4534 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004535
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004536 shell->screensaver.timer =
4537 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4538
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004539 wl_list_for_each(seat, &ec->seat_list, link)
4540 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004541
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004542 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004543
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004544 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004545
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004546 return 0;
4547}