blob: aa2db3d1110271213c584103f734c902694ba993 [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);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300338 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400339 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341}
342
343static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300344shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400346 if (grab->shsurf)
347 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400349 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300350}
351
352static void
Alex Wu4539b082012-03-01 12:57:46 +0800353center_on_output(struct weston_surface *surface,
354 struct weston_output *output);
355
Daniel Stone496ca172012-05-30 16:31:42 +0100356static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300357get_modifier(char *modifier)
358{
359 if (!modifier)
360 return MODIFIER_SUPER;
361
362 if (!strcmp("ctrl", modifier))
363 return MODIFIER_CTRL;
364 else if (!strcmp("alt", modifier))
365 return MODIFIER_ALT;
366 else if (!strcmp("super", modifier))
367 return MODIFIER_SUPER;
368 else
369 return MODIFIER_SUPER;
370}
371
Juan Zhaoe10d2792012-04-25 19:09:52 +0800372static enum animation_type
373get_animation_type(char *animation)
374{
375 if (!animation)
376 return ANIMATION_NONE;
377
378 if (!strcmp("zoom", animation))
379 return ANIMATION_ZOOM;
380 else if (!strcmp("fade", animation))
381 return ANIMATION_FADE;
382 else
383 return ANIMATION_NONE;
384}
385
Alex Wu4539b082012-03-01 12:57:46 +0800386static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400387shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200388{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400389 struct weston_config_section *section;
390 int duration;
391 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400393 section = weston_config_get_section(shell->compositor->config,
394 "screensaver", NULL, NULL);
395 weston_config_section_get_string(section,
396 "path", &shell->screensaver.path, NULL);
397 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200398 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400399
400 section = weston_config_get_section(shell->compositor->config,
401 "shell", NULL, NULL);
402 weston_config_section_get_string(section,
403 "binding-modifier", &s, "super");
404 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200405 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400406 weston_config_section_get_string(section, "animation", &s, "none");
407 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200408 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400409 weston_config_section_get_uint(section, "num-workspaces",
410 &shell->workspaces.num,
411 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200412}
413
414static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200415focus_state_destroy(struct focus_state *state)
416{
417 wl_list_remove(&state->seat_destroy_listener.link);
418 wl_list_remove(&state->surface_destroy_listener.link);
419 free(state);
420}
421
422static void
423focus_state_seat_destroy(struct wl_listener *listener, void *data)
424{
425 struct focus_state *state = container_of(listener,
426 struct focus_state,
427 seat_destroy_listener);
428
429 wl_list_remove(&state->link);
430 focus_state_destroy(state);
431}
432
433static void
434focus_state_surface_destroy(struct wl_listener *listener, void *data)
435{
436 struct focus_state *state = container_of(listener,
437 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400438 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400439 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300440 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200442
Pekka Paalanen01388e22013-04-25 13:57:44 +0300443 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
444
Kristian Høgsberge3778222012-07-31 17:29:30 -0400445 next = NULL;
446 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300447 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400448 continue;
449
450 next = surface;
451 break;
452 }
453
Pekka Paalanen01388e22013-04-25 13:57:44 +0300454 /* if the focus was a sub-surface, activate its main surface */
455 if (main_surface != state->keyboard_focus)
456 next = main_surface;
457
Kristian Høgsberge3778222012-07-31 17:29:30 -0400458 if (next) {
459 shell = state->seat->compositor->shell_interface.shell;
460 activate(shell, next, state->seat);
461 } else {
462 wl_list_remove(&state->link);
463 focus_state_destroy(state);
464 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200465}
466
467static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400468focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200469{
Jonas Ådahl04769742012-06-13 00:01:24 +0200470 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200471
472 state = malloc(sizeof *state);
473 if (state == NULL)
474 return NULL;
475
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400476 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200477 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400478 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200479
480 state->seat_destroy_listener.notify = focus_state_seat_destroy;
481 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400482 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200483 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400484 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200485
486 return state;
487}
488
Jonas Ådahl8538b222012-08-29 22:13:03 +0200489static struct focus_state *
490ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
491{
492 struct workspace *ws = get_current_workspace(shell);
493 struct focus_state *state;
494
495 wl_list_for_each(state, &ws->focus_list, link)
496 if (state->seat == seat)
497 break;
498
499 if (&state->link == &ws->focus_list)
500 state = focus_state_create(seat, ws);
501
502 return state;
503}
504
Jonas Ådahl04769742012-06-13 00:01:24 +0200505static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400506restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200507{
508 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400509 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200510
511 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400512 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200513
Kristian Høgsberge3148752013-05-06 23:19:49 -0400514 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200515 }
516}
517
518static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200519replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
520 struct weston_seat *seat)
521{
522 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400523 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200524
525 wl_list_for_each(state, &ws->focus_list, link) {
526 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400527 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500528 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200529 return;
530 }
531 }
532}
533
534static void
535drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
536 struct weston_surface *surface)
537{
538 struct focus_state *state;
539
540 wl_list_for_each(state, &ws->focus_list, link)
541 if (state->keyboard_focus == surface)
542 state->keyboard_focus = NULL;
543}
544
545static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200546workspace_destroy(struct workspace *ws)
547{
Jonas Ådahl04769742012-06-13 00:01:24 +0200548 struct focus_state *state, *next;
549
550 wl_list_for_each_safe(state, next, &ws->focus_list, link)
551 focus_state_destroy(state);
552
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553 free(ws);
554}
555
Jonas Ådahl04769742012-06-13 00:01:24 +0200556static void
557seat_destroyed(struct wl_listener *listener, void *data)
558{
559 struct weston_seat *seat = data;
560 struct focus_state *state, *next;
561 struct workspace *ws = container_of(listener,
562 struct workspace,
563 seat_destroyed_listener);
564
565 wl_list_for_each_safe(state, next, &ws->focus_list, link)
566 if (state->seat == seat)
567 wl_list_remove(&state->link);
568}
569
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200570static struct workspace *
571workspace_create(void)
572{
573 struct workspace *ws = malloc(sizeof *ws);
574 if (ws == NULL)
575 return NULL;
576
577 weston_layer_init(&ws->layer, NULL);
578
Jonas Ådahl04769742012-06-13 00:01:24 +0200579 wl_list_init(&ws->focus_list);
580 wl_list_init(&ws->seat_destroyed_listener.link);
581 ws->seat_destroyed_listener.notify = seat_destroyed;
582
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200583 return ws;
584}
585
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200586static int
587workspace_is_empty(struct workspace *ws)
588{
589 return wl_list_empty(&ws->layer.surface_list);
590}
591
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200592static struct workspace *
593get_workspace(struct desktop_shell *shell, unsigned int index)
594{
595 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200596 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200597 pws += index;
598 return *pws;
599}
600
601static struct workspace *
602get_current_workspace(struct desktop_shell *shell)
603{
604 return get_workspace(shell, shell->workspaces.current);
605}
606
607static void
608activate_workspace(struct desktop_shell *shell, unsigned int index)
609{
610 struct workspace *ws;
611
612 ws = get_workspace(shell, index);
613 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
614
615 shell->workspaces.current = index;
616}
617
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200618static unsigned int
619get_output_height(struct weston_output *output)
620{
621 return abs(output->region.extents.y1 - output->region.extents.y2);
622}
623
624static void
625surface_translate(struct weston_surface *surface, double d)
626{
627 struct shell_surface *shsurf = get_shell_surface(surface);
628 struct weston_transform *transform;
629
630 transform = &shsurf->workspace_transform;
631 if (wl_list_empty(&transform->link))
632 wl_list_insert(surface->geometry.transformation_list.prev,
633 &shsurf->workspace_transform.link);
634
635 weston_matrix_init(&shsurf->workspace_transform.matrix);
636 weston_matrix_translate(&shsurf->workspace_transform.matrix,
637 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200638 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200639}
640
641static void
642workspace_translate_out(struct workspace *ws, double fraction)
643{
644 struct weston_surface *surface;
645 unsigned int height;
646 double d;
647
648 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
649 height = get_output_height(surface->output);
650 d = height * fraction;
651
652 surface_translate(surface, d);
653 }
654}
655
656static void
657workspace_translate_in(struct workspace *ws, double fraction)
658{
659 struct weston_surface *surface;
660 unsigned int height;
661 double d;
662
663 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
664 height = get_output_height(surface->output);
665
666 if (fraction > 0)
667 d = -(height - height * fraction);
668 else
669 d = height + height * fraction;
670
671 surface_translate(surface, d);
672 }
673}
674
675static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200676broadcast_current_workspace_state(struct desktop_shell *shell)
677{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500678 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200679
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500680 for (link = shell->workspaces.client_list.next;
681 link != &shell->workspaces.client_list;
682 link = link->next) {
683 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200684 shell->workspaces.current,
685 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500686 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200687}
688
689static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200690reverse_workspace_change_animation(struct desktop_shell *shell,
691 unsigned int index,
692 struct workspace *from,
693 struct workspace *to)
694{
695 shell->workspaces.current = index;
696
697 shell->workspaces.anim_to = to;
698 shell->workspaces.anim_from = from;
699 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
700 shell->workspaces.anim_timestamp = 0;
701
Scott Moreau4272e632012-08-13 09:58:41 -0600702 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200703}
704
705static void
706workspace_deactivate_transforms(struct workspace *ws)
707{
708 struct weston_surface *surface;
709 struct shell_surface *shsurf;
710
711 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
712 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
714 wl_list_remove(&shsurf->workspace_transform.link);
715 wl_list_init(&shsurf->workspace_transform.link);
716 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200717 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718 }
719}
720
721static void
722finish_workspace_change_animation(struct desktop_shell *shell,
723 struct workspace *from,
724 struct workspace *to)
725{
Scott Moreau4272e632012-08-13 09:58:41 -0600726 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200727
728 wl_list_remove(&shell->workspaces.animation.link);
729 workspace_deactivate_transforms(from);
730 workspace_deactivate_transforms(to);
731 shell->workspaces.anim_to = NULL;
732
733 wl_list_remove(&shell->workspaces.anim_from->layer.link);
734}
735
736static void
737animate_workspace_change_frame(struct weston_animation *animation,
738 struct weston_output *output, uint32_t msecs)
739{
740 struct desktop_shell *shell =
741 container_of(animation, struct desktop_shell,
742 workspaces.animation);
743 struct workspace *from = shell->workspaces.anim_from;
744 struct workspace *to = shell->workspaces.anim_to;
745 uint32_t t;
746 double x, y;
747
748 if (workspace_is_empty(from) && workspace_is_empty(to)) {
749 finish_workspace_change_animation(shell, from, to);
750 return;
751 }
752
753 if (shell->workspaces.anim_timestamp == 0) {
754 if (shell->workspaces.anim_current == 0.0)
755 shell->workspaces.anim_timestamp = msecs;
756 else
757 shell->workspaces.anim_timestamp =
758 msecs -
759 /* Invers of movement function 'y' below. */
760 (asin(1.0 - shell->workspaces.anim_current) *
761 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
762 M_2_PI);
763 }
764
765 t = msecs - shell->workspaces.anim_timestamp;
766
767 /*
768 * x = [0, π/2]
769 * y(x) = sin(x)
770 */
771 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
772 y = sin(x);
773
774 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600775 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776
777 workspace_translate_out(from, shell->workspaces.anim_dir * y);
778 workspace_translate_in(to, shell->workspaces.anim_dir * y);
779 shell->workspaces.anim_current = y;
780
Scott Moreau4272e632012-08-13 09:58:41 -0600781 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200783 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200785}
786
787static void
788animate_workspace_change(struct desktop_shell *shell,
789 unsigned int index,
790 struct workspace *from,
791 struct workspace *to)
792{
793 struct weston_output *output;
794
795 int dir;
796
797 if (index > shell->workspaces.current)
798 dir = -1;
799 else
800 dir = 1;
801
802 shell->workspaces.current = index;
803
804 shell->workspaces.anim_dir = dir;
805 shell->workspaces.anim_from = from;
806 shell->workspaces.anim_to = to;
807 shell->workspaces.anim_current = 0.0;
808 shell->workspaces.anim_timestamp = 0;
809
810 output = container_of(shell->compositor->output_list.next,
811 struct weston_output, link);
812 wl_list_insert(&output->animation_list,
813 &shell->workspaces.animation.link);
814
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200815 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200816
817 workspace_translate_in(to, 0);
818
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400819 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200820
Scott Moreau4272e632012-08-13 09:58:41 -0600821 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200822}
823
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200825update_workspace(struct desktop_shell *shell, unsigned int index,
826 struct workspace *from, struct workspace *to)
827{
828 shell->workspaces.current = index;
829 wl_list_insert(&from->layer.link, &to->layer.link);
830 wl_list_remove(&from->layer.link);
831}
832
833static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834change_workspace(struct desktop_shell *shell, unsigned int index)
835{
836 struct workspace *from;
837 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200838
839 if (index == shell->workspaces.current)
840 return;
841
842 /* Don't change workspace when there is any fullscreen surfaces. */
843 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
844 return;
845
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846 from = get_current_workspace(shell);
847 to = get_workspace(shell, index);
848
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200849 if (shell->workspaces.anim_from == to &&
850 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200851 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200853 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200854 return;
855 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200856
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 if (shell->workspaces.anim_to != NULL)
858 finish_workspace_change_animation(shell,
859 shell->workspaces.anim_from,
860 shell->workspaces.anim_to);
861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200862 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200863
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200864 if (workspace_is_empty(to) && workspace_is_empty(from))
865 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866 else
867 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200868
869 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200870}
871
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200872static bool
873workspace_has_only(struct workspace *ws, struct weston_surface *surface)
874{
875 struct wl_list *list = &ws->layer.surface_list;
876 struct wl_list *e;
877
878 if (wl_list_empty(list))
879 return false;
880
881 e = list->next;
882
883 if (e->next != list)
884 return false;
885
886 return container_of(e, struct weston_surface, layer_link) == surface;
887}
888
889static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200890move_surface_to_workspace(struct desktop_shell *shell,
891 struct weston_surface *surface,
892 uint32_t workspace)
893{
894 struct workspace *from;
895 struct workspace *to;
896 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300897 struct weston_surface *focus;
898
899 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900
901 if (workspace == shell->workspaces.current)
902 return;
903
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200904 if (workspace >= shell->workspaces.num)
905 workspace = shell->workspaces.num - 1;
906
Jonas Ådahle9d22502012-08-29 22:13:01 +0200907 from = get_current_workspace(shell);
908 to = get_workspace(shell, workspace);
909
910 wl_list_remove(&surface->layer_link);
911 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
912
913 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300914 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
915 if (!seat->keyboard)
916 continue;
917
918 focus = weston_surface_get_main_surface(seat->keyboard->focus);
919 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400920 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300921 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200922
923 weston_surface_damage_below(surface);
924}
925
926static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400928 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929 unsigned int index)
930{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300931 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 struct shell_surface *shsurf;
933 struct workspace *from;
934 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200935 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936
Pekka Paalanen01388e22013-04-25 13:57:44 +0300937 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 if (surface == NULL ||
939 index == shell->workspaces.current)
940 return;
941
942 from = get_current_workspace(shell);
943 to = get_workspace(shell, index);
944
945 wl_list_remove(&surface->layer_link);
946 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
947
Jonas Ådahle9d22502012-08-29 22:13:01 +0200948 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200949 drop_focus_state(shell, from, surface);
950
951 if (shell->workspaces.anim_from == to &&
952 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953 wl_list_remove(&to->layer.link);
954 wl_list_insert(from->layer.link.prev, &to->layer.link);
955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200957 broadcast_current_workspace_state(shell);
958
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959 return;
960 }
961
962 if (shell->workspaces.anim_to != NULL)
963 finish_workspace_change_animation(shell,
964 shell->workspaces.anim_from,
965 shell->workspaces.anim_to);
966
967 if (workspace_is_empty(from) &&
968 workspace_has_only(to, surface))
969 update_workspace(shell, index, from, to);
970 else {
971 shsurf = get_shell_surface(surface);
972 if (wl_list_empty(&shsurf->workspace_transform.link))
973 wl_list_insert(&shell->workspaces.anim_sticky_list,
974 &shsurf->workspace_transform.link);
975
976 animate_workspace_change(shell, index, from, to);
977 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200978
979 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200980
981 state = ensure_focus_state(shell, seat);
982 if (state != NULL)
983 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200984}
985
986static void
987workspace_manager_move_surface(struct wl_client *client,
988 struct wl_resource *resource,
989 struct wl_resource *surface_resource,
990 uint32_t workspace)
991{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500992 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500994 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996
Pekka Paalanen01388e22013-04-25 13:57:44 +0300997 main_surface = weston_surface_get_main_surface(surface);
998 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200999}
1000
1001static const struct workspace_manager_interface workspace_manager_implementation = {
1002 workspace_manager_move_surface,
1003};
1004
1005static void
1006unbind_resource(struct wl_resource *resource)
1007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001008 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001009}
1010
1011static void
1012bind_workspace_manager(struct wl_client *client,
1013 void *data, uint32_t version, uint32_t id)
1014{
1015 struct desktop_shell *shell = data;
1016 struct wl_resource *resource;
1017
Jason Ekstranda85118c2013-06-27 20:17:02 -05001018 resource = wl_resource_create(client,
1019 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001020
1021 if (resource == NULL) {
1022 weston_log("couldn't add workspace manager object");
1023 return;
1024 }
1025
Jason Ekstranda85118c2013-06-27 20:17:02 -05001026 wl_resource_set_implementation(resource,
1027 &workspace_manager_implementation,
1028 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001029 wl_list_insert(&shell->workspaces.client_list,
1030 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001031
1032 workspace_manager_send_state(resource,
1033 shell->workspaces.current,
1034 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001035}
1036
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001037static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001038noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001039{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001040}
1041
1042static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001043move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001045 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001046 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001047 struct shell_surface *shsurf = move->base.shsurf;
1048 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001049 int dx = wl_fixed_to_int(pointer->x + move->dx);
1050 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001051
1052 if (!shsurf)
1053 return;
1054
1055 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056
Daniel Stone103db7f2012-05-08 17:17:55 +01001057 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001058 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001059
1060 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061}
1062
1063static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001064move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001065 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001066{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001067 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1068 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001069 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001070 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001071
Daniel Stone4dbadb12012-05-30 16:31:51 +01001072 if (pointer->button_count == 0 &&
1073 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001074 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001075 free(grab);
1076 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001077}
1078
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001079static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001080 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001081 move_grab_motion,
1082 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001083};
1084
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001085static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001086surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001087{
1088 struct weston_move_grab *move;
1089
1090 if (!shsurf)
1091 return -1;
1092
1093 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1094 return 0;
1095
1096 move = malloc(sizeof *move);
1097 if (!move)
1098 return -1;
1099
1100 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001101 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001102 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001103 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001104
1105 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001106 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001107
1108 return 0;
1109}
1110
1111static void
1112shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1113 struct wl_resource *seat_resource, uint32_t serial)
1114{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001115 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001116 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001117 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001118
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001119 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001120 if (seat->pointer->button_count == 0 ||
1121 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001122 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001123 return;
1124
Kristian Høgsberge3148752013-05-06 23:19:49 -04001125 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001126 wl_resource_post_no_memory(resource);
1127}
1128
1129struct weston_resize_grab {
1130 struct shell_grab base;
1131 uint32_t edges;
1132 int32_t width, height;
1133};
1134
1135static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001136resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001137{
1138 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001139 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001140 struct shell_surface *shsurf = resize->base.shsurf;
1141 int32_t width, height;
1142 wl_fixed_t from_x, from_y;
1143 wl_fixed_t to_x, to_y;
1144
1145 if (!shsurf)
1146 return;
1147
1148 weston_surface_from_global_fixed(shsurf->surface,
1149 pointer->grab_x, pointer->grab_y,
1150 &from_x, &from_y);
1151 weston_surface_from_global_fixed(shsurf->surface,
1152 pointer->x, pointer->y, &to_x, &to_y);
1153
1154 width = resize->width;
1155 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1156 width += wl_fixed_to_int(from_x - to_x);
1157 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1158 width += wl_fixed_to_int(to_x - from_x);
1159 }
1160
1161 height = resize->height;
1162 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1163 height += wl_fixed_to_int(from_y - to_y);
1164 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1165 height += wl_fixed_to_int(to_y - from_y);
1166 }
1167
1168 shsurf->client->send_configure(shsurf->surface,
1169 resize->edges, width, height);
1170}
1171
1172static void
1173send_configure(struct weston_surface *surface,
1174 uint32_t edges, int32_t width, int32_t height)
1175{
1176 struct shell_surface *shsurf = get_shell_surface(surface);
1177
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001178 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001179 edges, width, height);
1180}
1181
1182static const struct weston_shell_client shell_client = {
1183 send_configure
1184};
1185
1186static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001187resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001188 uint32_t time, uint32_t button, uint32_t state_w)
1189{
1190 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001191 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001192 enum wl_pointer_button_state state = state_w;
1193
1194 if (pointer->button_count == 0 &&
1195 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1196 shell_grab_end(&resize->base);
1197 free(grab);
1198 }
1199}
1200
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001201static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001202 noop_grab_focus,
1203 resize_grab_motion,
1204 resize_grab_button,
1205};
1206
Giulio Camuffob8366642013-04-25 13:57:46 +03001207/*
1208 * Returns the bounding box of a surface and all its sub-surfaces,
1209 * in the surface coordinates system. */
1210static void
1211surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1212 int32_t *y, int32_t *w, int32_t *h) {
1213 pixman_region32_t region;
1214 pixman_box32_t *box;
1215 struct weston_subsurface *subsurface;
1216
1217 pixman_region32_init_rect(&region, 0, 0,
1218 surface->geometry.width,
1219 surface->geometry.height);
1220
1221 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1222 pixman_region32_union_rect(&region, &region,
1223 subsurface->position.x,
1224 subsurface->position.y,
1225 subsurface->surface->geometry.width,
1226 subsurface->surface->geometry.height);
1227 }
1228
1229 box = pixman_region32_extents(&region);
1230 if (x)
1231 *x = box->x1;
1232 if (y)
1233 *y = box->y1;
1234 if (w)
1235 *w = box->x2 - box->x1;
1236 if (h)
1237 *h = box->y2 - box->y1;
1238
1239 pixman_region32_fini(&region);
1240}
1241
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001242static int
1243surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001244 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001245{
1246 struct weston_resize_grab *resize;
1247
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001248 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1249 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001250 return 0;
1251
1252 if (edges == 0 || edges > 15 ||
1253 (edges & 3) == 3 || (edges & 12) == 12)
1254 return 0;
1255
1256 resize = malloc(sizeof *resize);
1257 if (!resize)
1258 return -1;
1259
1260 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001261 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1262 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001263
1264 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001266
1267 return 0;
1268}
1269
1270static void
1271shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1272 struct wl_resource *seat_resource, uint32_t serial,
1273 uint32_t edges)
1274{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001275 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001276 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001277 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001278
1279 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1280 return;
1281
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001282 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001283 if (seat->pointer->button_count == 0 ||
1284 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001285 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001286 return;
1287
Kristian Høgsberge3148752013-05-06 23:19:49 -04001288 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001289 wl_resource_post_no_memory(resource);
1290}
1291
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001292static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001293busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001294{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001296 struct weston_pointer *pointer = base->pointer;
1297 struct weston_surface *surface;
1298 wl_fixed_t sx, sy;
1299
1300 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1301 pointer->x, pointer->y,
1302 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001303
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001304 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001305 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001306 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001307 }
1308}
1309
1310static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001311busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001312{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001313}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001314
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001316busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001317 uint32_t time, uint32_t button, uint32_t state)
1318{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001319 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001320 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001321 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001322
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001323 if (shsurf && button == BTN_LEFT && state) {
1324 activate(shsurf->shell, shsurf->surface, seat);
1325 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001326 } else if (shsurf && button == BTN_RIGHT && state) {
1327 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001328 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001329 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001330}
1331
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001332static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001333 busy_cursor_grab_focus,
1334 busy_cursor_grab_motion,
1335 busy_cursor_grab_button,
1336};
1337
1338static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001339set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001340{
1341 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001342
1343 grab = malloc(sizeof *grab);
1344 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001345 return;
1346
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001347 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1348 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001349}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001350
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001351static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001352end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001353{
1354 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1355
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001356 if (grab->grab.interface == &busy_cursor_grab_interface &&
1357 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001358 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001359 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001360 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001361}
1362
Scott Moreau9521d5e2012-04-19 13:06:17 -06001363static void
1364ping_timer_destroy(struct shell_surface *shsurf)
1365{
1366 if (!shsurf || !shsurf->ping_timer)
1367 return;
1368
1369 if (shsurf->ping_timer->source)
1370 wl_event_source_remove(shsurf->ping_timer->source);
1371
1372 free(shsurf->ping_timer);
1373 shsurf->ping_timer = NULL;
1374}
1375
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001376static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001377ping_timeout_handler(void *data)
1378{
1379 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001380 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001381
Scott Moreau9521d5e2012-04-19 13:06:17 -06001382 /* Client is not responding */
1383 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001384
1385 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001386 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001387 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001388
1389 return 1;
1390}
1391
1392static void
1393ping_handler(struct weston_surface *surface, uint32_t serial)
1394{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001395 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001396 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001397 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001398
1399 if (!shsurf)
1400 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001401 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001402 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001403
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001404 if (shsurf->surface == shsurf->shell->grab_surface)
1405 return;
1406
Scott Moreauff1db4a2012-04-17 19:06:18 -06001407 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001408 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001409 if (!shsurf->ping_timer)
1410 return;
1411
1412 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001413 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1414 shsurf->ping_timer->source =
1415 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1416 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1417
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001418 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001419 }
1420}
1421
1422static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001423handle_pointer_focus(struct wl_listener *listener, void *data)
1424{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001425 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001426 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001427 struct weston_compositor *compositor;
1428 struct shell_surface *shsurf;
1429 uint32_t serial;
1430
1431 if (!surface)
1432 return;
1433
1434 compositor = surface->compositor;
1435 shsurf = get_shell_surface(surface);
1436
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001437 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001438 set_busy_cursor(shsurf, pointer);
1439 } else {
1440 serial = wl_display_next_serial(compositor->wl_display);
1441 ping_handler(surface, serial);
1442 }
1443}
1444
1445static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001446create_pointer_focus_listener(struct weston_seat *seat)
1447{
1448 struct wl_listener *listener;
1449
Kristian Høgsberge3148752013-05-06 23:19:49 -04001450 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001451 return;
1452
1453 listener = malloc(sizeof *listener);
1454 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001456}
1457
1458static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001459shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1460 uint32_t serial)
1461{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001462 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001463 struct weston_seat *seat;
1464 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001465
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001466 if (shsurf->ping_timer == NULL)
1467 /* Just ignore unsolicited pong. */
1468 return;
1469
Scott Moreauff1db4a2012-04-17 19:06:18 -06001470 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001471 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001472 wl_list_for_each(seat, &ec->seat_list, link) {
1473 if(seat->pointer)
1474 end_busy_cursor(shsurf, seat->pointer);
1475 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001476 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001477 }
1478}
1479
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001480static void
1481shell_surface_set_title(struct wl_client *client,
1482 struct wl_resource *resource, const char *title)
1483{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001484 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001485
1486 free(shsurf->title);
1487 shsurf->title = strdup(title);
1488}
1489
1490static void
1491shell_surface_set_class(struct wl_client *client,
1492 struct wl_resource *resource, const char *class)
1493{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001494 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001495
1496 free(shsurf->class);
1497 shsurf->class = strdup(class);
1498}
1499
Juan Zhao96879df2012-02-07 08:45:41 +08001500static struct weston_output *
1501get_default_output(struct weston_compositor *compositor)
1502{
1503 return container_of(compositor->output_list.next,
1504 struct weston_output, link);
1505}
1506
Alex Wu4539b082012-03-01 12:57:46 +08001507static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001508restore_output_mode(struct weston_output *output)
1509{
1510 if (output->current != output->origin ||
1511 (int32_t)output->scale != output->origin_scale)
1512 weston_output_switch_mode(output,
1513 output->origin,
1514 output->origin_scale);
1515}
1516
1517static void
1518restore_all_output_modes(struct weston_compositor *compositor)
1519{
1520 struct weston_output *output;
1521
1522 wl_list_for_each(output, &compositor->output_list, link)
1523 restore_output_mode(output);
1524}
1525
1526static void
Alex Wu4539b082012-03-01 12:57:46 +08001527shell_unset_fullscreen(struct shell_surface *shsurf)
1528{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001529 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001530 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001531 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1532 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001533 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001534 }
Alex Wu4539b082012-03-01 12:57:46 +08001535 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1536 shsurf->fullscreen.framerate = 0;
1537 wl_list_remove(&shsurf->fullscreen.transform.link);
1538 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001539 if (shsurf->fullscreen.black_surface)
1540 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001541 shsurf->fullscreen.black_surface = NULL;
1542 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001543 weston_surface_set_position(shsurf->surface,
1544 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001545 if (shsurf->saved_rotation_valid) {
1546 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1547 &shsurf->rotation.transform.link);
1548 shsurf->saved_rotation_valid = false;
1549 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001550
1551 ws = get_current_workspace(shsurf->shell);
1552 wl_list_remove(&shsurf->surface->layer_link);
1553 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001554}
1555
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001556static void
1557shell_unset_maximized(struct shell_surface *shsurf)
1558{
1559 struct workspace *ws;
1560 /* undo all maximized things here */
1561 shsurf->output = get_default_output(shsurf->surface->compositor);
1562 weston_surface_set_position(shsurf->surface,
1563 shsurf->saved_x,
1564 shsurf->saved_y);
1565
1566 if (shsurf->saved_rotation_valid) {
1567 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1568 &shsurf->rotation.transform.link);
1569 shsurf->saved_rotation_valid = false;
1570 }
1571
1572 ws = get_current_workspace(shsurf->shell);
1573 wl_list_remove(&shsurf->surface->layer_link);
1574 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1575}
1576
Pekka Paalanen98262232011-12-01 10:42:22 +02001577static int
1578reset_shell_surface_type(struct shell_surface *surface)
1579{
1580 switch (surface->type) {
1581 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001582 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001583 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001584 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001585 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001586 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001587 case SHELL_SURFACE_NONE:
1588 case SHELL_SURFACE_TOPLEVEL:
1589 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001591 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001592 break;
1593 }
1594
1595 surface->type = SHELL_SURFACE_NONE;
1596 return 0;
1597}
1598
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001599static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001600set_surface_type(struct shell_surface *shsurf)
1601{
1602 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001603 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001604
1605 reset_shell_surface_type(shsurf);
1606
1607 shsurf->type = shsurf->next_type;
1608 shsurf->next_type = SHELL_SURFACE_NONE;
1609
1610 switch (shsurf->type) {
1611 case SHELL_SURFACE_TOPLEVEL:
1612 break;
1613 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001614 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001615 pes->geometry.x + shsurf->transient.x,
1616 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001617 break;
1618
1619 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001620 case SHELL_SURFACE_FULLSCREEN:
1621 shsurf->saved_x = surface->geometry.x;
1622 shsurf->saved_y = surface->geometry.y;
1623 shsurf->saved_position_valid = true;
1624
1625 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1626 wl_list_remove(&shsurf->rotation.transform.link);
1627 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001628 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001629 shsurf->saved_rotation_valid = true;
1630 }
1631 break;
1632
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001633 case SHELL_SURFACE_XWAYLAND:
1634 weston_surface_set_position(surface, shsurf->transient.x,
1635 shsurf->transient.y);
1636 break;
1637
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001638 default:
1639 break;
1640 }
1641}
1642
1643static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001644set_toplevel(struct shell_surface *shsurf)
1645{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001646 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001647}
1648
1649static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001650shell_surface_set_toplevel(struct wl_client *client,
1651 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001652{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001653 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001654
Tiago Vignattibc052c92012-04-19 16:18:18 +03001655 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001656}
1657
1658static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001659set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001660 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001661{
1662 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001663 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001664 shsurf->transient.x = x;
1665 shsurf->transient.y = y;
1666 shsurf->transient.flags = flags;
1667 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1668}
1669
1670static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001671shell_surface_set_transient(struct wl_client *client,
1672 struct wl_resource *resource,
1673 struct wl_resource *parent_resource,
1674 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001675{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001676 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001677 struct weston_surface *parent =
1678 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001679
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001680 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001681}
1682
Tiago Vignattibe143262012-04-16 17:31:41 +03001683static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001684shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001685{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001686 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001687}
1688
1689static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001690get_output_panel_height(struct desktop_shell *shell,
1691 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001692{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001693 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001694 int panel_height = 0;
1695
1696 if (!output)
1697 return 0;
1698
Juan Zhao4ab94682012-07-09 22:24:09 -07001699 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001700 if (surface->output == output) {
1701 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001702 break;
1703 }
1704 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001705
Juan Zhao96879df2012-02-07 08:45:41 +08001706 return panel_height;
1707}
1708
1709static void
1710shell_surface_set_maximized(struct wl_client *client,
1711 struct wl_resource *resource,
1712 struct wl_resource *output_resource )
1713{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001714 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001715 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001716 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001717 uint32_t edges = 0, panel_height = 0;
1718
1719 /* get the default output, if the client set it as NULL
1720 check whether the ouput is available */
1721 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001722 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001723 else if (es->output)
1724 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001725 else
1726 shsurf->output = get_default_output(es->compositor);
1727
Tiago Vignattibe143262012-04-16 17:31:41 +03001728 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001729 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001730 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001731
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001732 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001733 shsurf->output->width,
1734 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001735
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001736 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001737}
1738
Alex Wu21858432012-04-01 20:13:08 +08001739static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001740black_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 +08001741
Alex Wu4539b082012-03-01 12:57:46 +08001742static struct weston_surface *
1743create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001744 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001745 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001746{
1747 struct weston_surface *surface = NULL;
1748
1749 surface = weston_surface_create(ec);
1750 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001751 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001752 return NULL;
1753 }
1754
Alex Wu21858432012-04-01 20:13:08 +08001755 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001756 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001757 weston_surface_configure(surface, x, y, w, h);
1758 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001759 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001760 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001761 pixman_region32_fini(&surface->input);
1762 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001763
Alex Wu4539b082012-03-01 12:57:46 +08001764 return surface;
1765}
1766
1767/* Create black surface and append it to the associated fullscreen surface.
1768 * Handle size dismatch and positioning according to the method. */
1769static void
1770shell_configure_fullscreen(struct shell_surface *shsurf)
1771{
1772 struct weston_output *output = shsurf->fullscreen_output;
1773 struct weston_surface *surface = shsurf->surface;
1774 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001775 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001776 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001777
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001778 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1779 restore_output_mode(output);
1780
Alex Wu4539b082012-03-01 12:57:46 +08001781 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782 shsurf->fullscreen.black_surface =
1783 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001784 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001785 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001786 output->width,
1787 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001788
1789 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1790 wl_list_insert(&surface->layer_link,
1791 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001792 shsurf->fullscreen.black_surface->output = output;
1793
Giulio Camuffob8366642013-04-25 13:57:46 +03001794 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1795 &surf_width, &surf_height);
1796
Alex Wu4539b082012-03-01 12:57:46 +08001797 switch (shsurf->fullscreen.type) {
1798 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001799 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001800 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001801 break;
1802 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001803 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001804 if (output->width == surf_width &&
1805 output->height == surf_height) {
1806 weston_surface_set_position(surface, output->x - surf_x,
1807 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001808 break;
1809 }
1810
Alex Wu4539b082012-03-01 12:57:46 +08001811 matrix = &shsurf->fullscreen.transform.matrix;
1812 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001813
Scott Moreau1bad5db2012-08-18 01:04:05 -06001814 output_aspect = (float) output->width /
1815 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001816 surface_aspect = (float) surface->geometry.width /
1817 (float) surface->geometry.height;
1818 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001819 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001820 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001821 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001822 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001823 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001824
Alex Wu4539b082012-03-01 12:57:46 +08001825 weston_matrix_scale(matrix, scale, scale, 1);
1826 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001827 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001828 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001829 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1830 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001831 weston_surface_set_position(surface, x, y);
1832
Alex Wu4539b082012-03-01 12:57:46 +08001833 break;
1834 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001835 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001836 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001837 surf_width * surface->buffer_scale,
1838 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001839 shsurf->fullscreen.framerate};
1840
Alexander Larsson355748e2013-05-28 16:23:38 +02001841 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001842 weston_surface_set_position(surface,
1843 output->x - surf_x,
1844 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001845 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001846 output->x - surf_x,
1847 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001848 output->width,
1849 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001850 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001851 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001852 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001853 center_on_output(surface, output);
1854 }
Alex Wubd3354b2012-04-17 17:20:49 +08001855 }
Alex Wu4539b082012-03-01 12:57:46 +08001856 break;
1857 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001858 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001859 break;
1860 default:
1861 break;
1862 }
1863}
1864
1865/* make the fullscreen and black surface at the top */
1866static void
1867shell_stack_fullscreen(struct shell_surface *shsurf)
1868{
Alex Wubd3354b2012-04-17 17:20:49 +08001869 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001870 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001871 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001872
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001873 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001874 wl_list_insert(&shell->fullscreen_layer.surface_list,
1875 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001876 weston_surface_damage(surface);
1877
1878 if (!shsurf->fullscreen.black_surface)
1879 shsurf->fullscreen.black_surface =
1880 create_black_surface(surface->compositor,
1881 surface,
1882 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001883 output->width,
1884 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001885
1886 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001887 wl_list_insert(&surface->layer_link,
1888 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001889 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001890}
1891
1892static void
1893shell_map_fullscreen(struct shell_surface *shsurf)
1894{
Alex Wu4539b082012-03-01 12:57:46 +08001895 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001896 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001897}
1898
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001899static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001900set_fullscreen(struct shell_surface *shsurf,
1901 uint32_t method,
1902 uint32_t framerate,
1903 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001904{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001905 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001906
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001907 if (output)
1908 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001909 else if (es->output)
1910 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001911 else
1912 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001913
Alex Wu4539b082012-03-01 12:57:46 +08001914 shsurf->fullscreen_output = shsurf->output;
1915 shsurf->fullscreen.type = method;
1916 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001917 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001918
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001919 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001920 shsurf->output->width,
1921 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001922}
1923
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001924static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001925shell_surface_set_fullscreen(struct wl_client *client,
1926 struct wl_resource *resource,
1927 uint32_t method,
1928 uint32_t framerate,
1929 struct wl_resource *output_resource)
1930{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001931 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001932 struct weston_output *output;
1933
1934 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001935 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001936 else
1937 output = NULL;
1938
1939 set_fullscreen(shsurf, method, framerate, output);
1940}
1941
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001942static void
1943set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1944{
1945 /* XXX: using the same fields for transient type */
1946 shsurf->transient.x = x;
1947 shsurf->transient.y = y;
1948 shsurf->transient.flags = flags;
1949 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1950}
1951
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001952static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001953
1954static void
1955destroy_shell_seat(struct wl_listener *listener, void *data)
1956{
1957 struct shell_seat *shseat =
1958 container_of(listener,
1959 struct shell_seat, seat_destroy_listener);
1960 struct shell_surface *shsurf, *prev = NULL;
1961
1962 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001963 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001964 shseat->popup_grab.client = NULL;
1965
1966 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1967 shsurf->popup.shseat = NULL;
1968 if (prev) {
1969 wl_list_init(&prev->popup.grab_link);
1970 }
1971 prev = shsurf;
1972 }
1973 wl_list_init(&prev->popup.grab_link);
1974 }
1975
1976 wl_list_remove(&shseat->seat_destroy_listener.link);
1977 free(shseat);
1978}
1979
1980static struct shell_seat *
1981create_shell_seat(struct weston_seat *seat)
1982{
1983 struct shell_seat *shseat;
1984
1985 shseat = calloc(1, sizeof *shseat);
1986 if (!shseat) {
1987 weston_log("no memory to allocate shell seat\n");
1988 return NULL;
1989 }
1990
1991 shseat->seat = seat;
1992 wl_list_init(&shseat->popup_grab.surfaces_list);
1993
1994 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1995 wl_signal_add(&seat->destroy_signal,
1996 &shseat->seat_destroy_listener);
1997
1998 return shseat;
1999}
2000
2001static struct shell_seat *
2002get_shell_seat(struct weston_seat *seat)
2003{
2004 struct wl_listener *listener;
2005
2006 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2007 if (listener == NULL)
2008 return create_shell_seat(seat);
2009
2010 return container_of(listener,
2011 struct shell_seat, seat_destroy_listener);
2012}
2013
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002014static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002016{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002017 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002018 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002019 struct shell_seat *shseat =
2020 container_of(grab, struct shell_seat, popup_grab.grab);
2021 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002022 wl_fixed_t sx, sy;
2023
2024 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2025 pointer->x, pointer->y,
2026 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002027
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002028 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002029 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002030 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002031 weston_pointer_set_focus(pointer, NULL,
2032 wl_fixed_from_int(0),
2033 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002034 }
2035}
2036
2037static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002038popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002039{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002040 struct weston_pointer *pointer = grab->pointer;
2041 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002043 if (pointer->focus_resource) {
2044 weston_surface_from_global_fixed(pointer->focus,
2045 pointer->x, pointer->y,
2046 &sx, &sy);
2047 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2048 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002049}
2050
2051static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002052popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002053 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054{
2055 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002056 struct shell_seat *shseat =
2057 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002058 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002059 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002060 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002061
Daniel Stone37816df2012-05-16 18:45:18 +01002062 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002064 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002065 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002066 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002067 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002068 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002069 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002070 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002071 }
2072
Daniel Stone4dbadb12012-05-30 16:31:51 +01002073 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002074 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075}
2076
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002077static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002078 popup_grab_focus,
2079 popup_grab_motion,
2080 popup_grab_button,
2081};
2082
2083static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002084popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002085{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002086 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002087 struct shell_seat *shseat =
2088 container_of(grab, struct shell_seat, popup_grab.grab);
2089 struct shell_surface *shsurf;
2090 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002091
2092 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002093 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002094 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002095 shseat->popup_grab.grab.interface = NULL;
2096 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002097 /* Send the popup_done event to all the popups open */
2098 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002099 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002100 shsurf->popup.shseat = NULL;
2101 if (prev) {
2102 wl_list_init(&prev->popup.grab_link);
2103 }
2104 prev = shsurf;
2105 }
2106 wl_list_init(&prev->popup.grab_link);
2107 wl_list_init(&shseat->popup_grab.surfaces_list);
2108 }
2109}
2110
2111static void
2112add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2113{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002114 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002115
2116 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002117 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002118 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002119 /* We must make sure here that this popup was opened after
2120 * a mouse press, and not just by moving around with other
2121 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002122 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002123 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002124
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002125 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002126 }
2127 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2128}
2129
2130static void
2131remove_popup_grab(struct shell_surface *shsurf)
2132{
2133 struct shell_seat *shseat = shsurf->popup.shseat;
2134
2135 wl_list_remove(&shsurf->popup.grab_link);
2136 wl_list_init(&shsurf->popup.grab_link);
2137 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002138 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002139 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002140 }
2141}
2142
2143static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002144shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002145{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002146 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002148 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149
2150 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151
Pekka Paalanen483243f2013-03-08 14:56:50 +02002152 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002153 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2154 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002155
Kristian Høgsberge3148752013-05-06 23:19:49 -04002156 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002157 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002158 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002159 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002160 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002161 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002162}
2163
2164static void
2165shell_surface_set_popup(struct wl_client *client,
2166 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002167 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002168 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002169 struct wl_resource *parent_resource,
2170 int32_t x, int32_t y, uint32_t flags)
2171{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002172 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002173
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002174 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002175 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002176 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002177 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002178 shsurf->popup.x = x;
2179 shsurf->popup.y = y;
2180}
2181
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002182static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002183 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002184 shell_surface_move,
2185 shell_surface_resize,
2186 shell_surface_set_toplevel,
2187 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002188 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002189 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002190 shell_surface_set_maximized,
2191 shell_surface_set_title,
2192 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002193};
2194
2195static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002196destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002197{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002198 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2199
Giulio Camuffo5085a752013-03-25 21:42:45 +01002200 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2201 remove_popup_grab(shsurf);
2202 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002203
Alex Wubd3354b2012-04-17 17:20:49 +08002204 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002205 shell_surface_is_top_fullscreen(shsurf))
2206 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002207
Alex Wuaa08e2d2012-03-05 11:01:40 +08002208 if (shsurf->fullscreen.black_surface)
2209 weston_surface_destroy(shsurf->fullscreen.black_surface);
2210
Alex Wubd3354b2012-04-17 17:20:49 +08002211 /* As destroy_resource() use wl_list_for_each_safe(),
2212 * we can always remove the listener.
2213 */
2214 wl_list_remove(&shsurf->surface_destroy_listener.link);
2215 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002216 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002217 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002218
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002219 wl_list_remove(&shsurf->link);
2220 free(shsurf);
2221}
2222
2223static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002224shell_destroy_shell_surface(struct wl_resource *resource)
2225{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002226 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002227
2228 destroy_shell_surface(shsurf);
2229}
2230
2231static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002232shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002233{
2234 struct shell_surface *shsurf = container_of(listener,
2235 struct shell_surface,
2236 surface_destroy_listener);
2237
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002238 if (shsurf->resource)
2239 wl_resource_destroy(shsurf->resource);
2240 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002241 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002242}
2243
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002244static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002245shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002246
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002247static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002248get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002249{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002250 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002251 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002252 else
2253 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002254}
2255
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002256static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002257create_shell_surface(void *shell, struct weston_surface *surface,
2258 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002259{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002260 struct shell_surface *shsurf;
2261
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002262 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002263 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002264 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002265 }
2266
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002267 shsurf = calloc(1, sizeof *shsurf);
2268 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002269 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002270 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002271 }
2272
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002273 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002274 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002275
Tiago Vignattibc052c92012-04-19 16:18:18 +03002276 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002277 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002278 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002279 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002280 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002281 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2282 shsurf->fullscreen.framerate = 0;
2283 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002284 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002285 wl_list_init(&shsurf->fullscreen.transform.link);
2286
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002287 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002288 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002289 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002290 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002291
2292 /* init link so its safe to always remove it in destroy_shell_surface */
2293 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002294 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002295
Pekka Paalanen460099f2012-01-20 16:48:25 +02002296 /* empty when not in use */
2297 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002298 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002299
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002300 wl_list_init(&shsurf->workspace_transform.link);
2301
Pekka Paalanen98262232011-12-01 10:42:22 +02002302 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002303 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002304
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002305 shsurf->client = client;
2306
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002307 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002308}
2309
2310static void
2311shell_get_shell_surface(struct wl_client *client,
2312 struct wl_resource *resource,
2313 uint32_t id,
2314 struct wl_resource *surface_resource)
2315{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002316 struct weston_surface *surface =
2317 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002318 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002319 struct shell_surface *shsurf;
2320
2321 if (get_shell_surface(surface)) {
2322 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002323 WL_DISPLAY_ERROR_INVALID_OBJECT,
2324 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002325 return;
2326 }
2327
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002328 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002329 if (!shsurf) {
2330 wl_resource_post_error(surface_resource,
2331 WL_DISPLAY_ERROR_INVALID_OBJECT,
2332 "surface->configure already set");
2333 return;
2334 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002335
Jason Ekstranda85118c2013-06-27 20:17:02 -05002336 shsurf->resource =
2337 wl_resource_create(client,
2338 &wl_shell_surface_interface, 1, id);
2339 wl_resource_set_implementation(shsurf->resource,
2340 &shell_surface_implementation,
2341 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002342}
2343
2344static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002345 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002346};
2347
Kristian Høgsberg07937562011-04-12 17:25:42 -04002348static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002349shell_fade(struct desktop_shell *shell, enum fade_type type);
2350
2351static int
2352screensaver_timeout(void *data)
2353{
2354 struct desktop_shell *shell = data;
2355
2356 shell_fade(shell, FADE_OUT);
2357
2358 return 1;
2359}
2360
2361static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002362handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002363{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002364 struct desktop_shell *shell =
2365 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002366
Pekka Paalanen18027e52011-12-02 16:31:49 +02002367 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002368
2369 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002370 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002371}
2372
2373static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002374launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002375{
2376 if (shell->screensaver.binding)
2377 return;
2378
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002379 if (!shell->screensaver.path) {
2380 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002381 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002382 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002383
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002384 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002385 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002386 return;
2387 }
2388
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002389 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002390 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002391 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002392 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002393}
2394
2395static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002396terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002397{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002398 if (shell->screensaver.process.pid == 0)
2399 return;
2400
2401 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002402}
2403
2404static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002405configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002406{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002407 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002408
Giulio Camuffo184df502013-02-21 11:29:21 +01002409 if (width == 0)
2410 return;
2411
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002412 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2413 if (s->output == es->output && s != es) {
2414 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002415 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002416 }
2417 }
2418
Giulio Camuffo184df502013-02-21 11:29:21 +01002419 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002420
2421 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002422 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002423 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002424 }
2425}
2426
2427static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002428background_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 -04002429{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002430 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002431
Giulio Camuffo184df502013-02-21 11:29:21 +01002432 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002433}
2434
2435static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002436desktop_shell_set_background(struct wl_client *client,
2437 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002438 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002439 struct wl_resource *surface_resource)
2440{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002441 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002442 struct weston_surface *surface =
2443 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002444
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002445 if (surface->configure) {
2446 wl_resource_post_error(surface_resource,
2447 WL_DISPLAY_ERROR_INVALID_OBJECT,
2448 "surface role already assigned");
2449 return;
2450 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002451
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002452 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002453 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002454 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002455 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002456 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002457 surface->output->width,
2458 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002459}
2460
2461static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002462panel_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 -04002463{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002464 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002465
Giulio Camuffo184df502013-02-21 11:29:21 +01002466 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002467}
2468
2469static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002470desktop_shell_set_panel(struct wl_client *client,
2471 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002472 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002473 struct wl_resource *surface_resource)
2474{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002475 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002476 struct weston_surface *surface =
2477 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002478
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002479 if (surface->configure) {
2480 wl_resource_post_error(surface_resource,
2481 WL_DISPLAY_ERROR_INVALID_OBJECT,
2482 "surface role already assigned");
2483 return;
2484 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002485
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002486 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002487 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002488 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002489 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002490 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002491 surface->output->width,
2492 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002493}
2494
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002495static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002496lock_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 -04002497{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002498 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002499
Giulio Camuffo184df502013-02-21 11:29:21 +01002500 if (width == 0)
2501 return;
2502
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002503 surface->geometry.width = width;
2504 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002505 center_on_output(surface, get_default_output(shell->compositor));
2506
2507 if (!weston_surface_is_mapped(surface)) {
2508 wl_list_insert(&shell->lock_layer.surface_list,
2509 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002510 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002511 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002512 }
2513}
2514
2515static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002516handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002517{
Tiago Vignattibe143262012-04-16 17:31:41 +03002518 struct desktop_shell *shell =
2519 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002520
Martin Minarik6d118362012-06-07 18:01:59 +02002521 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002522 shell->lock_surface = NULL;
2523}
2524
2525static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002526desktop_shell_set_lock_surface(struct wl_client *client,
2527 struct wl_resource *resource,
2528 struct wl_resource *surface_resource)
2529{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002530 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002531 struct weston_surface *surface =
2532 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002533
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002534 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002535
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002536 if (!shell->locked)
2537 return;
2538
Pekka Paalanen98262232011-12-01 10:42:22 +02002539 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002540
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002541 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002542 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002543 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002544
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002545 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002546 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002547}
2548
2549static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002550resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002551{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002552 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002553
Pekka Paalanen77346a62011-11-30 16:26:35 +02002554 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002555
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002556 wl_list_remove(&shell->lock_layer.link);
2557 wl_list_insert(&shell->compositor->cursor_layer.link,
2558 &shell->fullscreen_layer.link);
2559 wl_list_insert(&shell->fullscreen_layer.link,
2560 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002561 if (shell->showing_input_panels) {
2562 wl_list_insert(&shell->panel_layer.link,
2563 &shell->input_panel_layer.link);
2564 wl_list_insert(&shell->input_panel_layer.link,
2565 &ws->layer.link);
2566 } else {
2567 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2568 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002569
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002570 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002571
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002572 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002573 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002574 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002575}
2576
2577static void
2578desktop_shell_unlock(struct wl_client *client,
2579 struct wl_resource *resource)
2580{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002581 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002582
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002583 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002584
2585 if (shell->locked)
2586 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002587}
2588
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002589static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002590desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002591 struct wl_resource *resource,
2592 struct wl_resource *surface_resource)
2593{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002594 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002595
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002596 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002597}
2598
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002599static void
2600desktop_shell_desktop_ready(struct wl_client *client,
2601 struct wl_resource *resource)
2602{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002603 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002604
2605 shell_fade_startup(shell);
2606}
2607
Kristian Høgsberg75840622011-09-06 13:48:16 -04002608static const struct desktop_shell_interface desktop_shell_implementation = {
2609 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002610 desktop_shell_set_panel,
2611 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002612 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002613 desktop_shell_set_grab_surface,
2614 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002615};
2616
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002617static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002618get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002619{
2620 struct shell_surface *shsurf;
2621
2622 shsurf = get_shell_surface(surface);
2623 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002624 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002625 return shsurf->type;
2626}
2627
Kristian Høgsberg75840622011-09-06 13:48:16 -04002628static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002629move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002630{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002631 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002632 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002633 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002634 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002635
Pekka Paalanen01388e22013-04-25 13:57:44 +03002636 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002637 if (surface == NULL)
2638 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002639
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002640 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002641 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2642 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002643 return;
2644
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002645 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002646}
2647
2648static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002649resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002650{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002651 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002652 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002653 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002654 uint32_t edges = 0;
2655 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002656 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002657
Pekka Paalanen01388e22013-04-25 13:57:44 +03002658 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002659 if (surface == NULL)
2660 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002661
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002662 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002663 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2664 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002665 return;
2666
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002667 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002668 wl_fixed_to_int(seat->pointer->grab_x),
2669 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002670 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002671
Pekka Paalanen60921e52012-01-25 15:55:43 +02002672 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002673 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002674 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002675 edges |= 0;
2676 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002677 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002678
Pekka Paalanen60921e52012-01-25 15:55:43 +02002679 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002680 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002681 else if (y < 2 * surface->geometry.height / 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_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002685
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002686 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002687}
2688
2689static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002690surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002691 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002692{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002693 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002694 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002695 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002696 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002697 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002698
Pekka Paalanen01388e22013-04-25 13:57:44 +03002699 /* XXX: broken for windows containing sub-surfaces */
2700 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002701 if (surface == NULL)
2702 return;
2703
2704 shsurf = get_shell_surface(surface);
2705 if (!shsurf)
2706 return;
2707
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002708 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002709
Scott Moreau02709af2012-05-22 01:54:10 -06002710 if (surface->alpha > 1.0)
2711 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002712 if (surface->alpha < step)
2713 surface->alpha = step;
2714
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002715 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002716 weston_surface_damage(surface);
2717}
2718
2719static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002720do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002721 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002722{
Daniel Stone37816df2012-05-16 18:45:18 +01002723 struct weston_seat *ws = (struct weston_seat *) seat;
2724 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002725 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002726 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002727
2728 wl_list_for_each(output, &compositor->output_list, link) {
2729 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002730 wl_fixed_to_double(seat->pointer->x),
2731 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002732 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002733 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002734 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002735 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002736 increment = -output->zoom.increment;
2737 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002738 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002739 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002740 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002741 else
2742 increment = 0;
2743
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002744 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002745
Scott Moreaue6603982012-06-11 13:07:51 -06002746 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002747 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002748 else if (output->zoom.level > output->zoom.max_level)
2749 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002750 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002751 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002752 output->disable_planes++;
2753 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002754
Scott Moreaue6603982012-06-11 13:07:51 -06002755 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002756
Scott Moreau8dacaab2012-06-17 18:10:58 -06002757 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002758 }
2759 }
2760}
2761
Scott Moreauccbf29d2012-02-22 14:21:41 -07002762static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002763zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002764 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002765{
2766 do_zoom(seat, time, 0, axis, value);
2767}
2768
2769static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002770zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002771 void *data)
2772{
2773 do_zoom(seat, time, key, 0, 0);
2774}
2775
2776static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002777terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002778 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002779{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002780 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002781
Daniel Stone325fc2d2012-05-30 16:31:58 +01002782 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002783}
2784
2785static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002786rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002787{
2788 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002789 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002790 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002791 struct shell_surface *shsurf = rotate->base.shsurf;
2792 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002793 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002794
2795 if (!shsurf)
2796 return;
2797
2798 surface = shsurf->surface;
2799
2800 cx = 0.5f * surface->geometry.width;
2801 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002802
Daniel Stone37816df2012-05-16 18:45:18 +01002803 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2804 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002805 r = sqrtf(dx * dx + dy * dy);
2806
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002807 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002808 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002809
2810 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002811 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002812 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002813
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002814 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002815 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002816
2817 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002818 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002819 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002820 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002821 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002822
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002823 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002824 &shsurf->surface->geometry.transformation_list,
2825 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002826 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002827 wl_list_init(&shsurf->rotation.transform.link);
2828 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002829 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002830 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002831
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002832 /* We need to adjust the position of the surface
2833 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002834 cposx = surface->geometry.x + cx;
2835 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002836 dposx = rotate->center.x - cposx;
2837 dposy = rotate->center.y - cposy;
2838 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002839 weston_surface_set_position(surface,
2840 surface->geometry.x + dposx,
2841 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002842 }
2843
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002844 /* Repaint implies weston_surface_update_transform(), which
2845 * lazily applies the damage due to rotation update.
2846 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002847 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002848}
2849
2850static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002851rotate_grab_button(struct weston_pointer_grab *grab,
2852 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002853{
2854 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002855 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002856 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002857 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002858 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002859
Daniel Stone4dbadb12012-05-30 16:31:51 +01002860 if (pointer->button_count == 0 &&
2861 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002862 if (shsurf)
2863 weston_matrix_multiply(&shsurf->rotation.rotation,
2864 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002865 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002866 free(rotate);
2867 }
2868}
2869
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002870static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002871 noop_grab_focus,
2872 rotate_grab_motion,
2873 rotate_grab_button,
2874};
2875
2876static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002877surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002878{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002879 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002880 float dx, dy;
2881 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002882
Pekka Paalanen460099f2012-01-20 16:48:25 +02002883 rotate = malloc(sizeof *rotate);
2884 if (!rotate)
2885 return;
2886
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002887 weston_surface_to_global_float(surface->surface,
2888 surface->surface->geometry.width / 2,
2889 surface->surface->geometry.height / 2,
2890 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002891
Daniel Stone37816df2012-05-16 18:45:18 +01002892 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2893 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002894 r = sqrtf(dx * dx + dy * dy);
2895 if (r > 20.0f) {
2896 struct weston_matrix inverse;
2897
2898 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002899 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002900 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002901
2902 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002903 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002904 } else {
2905 weston_matrix_init(&surface->rotation.rotation);
2906 weston_matrix_init(&rotate->rotation);
2907 }
2908
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002909 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2910 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002911}
2912
2913static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002914rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002915 void *data)
2916{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002917 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002918 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002919 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002920 struct shell_surface *surface;
2921
Pekka Paalanen01388e22013-04-25 13:57:44 +03002922 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002923 if (base_surface == NULL)
2924 return;
2925
2926 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002927 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2928 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002929 return;
2930
2931 surface_rotate(surface, seat);
2932}
2933
2934static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002935lower_fullscreen_layer(struct desktop_shell *shell)
2936{
2937 struct workspace *ws;
2938 struct weston_surface *surface, *prev;
2939
2940 ws = get_current_workspace(shell);
2941 wl_list_for_each_reverse_safe(surface, prev,
2942 &shell->fullscreen_layer.surface_list,
2943 layer_link)
2944 weston_surface_restack(surface, &ws->layer.surface_list);
2945}
2946
2947static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002948activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002949 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002950{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002951 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002952 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002953 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002954
Pekka Paalanen01388e22013-04-25 13:57:44 +03002955 main_surface = weston_surface_get_main_surface(es);
2956
Daniel Stone37816df2012-05-16 18:45:18 +01002957 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002958
Jonas Ådahl8538b222012-08-29 22:13:03 +02002959 state = ensure_focus_state(shell, seat);
2960 if (state == NULL)
2961 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002962
2963 state->keyboard_focus = es;
2964 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002965 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002966
Pekka Paalanen01388e22013-04-25 13:57:44 +03002967 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002968 case SHELL_SURFACE_FULLSCREEN:
2969 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002970 shell_stack_fullscreen(get_shell_surface(main_surface));
2971 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002972 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002973 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002974 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002975 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002976 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002977 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002978 }
2979}
2980
Alex Wu21858432012-04-01 20:13:08 +08002981/* no-op func for checking black surface */
2982static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002983black_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 +08002984{
2985}
2986
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002987static bool
Alex Wu21858432012-04-01 20:13:08 +08002988is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2989{
2990 if (es->configure == black_surface_configure) {
2991 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002992 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002993 return true;
2994 }
2995 return false;
2996}
2997
Kristian Høgsberg75840622011-09-06 13:48:16 -04002998static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002999click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003000 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003001{
Daniel Stone37816df2012-05-16 18:45:18 +01003002 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003003 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003004 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003005 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003006
Daniel Stone37816df2012-05-16 18:45:18 +01003007 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003008 if (!focus)
3009 return;
3010
Pekka Paalanen01388e22013-04-25 13:57:44 +03003011 if (is_black_surface(focus, &main_surface))
3012 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003013
Pekka Paalanen01388e22013-04-25 13:57:44 +03003014 main_surface = weston_surface_get_main_surface(focus);
3015 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003016 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003017
Daniel Stone325fc2d2012-05-30 16:31:58 +01003018 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003019 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003020}
3021
3022static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003023lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003024{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003025 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003026
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003027 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003028 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003029 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003030 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003031
3032 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003033
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003034 /* Hide all surfaces by removing the fullscreen, panel and
3035 * toplevel layers. This way nothing else can show or receive
3036 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003037
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003038 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003039 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003040 if (shell->showing_input_panels)
3041 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003042 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003043 wl_list_insert(&shell->compositor->cursor_layer.link,
3044 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003045
Pekka Paalanen77346a62011-11-30 16:26:35 +02003046 launch_screensaver(shell);
3047
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003048 /* TODO: disable bindings that should not work while locked. */
3049
3050 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003051}
3052
3053static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003054unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003055{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003056 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003057 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003058 return;
3059 }
3060
3061 /* If desktop-shell client has gone away, unlock immediately. */
3062 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003063 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003064 return;
3065 }
3066
3067 if (shell->prepare_event_sent)
3068 return;
3069
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003070 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003071 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003072}
3073
3074static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003075shell_fade_done(struct weston_surface_animation *animation, void *data)
3076{
3077 struct desktop_shell *shell = data;
3078
3079 shell->fade.animation = NULL;
3080
3081 switch (shell->fade.type) {
3082 case FADE_IN:
3083 weston_surface_destroy(shell->fade.surface);
3084 shell->fade.surface = NULL;
3085 break;
3086 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003087 lock(shell);
3088 break;
3089 }
3090}
3091
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003092static struct weston_surface *
3093shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003094{
3095 struct weston_compositor *compositor = shell->compositor;
3096 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003097
3098 surface = weston_surface_create(compositor);
3099 if (!surface)
3100 return NULL;
3101
3102 weston_surface_configure(surface, 0, 0, 8192, 8192);
3103 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3104 wl_list_insert(&compositor->fade_layer.surface_list,
3105 &surface->layer_link);
3106 pixman_region32_init(&surface->input);
3107
3108 return surface;
3109}
3110
3111static void
3112shell_fade(struct desktop_shell *shell, enum fade_type type)
3113{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003114 float tint;
3115
3116 switch (type) {
3117 case FADE_IN:
3118 tint = 0.0;
3119 break;
3120 case FADE_OUT:
3121 tint = 1.0;
3122 break;
3123 default:
3124 weston_log("shell: invalid fade type\n");
3125 return;
3126 }
3127
3128 shell->fade.type = type;
3129
3130 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003131 shell->fade.surface = shell_fade_create_surface(shell);
3132 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003133 return;
3134
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003135 shell->fade.surface->alpha = 1.0 - tint;
3136 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003137 }
3138
3139 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003140 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003141 else
3142 shell->fade.animation =
3143 weston_fade_run(shell->fade.surface,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003144 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003145 shell_fade_done, shell);
3146}
3147
3148static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003149do_shell_fade_startup(void *data)
3150{
3151 struct desktop_shell *shell = data;
3152
3153 shell_fade(shell, FADE_IN);
3154}
3155
3156static void
3157shell_fade_startup(struct desktop_shell *shell)
3158{
3159 struct wl_event_loop *loop;
3160
3161 if (!shell->fade.startup_timer)
3162 return;
3163
3164 wl_event_source_remove(shell->fade.startup_timer);
3165 shell->fade.startup_timer = NULL;
3166
3167 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3168 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3169}
3170
3171static int
3172fade_startup_timeout(void *data)
3173{
3174 struct desktop_shell *shell = data;
3175
3176 shell_fade_startup(shell);
3177 return 0;
3178}
3179
3180static void
3181shell_fade_init(struct desktop_shell *shell)
3182{
3183 /* Make compositor output all black, and wait for the desktop-shell
3184 * client to signal it is ready, then fade in. The timer triggers a
3185 * fade-in, in case the desktop-shell client takes too long.
3186 */
3187
3188 struct wl_event_loop *loop;
3189
3190 if (shell->fade.surface != NULL) {
3191 weston_log("%s: warning: fade surface already exists\n",
3192 __func__);
3193 return;
3194 }
3195
3196 shell->fade.surface = shell_fade_create_surface(shell);
3197 if (!shell->fade.surface)
3198 return;
3199
3200 weston_surface_update_transform(shell->fade.surface);
3201 weston_surface_damage(shell->fade.surface);
3202
3203 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3204 shell->fade.startup_timer =
3205 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3206 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3207}
3208
3209static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003210idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003211{
3212 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003213 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003214
3215 shell_fade(shell, FADE_OUT);
3216 /* lock() is called from shell_fade_done() */
3217}
3218
3219static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003220wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003221{
3222 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003223 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003224
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003225 unlock(shell);
3226}
3227
3228static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003229show_input_panels(struct wl_listener *listener, void *data)
3230{
3231 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003232 container_of(listener, struct desktop_shell,
3233 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003234 struct input_panel_surface *surface, *next;
3235 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003236
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003237 shell->text_input.surface = (struct weston_surface*)data;
3238
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003239 if (shell->showing_input_panels)
3240 return;
3241
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003242 shell->showing_input_panels = true;
3243
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003244 if (!shell->locked)
3245 wl_list_insert(&shell->panel_layer.link,
3246 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003247
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003248 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003249 &shell->input_panel.surfaces, link) {
3250 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003251 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003252 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003253 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003254 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003255 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003256 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003257 weston_surface_damage(ws);
3258 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003259 }
3260}
3261
3262static void
3263hide_input_panels(struct wl_listener *listener, void *data)
3264{
3265 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003266 container_of(listener, struct desktop_shell,
3267 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003268 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003269
Jan Arne Petersen61381972013-01-31 15:52:21 +01003270 if (!shell->showing_input_panels)
3271 return;
3272
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003273 shell->showing_input_panels = false;
3274
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003275 if (!shell->locked)
3276 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003277
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003278 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003279 &shell->input_panel_layer.surface_list, layer_link)
3280 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003281}
3282
3283static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003284update_input_panels(struct wl_listener *listener, void *data)
3285{
3286 struct desktop_shell *shell =
3287 container_of(listener, struct desktop_shell,
3288 update_input_panel_listener);
3289
3290 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3291}
3292
3293static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003294center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003295{
Giulio Camuffob8366642013-04-25 13:57:46 +03003296 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003297 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003298
Giulio Camuffob8366642013-04-25 13:57:46 +03003299 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3300
3301 x = output->x + (output->width - width) / 2 - surf_x / 2;
3302 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003303
3304 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003305}
3306
3307static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003308weston_surface_set_initial_position (struct weston_surface *surface,
3309 struct desktop_shell *shell)
3310{
3311 struct weston_compositor *compositor = shell->compositor;
3312 int ix = 0, iy = 0;
3313 int range_x, range_y;
3314 int dx, dy, x, y, panel_height;
3315 struct weston_output *output, *target_output = NULL;
3316 struct weston_seat *seat;
3317
3318 /* As a heuristic place the new window on the same output as the
3319 * pointer. Falling back to the output containing 0, 0.
3320 *
3321 * TODO: Do something clever for touch too?
3322 */
3323 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003324 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003325 ix = wl_fixed_to_int(seat->pointer->x);
3326 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003327 break;
3328 }
3329 }
3330
3331 wl_list_for_each(output, &compositor->output_list, link) {
3332 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3333 target_output = output;
3334 break;
3335 }
3336 }
3337
3338 if (!target_output) {
3339 weston_surface_set_position(surface, 10 + random() % 400,
3340 10 + random() % 400);
3341 return;
3342 }
3343
3344 /* Valid range within output where the surface will still be onscreen.
3345 * If this is negative it means that the surface is bigger than
3346 * output.
3347 */
3348 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003349 range_x = target_output->width - surface->geometry.width;
3350 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003351 surface->geometry.height;
3352
Rob Bradford4cb88c72012-08-13 15:18:44 +01003353 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003354 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003355 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003356 dx = 0;
3357
3358 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003359 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003360 else
3361 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003362
3363 x = target_output->x + dx;
3364 y = target_output->y + dy;
3365
3366 weston_surface_set_position (surface, x, y);
3367}
3368
3369static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003370map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003371 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003372{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003373 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003374 struct shell_surface *shsurf = get_shell_surface(surface);
3375 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003376 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003377 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003378 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003379 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003380 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003381
Pekka Paalanen60921e52012-01-25 15:55:43 +02003382 surface->geometry.width = width;
3383 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003384 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003385
3386 /* initial positioning, see also configure() */
3387 switch (surface_type) {
3388 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003389 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003390 break;
Alex Wu4539b082012-03-01 12:57:46 +08003391 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003392 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003393 shell_map_fullscreen(shsurf);
3394 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003395 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003396 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003397 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003398 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3399 NULL, NULL);
3400 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3401 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003402 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003403 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003404 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003405 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003406 case SHELL_SURFACE_NONE:
3407 weston_surface_set_position(surface,
3408 surface->geometry.x + sx,
3409 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003410 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003411 default:
3412 ;
3413 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003414
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003415 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003416 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003417 case SHELL_SURFACE_POPUP:
3418 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003419 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003420 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3421 break;
Alex Wu4539b082012-03-01 12:57:46 +08003422 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003423 case SHELL_SURFACE_NONE:
3424 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003425 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003426 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003427 ws = get_current_workspace(shell);
3428 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003429 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003430 }
3431
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003432 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003433 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003434 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3435 surface->output = shsurf->output;
3436 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003437
Juan Zhao7bb92f02011-12-15 11:31:51 -05003438 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003439 /* XXX: xwayland's using the same fields for transient type */
3440 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003441 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003442 if (shsurf->transient.flags ==
3443 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3444 break;
3445 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003446 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003447 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003448 if (!shell->locked) {
3449 wl_list_for_each(seat, &compositor->seat_list, link)
3450 activate(shell, surface, seat);
3451 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003452 break;
3453 default:
3454 break;
3455 }
3456
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003457 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003458 {
3459 switch (shell->win_animation_type) {
3460 case ANIMATION_FADE:
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003461 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003462 break;
3463 case ANIMATION_ZOOM:
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -04003464 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003465 break;
3466 default:
3467 break;
3468 }
3469 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003470}
3471
3472static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003473configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003474 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003475{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003476 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3477 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003478 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003479
Pekka Paalanen77346a62011-11-30 16:26:35 +02003480 shsurf = get_shell_surface(surface);
3481 if (shsurf)
3482 surface_type = shsurf->type;
3483
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003484 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003485
3486 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003487 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003488 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003489 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003490 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003491 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003492 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003493 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3494 NULL, NULL);
3495 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003496 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003497 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003498 break;
Alex Wu4539b082012-03-01 12:57:46 +08003499 case SHELL_SURFACE_TOPLEVEL:
3500 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003501 default:
3502 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003503 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003504
Alex Wu4539b082012-03-01 12:57:46 +08003505 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003506 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003507 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003508
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003509 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003510 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003511 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003512}
3513
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003514static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003515shell_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 +03003516{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003517 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003518 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003519
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003520 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003521
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003522 if (!weston_surface_is_mapped(es) &&
3523 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003524 remove_popup_grab(shsurf);
3525 }
3526
Giulio Camuffo184df502013-02-21 11:29:21 +01003527 if (width == 0)
3528 return;
3529
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003530 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003531 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003532 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003533 type_changed = 1;
3534 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003535
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003536 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003537 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003538 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003539 es->geometry.width != width ||
3540 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003541 float from_x, from_y;
3542 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003543
3544 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3545 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3546 configure(shell, es,
3547 es->geometry.x + to_x - from_x,
3548 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003549 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003550 }
3551}
3552
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003553static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003554
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003555static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003556desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003557{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003558 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003559 struct desktop_shell *shell =
3560 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003561
3562 shell->child.process.pid = 0;
3563 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003564
3565 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3566 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003567 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003568 shell->child.deathstamp = time;
3569 shell->child.deathcount = 0;
3570 }
3571
3572 shell->child.deathcount++;
3573 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003574 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003575 return;
3576 }
3577
Martin Minarik6d118362012-06-07 18:01:59 +02003578 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003579 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003580 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003581}
3582
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003583static void
3584launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003585{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003586 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003587 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003588
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003589 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003590 &shell->child.process,
3591 shell_exe,
3592 desktop_shell_sigchld);
3593
3594 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003595 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003596}
3597
3598static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003599bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3600{
Tiago Vignattibe143262012-04-16 17:31:41 +03003601 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003602 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003603
Jason Ekstranda85118c2013-06-27 20:17:02 -05003604 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3605 if (resource)
3606 wl_resource_set_implementation(resource, &shell_implementation,
3607 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003608}
3609
Kristian Høgsberg75840622011-09-06 13:48:16 -04003610static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003611unbind_desktop_shell(struct wl_resource *resource)
3612{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003613 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003614
3615 if (shell->locked)
3616 resume_desktop(shell);
3617
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003618 shell->child.desktop_shell = NULL;
3619 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003620}
3621
3622static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003623bind_desktop_shell(struct wl_client *client,
3624 void *data, uint32_t version, uint32_t id)
3625{
Tiago Vignattibe143262012-04-16 17:31:41 +03003626 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003627 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003628
Jason Ekstranda85118c2013-06-27 20:17:02 -05003629 resource = wl_resource_create(client, &desktop_shell_interface,
3630 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003631
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003632 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003633 wl_resource_set_implementation(resource,
3634 &desktop_shell_implementation,
3635 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003636 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003637
3638 if (version < 2)
3639 shell_fade_startup(shell);
3640
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003641 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003642 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003643
3644 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3645 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003646 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003647}
3648
Pekka Paalanen6e168112011-11-24 11:34:05 +02003649static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003650screensaver_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 -04003651{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003652 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003653
Giulio Camuffo184df502013-02-21 11:29:21 +01003654 if (width == 0)
3655 return;
3656
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003657 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003658 if (!shell->locked)
3659 return;
3660
3661 center_on_output(surface, surface->output);
3662
3663 if (wl_list_empty(&surface->layer_link)) {
3664 wl_list_insert(shell->lock_layer.surface_list.prev,
3665 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003666 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003667 wl_event_source_timer_update(shell->screensaver.timer,
3668 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003669 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003670 }
3671}
3672
3673static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003674screensaver_set_surface(struct wl_client *client,
3675 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003676 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003677 struct wl_resource *output_resource)
3678{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003679 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003680 struct weston_surface *surface =
3681 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003682 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003683
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003684 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003685 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003686 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003687}
3688
3689static const struct screensaver_interface screensaver_implementation = {
3690 screensaver_set_surface
3691};
3692
3693static void
3694unbind_screensaver(struct wl_resource *resource)
3695{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003696 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003697
Pekka Paalanen77346a62011-11-30 16:26:35 +02003698 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003699}
3700
3701static void
3702bind_screensaver(struct wl_client *client,
3703 void *data, uint32_t version, uint32_t id)
3704{
Tiago Vignattibe143262012-04-16 17:31:41 +03003705 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003706 struct wl_resource *resource;
3707
Jason Ekstranda85118c2013-06-27 20:17:02 -05003708 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003709
Pekka Paalanen77346a62011-11-30 16:26:35 +02003710 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003711 wl_resource_set_implementation(resource,
3712 &screensaver_implementation,
3713 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003714 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003715 return;
3716 }
3717
3718 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3719 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003720 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003721}
3722
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003723static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003724input_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 -04003725{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003726 struct input_panel_surface *ip_surface = surface->configure_private;
3727 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003728 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003729 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003730 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003731
Giulio Camuffo184df502013-02-21 11:29:21 +01003732 if (width == 0)
3733 return;
3734
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003735 if (!weston_surface_is_mapped(surface)) {
3736 if (!shell->showing_input_panels)
3737 return;
3738
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003739 show_surface = 1;
3740 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003741
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003742 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003743
3744 if (ip_surface->panel) {
3745 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3746 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3747 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003748 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003749
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003750 x = ip_surface->output->x + (mode->width - width) / 2;
3751 y = ip_surface->output->y + mode->height - height;
3752 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003753
3754 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003755 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003756 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003757
3758 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003759 wl_list_insert(&shell->input_panel_layer.surface_list,
3760 &surface->layer_link);
3761 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003762 weston_surface_damage(surface);
3763 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3764 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003765}
3766
3767static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003768destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003769{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003770 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3771
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003772 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003773 wl_list_remove(&input_panel_surface->link);
3774
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003775 input_panel_surface->surface->configure = NULL;
3776
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003777 free(input_panel_surface);
3778}
3779
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003780static struct input_panel_surface *
3781get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003782{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003783 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003784 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003785 } else {
3786 return NULL;
3787 }
3788}
3789
3790static void
3791input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3792{
3793 struct input_panel_surface *ipsurface = container_of(listener,
3794 struct input_panel_surface,
3795 surface_destroy_listener);
3796
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003797 if (ipsurface->resource) {
3798 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003799 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003800 destroy_input_panel_surface(ipsurface);
3801 }
3802}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003803
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003804static struct input_panel_surface *
3805create_input_panel_surface(struct desktop_shell *shell,
3806 struct weston_surface *surface)
3807{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003808 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003809
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003810 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3811 if (!input_panel_surface)
3812 return NULL;
3813
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003814 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003815 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003816
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003817 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003818
3819 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003820
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003821 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003822 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003823 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003824 &input_panel_surface->surface_destroy_listener);
3825
3826 wl_list_init(&input_panel_surface->link);
3827
3828 return input_panel_surface;
3829}
3830
3831static void
3832input_panel_surface_set_toplevel(struct wl_client *client,
3833 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003834 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003835 uint32_t position)
3836{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003837 struct input_panel_surface *input_panel_surface =
3838 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003839 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003840
3841 wl_list_insert(&shell->input_panel.surfaces,
3842 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003843
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003844 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003845 input_panel_surface->panel = 0;
3846}
3847
3848static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003849input_panel_surface_set_overlay_panel(struct wl_client *client,
3850 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003851{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003852 struct input_panel_surface *input_panel_surface =
3853 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003854 struct desktop_shell *shell = input_panel_surface->shell;
3855
3856 wl_list_insert(&shell->input_panel.surfaces,
3857 &input_panel_surface->link);
3858
3859 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003860}
3861
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003862static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003863 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003864 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003865};
3866
3867static void
3868destroy_input_panel_surface_resource(struct wl_resource *resource)
3869{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003870 struct input_panel_surface *ipsurf =
3871 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003872
3873 destroy_input_panel_surface(ipsurf);
3874}
3875
3876static void
3877input_panel_get_input_panel_surface(struct wl_client *client,
3878 struct wl_resource *resource,
3879 uint32_t id,
3880 struct wl_resource *surface_resource)
3881{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003882 struct weston_surface *surface =
3883 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003884 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003885 struct input_panel_surface *ipsurf;
3886
3887 if (get_input_panel_surface(surface)) {
3888 wl_resource_post_error(surface_resource,
3889 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003890 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003891 return;
3892 }
3893
3894 ipsurf = create_input_panel_surface(shell, surface);
3895 if (!ipsurf) {
3896 wl_resource_post_error(surface_resource,
3897 WL_DISPLAY_ERROR_INVALID_OBJECT,
3898 "surface->configure already set");
3899 return;
3900 }
3901
Jason Ekstranda85118c2013-06-27 20:17:02 -05003902 ipsurf->resource =
3903 wl_resource_create(client,
3904 &wl_input_panel_surface_interface, 1, id);
3905 wl_resource_set_implementation(ipsurf->resource,
3906 &input_panel_surface_implementation,
3907 ipsurf,
3908 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003909}
3910
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003911static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003912 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003913};
3914
3915static void
3916unbind_input_panel(struct wl_resource *resource)
3917{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003918 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003919
3920 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003921}
3922
3923static void
3924bind_input_panel(struct wl_client *client,
3925 void *data, uint32_t version, uint32_t id)
3926{
3927 struct desktop_shell *shell = data;
3928 struct wl_resource *resource;
3929
Jason Ekstranda85118c2013-06-27 20:17:02 -05003930 resource = wl_resource_create(client,
3931 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003932
3933 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003934 wl_resource_set_implementation(resource,
3935 &input_panel_implementation,
3936 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003937 shell->input_panel.binding = resource;
3938 return;
3939 }
3940
3941 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3942 "interface object already bound");
3943 wl_resource_destroy(resource);
3944}
3945
Kristian Høgsberg07045392012-02-19 18:52:44 -05003946struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003947 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003948 struct weston_surface *current;
3949 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003950 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003951};
3952
3953static void
3954switcher_next(struct switcher *switcher)
3955{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003956 struct weston_surface *surface;
3957 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003958 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003959 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003960
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003961 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003962 switch (get_shell_surface_type(surface)) {
3963 case SHELL_SURFACE_TOPLEVEL:
3964 case SHELL_SURFACE_FULLSCREEN:
3965 case SHELL_SURFACE_MAXIMIZED:
3966 if (first == NULL)
3967 first = surface;
3968 if (prev == switcher->current)
3969 next = surface;
3970 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003971 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003972 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003973 weston_surface_damage(surface);
3974 break;
3975 default:
3976 break;
3977 }
Alex Wu1659daa2012-04-01 20:13:09 +08003978
3979 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003980 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003981 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003982 weston_surface_damage(surface);
3983 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003984 }
3985
3986 if (next == NULL)
3987 next = first;
3988
Alex Wu07b26062012-03-12 16:06:01 +08003989 if (next == NULL)
3990 return;
3991
Kristian Høgsberg07045392012-02-19 18:52:44 -05003992 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003993 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003994
3995 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003996 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003997
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003998 shsurf = get_shell_surface(switcher->current);
3999 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004000 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004001}
4002
4003static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004004switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004005{
4006 struct switcher *switcher =
4007 container_of(listener, struct switcher, listener);
4008
4009 switcher_next(switcher);
4010}
4011
4012static void
Daniel Stone351eb612012-05-31 15:27:47 -04004013switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004014{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004015 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004016 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004017 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004018
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004019 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004020 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004021 weston_surface_damage(surface);
4022 }
4023
Alex Wu07b26062012-03-12 16:06:01 +08004024 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004025 activate(switcher->shell, switcher->current,
4026 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004027 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004028 weston_keyboard_end_grab(keyboard);
4029 if (keyboard->input_method_resource)
4030 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004031 free(switcher);
4032}
4033
4034static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004035switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004036 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004037{
4038 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004039 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004040
Daniel Stonec9785ea2012-05-30 16:31:52 +01004041 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004042 switcher_next(switcher);
4043}
4044
4045static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004046switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004047 uint32_t mods_depressed, uint32_t mods_latched,
4048 uint32_t mods_locked, uint32_t group)
4049{
4050 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004051 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004052
Daniel Stone351eb612012-05-31 15:27:47 -04004053 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4054 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004055}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004056
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004057static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004058 switcher_key,
4059 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004060};
4061
4062static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004063switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004064 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004065{
Tiago Vignattibe143262012-04-16 17:31:41 +03004066 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004067 struct switcher *switcher;
4068
4069 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004070 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004071 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004072 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004073 wl_list_init(&switcher->listener.link);
4074
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004075 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004076 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004077 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004078 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4079 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004080 switcher_next(switcher);
4081}
4082
Pekka Paalanen3c647232011-12-22 13:43:43 +02004083static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004084backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004085 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004086{
4087 struct weston_compositor *compositor = data;
4088 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004089 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004090
4091 /* TODO: we're limiting to simple use cases, where we assume just
4092 * control on the primary display. We'd have to extend later if we
4093 * ever get support for setting backlights on random desktop LCD
4094 * panels though */
4095 output = get_default_output(compositor);
4096 if (!output)
4097 return;
4098
4099 if (!output->set_backlight)
4100 return;
4101
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004102 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4103 backlight_new = output->backlight_current - 25;
4104 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4105 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004106
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004107 if (backlight_new < 5)
4108 backlight_new = 5;
4109 if (backlight_new > 255)
4110 backlight_new = 255;
4111
4112 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004113 output->set_backlight(output, output->backlight_current);
4114}
4115
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004116struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004117 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004118 struct weston_seat *seat;
4119 uint32_t key[2];
4120 int key_released[2];
4121};
4122
4123static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004124debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004125 uint32_t key, uint32_t state)
4126{
4127 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4128 struct wl_resource *resource;
4129 struct wl_display *display;
4130 uint32_t serial;
4131 int send = 0, terminate = 0;
4132 int check_binding = 1;
4133 int i;
4134
4135 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4136 /* Do not run bindings on key releases */
4137 check_binding = 0;
4138
4139 for (i = 0; i < 2; i++)
4140 if (key == db->key[i])
4141 db->key_released[i] = 1;
4142
4143 if (db->key_released[0] && db->key_released[1]) {
4144 /* All key releases been swalled so end the grab */
4145 terminate = 1;
4146 } else if (key != db->key[0] && key != db->key[1]) {
4147 /* Should not swallow release of other keys */
4148 send = 1;
4149 }
4150 } else if (key == db->key[0] && !db->key_released[0]) {
4151 /* Do not check bindings for the first press of the binding
4152 * key. This allows it to be used as a debug shortcut.
4153 * We still need to swallow this event. */
4154 check_binding = 0;
4155 } else if (db->key[1]) {
4156 /* If we already ran a binding don't process another one since
4157 * we can't keep track of all the binding keys that were
4158 * pressed in order to swallow the release events. */
4159 send = 1;
4160 check_binding = 0;
4161 }
4162
4163 if (check_binding) {
4164 struct weston_compositor *ec = db->seat->compositor;
4165
4166 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4167 key, state)) {
4168 /* We ran a binding so swallow the press and keep the
4169 * grab to swallow the released too. */
4170 send = 0;
4171 terminate = 0;
4172 db->key[1] = key;
4173 } else {
4174 /* Terminate the grab since the key pressed is not a
4175 * debug binding key. */
4176 send = 1;
4177 terminate = 1;
4178 }
4179 }
4180
4181 if (send) {
4182 resource = grab->keyboard->focus_resource;
4183
4184 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004185 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004186 serial = wl_display_next_serial(display);
4187 wl_keyboard_send_key(resource, serial, time, key, state);
4188 }
4189 }
4190
4191 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004192 weston_keyboard_end_grab(grab->keyboard);
4193 if (grab->keyboard->input_method_resource)
4194 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004195 free(db);
4196 }
4197}
4198
4199static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004200debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004201 uint32_t mods_depressed, uint32_t mods_latched,
4202 uint32_t mods_locked, uint32_t group)
4203{
4204 struct wl_resource *resource;
4205
4206 resource = grab->keyboard->focus_resource;
4207 if (!resource)
4208 return;
4209
4210 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4211 mods_latched, mods_locked, group);
4212}
4213
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004214struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004215 debug_binding_key,
4216 debug_binding_modifiers
4217};
4218
4219static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004220debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004221{
4222 struct debug_binding_grab *grab;
4223
4224 grab = calloc(1, sizeof *grab);
4225 if (!grab)
4226 return;
4227
4228 grab->seat = (struct weston_seat *) seat;
4229 grab->key[0] = key;
4230 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004231 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004232}
4233
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004234static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004235force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004236 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004237{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004238 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004239 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004240 struct desktop_shell *shell = data;
4241 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004242 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004243
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004244 focus_surface = seat->keyboard->focus;
4245 if (!focus_surface)
4246 return;
4247
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004248 wl_signal_emit(&compositor->kill_signal, focus_surface);
4249
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004250 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004251 wl_client_get_credentials(client, &pid, NULL, NULL);
4252
4253 /* Skip clients that we launched ourselves (the credentials of
4254 * the socketpair is ours) */
4255 if (pid == getpid())
4256 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004257
Daniel Stone325fc2d2012-05-30 16:31:58 +01004258 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004259}
4260
4261static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004262workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004263 uint32_t key, void *data)
4264{
4265 struct desktop_shell *shell = data;
4266 unsigned int new_index = shell->workspaces.current;
4267
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004268 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004269 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004270 if (new_index != 0)
4271 new_index--;
4272
4273 change_workspace(shell, new_index);
4274}
4275
4276static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004277workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004278 uint32_t key, void *data)
4279{
4280 struct desktop_shell *shell = data;
4281 unsigned int new_index = shell->workspaces.current;
4282
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004283 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004284 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004285 if (new_index < shell->workspaces.num - 1)
4286 new_index++;
4287
4288 change_workspace(shell, new_index);
4289}
4290
4291static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004292workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004293 uint32_t key, void *data)
4294{
4295 struct desktop_shell *shell = data;
4296 unsigned int new_index;
4297
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004298 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004299 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004300 new_index = key - KEY_F1;
4301 if (new_index >= shell->workspaces.num)
4302 new_index = shell->workspaces.num - 1;
4303
4304 change_workspace(shell, new_index);
4305}
4306
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004307static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004308workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004309 uint32_t key, void *data)
4310{
4311 struct desktop_shell *shell = data;
4312 unsigned int new_index = shell->workspaces.current;
4313
4314 if (shell->locked)
4315 return;
4316
4317 if (new_index != 0)
4318 new_index--;
4319
4320 take_surface_to_workspace_by_seat(shell, seat, new_index);
4321}
4322
4323static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004324workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004325 uint32_t key, void *data)
4326{
4327 struct desktop_shell *shell = data;
4328 unsigned int new_index = shell->workspaces.current;
4329
4330 if (shell->locked)
4331 return;
4332
4333 if (new_index < shell->workspaces.num - 1)
4334 new_index++;
4335
4336 take_surface_to_workspace_by_seat(shell, seat, new_index);
4337}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004338
4339static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004340shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004341{
Tiago Vignattibe143262012-04-16 17:31:41 +03004342 struct desktop_shell *shell =
4343 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004344 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004345
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004346 if (shell->child.client)
4347 wl_client_destroy(shell->child.client);
4348
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004349 wl_list_remove(&shell->idle_listener.link);
4350 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004351 wl_list_remove(&shell->show_input_panel_listener.link);
4352 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004353
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004354 wl_array_for_each(ws, &shell->workspaces.array)
4355 workspace_destroy(*ws);
4356 wl_array_release(&shell->workspaces.array);
4357
Pekka Paalanen3c647232011-12-22 13:43:43 +02004358 free(shell->screensaver.path);
4359 free(shell);
4360}
4361
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004362static void
4363shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4364{
4365 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004366 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004367
4368 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004369 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4370 MODIFIER_CTRL | MODIFIER_ALT,
4371 terminate_binding, ec);
4372 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4373 click_to_activate_binding,
4374 shell);
4375 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4376 MODIFIER_SUPER | MODIFIER_ALT,
4377 surface_opacity_binding, NULL);
4378 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4379 MODIFIER_SUPER, zoom_axis_binding,
4380 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004381
4382 /* configurable bindings */
4383 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004384 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4385 zoom_key_binding, NULL);
4386 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4387 zoom_key_binding, NULL);
4388 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4389 shell);
4390 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4391 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004392
4393 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4394 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4395 rotate_binding, NULL);
4396
Daniel Stone325fc2d2012-05-30 16:31:58 +01004397 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4398 shell);
4399 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4400 ec);
4401 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4402 backlight_binding, ec);
4403 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4404 ec);
4405 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4406 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004407 weston_compositor_add_key_binding(ec, KEY_K, mod,
4408 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004409 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4410 workspace_up_binding, shell);
4411 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4412 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004413 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4414 workspace_move_surface_up_binding,
4415 shell);
4416 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4417 workspace_move_surface_down_binding,
4418 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004419
4420 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4421 if (shell->workspaces.num > 1) {
4422 num_workspace_bindings = shell->workspaces.num;
4423 if (num_workspace_bindings > 6)
4424 num_workspace_bindings = 6;
4425 for (i = 0; i < num_workspace_bindings; i++)
4426 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4427 workspace_f_binding,
4428 shell);
4429 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004430
4431 /* Debug bindings */
4432 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4433 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004434}
4435
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004436WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004437module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004438 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004439{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004440 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004441 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004442 struct workspace **pws;
4443 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004444 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004445
4446 shell = malloc(sizeof *shell);
4447 if (shell == NULL)
4448 return -1;
4449
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004450 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004451 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004452
4453 shell->destroy_listener.notify = shell_destroy;
4454 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004455 shell->idle_listener.notify = idle_handler;
4456 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4457 shell->wake_listener.notify = wake_handler;
4458 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004459 shell->show_input_panel_listener.notify = show_input_panels;
4460 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4461 shell->hide_input_panel_listener.notify = hide_input_panels;
4462 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004463 shell->update_input_panel_listener.notify = update_input_panels;
4464 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004465 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004466 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004467 ec->shell_interface.create_shell_surface = create_shell_surface;
4468 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004469 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004470 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004471 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004472 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004473 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004474
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004475 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004476
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004477 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4478 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004479 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4480 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004481 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004482
4483 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004484 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004485
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004486 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004487
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004488 for (i = 0; i < shell->workspaces.num; i++) {
4489 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4490 if (pws == NULL)
4491 return -1;
4492
4493 *pws = workspace_create();
4494 if (*pws == NULL)
4495 return -1;
4496 }
4497 activate_workspace(shell, 0);
4498
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004499 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004500 wl_list_init(&shell->workspaces.animation.link);
4501 shell->workspaces.animation.frame = animate_workspace_change_frame;
4502
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004503 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4504 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004505 return -1;
4506
Kristian Høgsberg75840622011-09-06 13:48:16 -04004507 if (wl_display_add_global(ec->wl_display,
4508 &desktop_shell_interface,
4509 shell, bind_desktop_shell) == NULL)
4510 return -1;
4511
Pekka Paalanen6e168112011-11-24 11:34:05 +02004512 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4513 shell, bind_screensaver) == NULL)
4514 return -1;
4515
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004516 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004517 shell, bind_input_panel) == NULL)
4518 return -1;
4519
Jonas Ådahle9d22502012-08-29 22:13:01 +02004520 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4521 shell, bind_workspace_manager) == NULL)
4522 return -1;
4523
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004524 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004525
4526 loop = wl_display_get_event_loop(ec->wl_display);
4527 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004528
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004529 shell->screensaver.timer =
4530 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4531
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004532 wl_list_for_each(seat, &ec->seat_list, link)
4533 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004534
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004535 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004536
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004537 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004538
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004539 return 0;
4540}