blob: a3b1b1f83ff9f380aa9010508d155eecc2c0f640 [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);
405 weston_config_section_get_string(section, "animation", &s, "none");
406 shell->win_animation_type = get_animation_type(s);
407 weston_config_section_get_uint(section, "num-workspaces",
408 &shell->workspaces.num,
409 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200410}
411
412static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200413focus_state_destroy(struct focus_state *state)
414{
415 wl_list_remove(&state->seat_destroy_listener.link);
416 wl_list_remove(&state->surface_destroy_listener.link);
417 free(state);
418}
419
420static void
421focus_state_seat_destroy(struct wl_listener *listener, void *data)
422{
423 struct focus_state *state = container_of(listener,
424 struct focus_state,
425 seat_destroy_listener);
426
427 wl_list_remove(&state->link);
428 focus_state_destroy(state);
429}
430
431static void
432focus_state_surface_destroy(struct wl_listener *listener, void *data)
433{
434 struct focus_state *state = container_of(listener,
435 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400436 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400437 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300438 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400439 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200440
Pekka Paalanen01388e22013-04-25 13:57:44 +0300441 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
442
Kristian Høgsberge3778222012-07-31 17:29:30 -0400443 next = NULL;
444 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300445 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400446 continue;
447
448 next = surface;
449 break;
450 }
451
Pekka Paalanen01388e22013-04-25 13:57:44 +0300452 /* if the focus was a sub-surface, activate its main surface */
453 if (main_surface != state->keyboard_focus)
454 next = main_surface;
455
Kristian Høgsberge3778222012-07-31 17:29:30 -0400456 if (next) {
457 shell = state->seat->compositor->shell_interface.shell;
458 activate(shell, next, state->seat);
459 } else {
460 wl_list_remove(&state->link);
461 focus_state_destroy(state);
462 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200463}
464
465static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400466focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200467{
Jonas Ådahl04769742012-06-13 00:01:24 +0200468 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200469
470 state = malloc(sizeof *state);
471 if (state == NULL)
472 return NULL;
473
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400474 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200475 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400476 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200477
478 state->seat_destroy_listener.notify = focus_state_seat_destroy;
479 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400480 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200481 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400482 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200483
484 return state;
485}
486
Jonas Ådahl8538b222012-08-29 22:13:03 +0200487static struct focus_state *
488ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
489{
490 struct workspace *ws = get_current_workspace(shell);
491 struct focus_state *state;
492
493 wl_list_for_each(state, &ws->focus_list, link)
494 if (state->seat == seat)
495 break;
496
497 if (&state->link == &ws->focus_list)
498 state = focus_state_create(seat, ws);
499
500 return state;
501}
502
Jonas Ådahl04769742012-06-13 00:01:24 +0200503static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400504restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200505{
506 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400507 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200508
509 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400510 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200511
Kristian Høgsberge3148752013-05-06 23:19:49 -0400512 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200513 }
514}
515
516static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200517replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
518 struct weston_seat *seat)
519{
520 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400521 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200522
523 wl_list_for_each(state, &ws->focus_list, link) {
524 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400525 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500526 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200527 return;
528 }
529 }
530}
531
532static void
533drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
534 struct weston_surface *surface)
535{
536 struct focus_state *state;
537
538 wl_list_for_each(state, &ws->focus_list, link)
539 if (state->keyboard_focus == surface)
540 state->keyboard_focus = NULL;
541}
542
543static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200544workspace_destroy(struct workspace *ws)
545{
Jonas Ådahl04769742012-06-13 00:01:24 +0200546 struct focus_state *state, *next;
547
548 wl_list_for_each_safe(state, next, &ws->focus_list, link)
549 focus_state_destroy(state);
550
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200551 free(ws);
552}
553
Jonas Ådahl04769742012-06-13 00:01:24 +0200554static void
555seat_destroyed(struct wl_listener *listener, void *data)
556{
557 struct weston_seat *seat = data;
558 struct focus_state *state, *next;
559 struct workspace *ws = container_of(listener,
560 struct workspace,
561 seat_destroyed_listener);
562
563 wl_list_for_each_safe(state, next, &ws->focus_list, link)
564 if (state->seat == seat)
565 wl_list_remove(&state->link);
566}
567
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200568static struct workspace *
569workspace_create(void)
570{
571 struct workspace *ws = malloc(sizeof *ws);
572 if (ws == NULL)
573 return NULL;
574
575 weston_layer_init(&ws->layer, NULL);
576
Jonas Ådahl04769742012-06-13 00:01:24 +0200577 wl_list_init(&ws->focus_list);
578 wl_list_init(&ws->seat_destroyed_listener.link);
579 ws->seat_destroyed_listener.notify = seat_destroyed;
580
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200581 return ws;
582}
583
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200584static int
585workspace_is_empty(struct workspace *ws)
586{
587 return wl_list_empty(&ws->layer.surface_list);
588}
589
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200590static struct workspace *
591get_workspace(struct desktop_shell *shell, unsigned int index)
592{
593 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200594 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200595 pws += index;
596 return *pws;
597}
598
599static struct workspace *
600get_current_workspace(struct desktop_shell *shell)
601{
602 return get_workspace(shell, shell->workspaces.current);
603}
604
605static void
606activate_workspace(struct desktop_shell *shell, unsigned int index)
607{
608 struct workspace *ws;
609
610 ws = get_workspace(shell, index);
611 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
612
613 shell->workspaces.current = index;
614}
615
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200616static unsigned int
617get_output_height(struct weston_output *output)
618{
619 return abs(output->region.extents.y1 - output->region.extents.y2);
620}
621
622static void
623surface_translate(struct weston_surface *surface, double d)
624{
625 struct shell_surface *shsurf = get_shell_surface(surface);
626 struct weston_transform *transform;
627
628 transform = &shsurf->workspace_transform;
629 if (wl_list_empty(&transform->link))
630 wl_list_insert(surface->geometry.transformation_list.prev,
631 &shsurf->workspace_transform.link);
632
633 weston_matrix_init(&shsurf->workspace_transform.matrix);
634 weston_matrix_translate(&shsurf->workspace_transform.matrix,
635 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200636 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200637}
638
639static void
640workspace_translate_out(struct workspace *ws, double fraction)
641{
642 struct weston_surface *surface;
643 unsigned int height;
644 double d;
645
646 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
647 height = get_output_height(surface->output);
648 d = height * fraction;
649
650 surface_translate(surface, d);
651 }
652}
653
654static void
655workspace_translate_in(struct workspace *ws, double fraction)
656{
657 struct weston_surface *surface;
658 unsigned int height;
659 double d;
660
661 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
662 height = get_output_height(surface->output);
663
664 if (fraction > 0)
665 d = -(height - height * fraction);
666 else
667 d = height + height * fraction;
668
669 surface_translate(surface, d);
670 }
671}
672
673static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200674broadcast_current_workspace_state(struct desktop_shell *shell)
675{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500676 struct wl_list *link;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200677
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500678 for (link = shell->workspaces.client_list.next;
679 link != &shell->workspaces.client_list;
680 link = link->next) {
681 workspace_manager_send_state(wl_resource_from_link(link),
Jonas Ådahle9d22502012-08-29 22:13:01 +0200682 shell->workspaces.current,
683 shell->workspaces.num);
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500684 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200685}
686
687static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200688reverse_workspace_change_animation(struct desktop_shell *shell,
689 unsigned int index,
690 struct workspace *from,
691 struct workspace *to)
692{
693 shell->workspaces.current = index;
694
695 shell->workspaces.anim_to = to;
696 shell->workspaces.anim_from = from;
697 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
698 shell->workspaces.anim_timestamp = 0;
699
Scott Moreau4272e632012-08-13 09:58:41 -0600700 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200701}
702
703static void
704workspace_deactivate_transforms(struct workspace *ws)
705{
706 struct weston_surface *surface;
707 struct shell_surface *shsurf;
708
709 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
710 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200711 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
712 wl_list_remove(&shsurf->workspace_transform.link);
713 wl_list_init(&shsurf->workspace_transform.link);
714 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200715 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200716 }
717}
718
719static void
720finish_workspace_change_animation(struct desktop_shell *shell,
721 struct workspace *from,
722 struct workspace *to)
723{
Scott Moreau4272e632012-08-13 09:58:41 -0600724 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200725
726 wl_list_remove(&shell->workspaces.animation.link);
727 workspace_deactivate_transforms(from);
728 workspace_deactivate_transforms(to);
729 shell->workspaces.anim_to = NULL;
730
731 wl_list_remove(&shell->workspaces.anim_from->layer.link);
732}
733
734static void
735animate_workspace_change_frame(struct weston_animation *animation,
736 struct weston_output *output, uint32_t msecs)
737{
738 struct desktop_shell *shell =
739 container_of(animation, struct desktop_shell,
740 workspaces.animation);
741 struct workspace *from = shell->workspaces.anim_from;
742 struct workspace *to = shell->workspaces.anim_to;
743 uint32_t t;
744 double x, y;
745
746 if (workspace_is_empty(from) && workspace_is_empty(to)) {
747 finish_workspace_change_animation(shell, from, to);
748 return;
749 }
750
751 if (shell->workspaces.anim_timestamp == 0) {
752 if (shell->workspaces.anim_current == 0.0)
753 shell->workspaces.anim_timestamp = msecs;
754 else
755 shell->workspaces.anim_timestamp =
756 msecs -
757 /* Invers of movement function 'y' below. */
758 (asin(1.0 - shell->workspaces.anim_current) *
759 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
760 M_2_PI);
761 }
762
763 t = msecs - shell->workspaces.anim_timestamp;
764
765 /*
766 * x = [0, π/2]
767 * y(x) = sin(x)
768 */
769 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
770 y = sin(x);
771
772 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600773 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774
775 workspace_translate_out(from, shell->workspaces.anim_dir * y);
776 workspace_translate_in(to, shell->workspaces.anim_dir * y);
777 shell->workspaces.anim_current = y;
778
Scott Moreau4272e632012-08-13 09:58:41 -0600779 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200780 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200781 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200782 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200783}
784
785static void
786animate_workspace_change(struct desktop_shell *shell,
787 unsigned int index,
788 struct workspace *from,
789 struct workspace *to)
790{
791 struct weston_output *output;
792
793 int dir;
794
795 if (index > shell->workspaces.current)
796 dir = -1;
797 else
798 dir = 1;
799
800 shell->workspaces.current = index;
801
802 shell->workspaces.anim_dir = dir;
803 shell->workspaces.anim_from = from;
804 shell->workspaces.anim_to = to;
805 shell->workspaces.anim_current = 0.0;
806 shell->workspaces.anim_timestamp = 0;
807
808 output = container_of(shell->compositor->output_list.next,
809 struct weston_output, link);
810 wl_list_insert(&output->animation_list,
811 &shell->workspaces.animation.link);
812
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200813 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200814
815 workspace_translate_in(to, 0);
816
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400817 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200818
Scott Moreau4272e632012-08-13 09:58:41 -0600819 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200820}
821
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200822static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200823update_workspace(struct desktop_shell *shell, unsigned int index,
824 struct workspace *from, struct workspace *to)
825{
826 shell->workspaces.current = index;
827 wl_list_insert(&from->layer.link, &to->layer.link);
828 wl_list_remove(&from->layer.link);
829}
830
831static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200832change_workspace(struct desktop_shell *shell, unsigned int index)
833{
834 struct workspace *from;
835 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200836
837 if (index == shell->workspaces.current)
838 return;
839
840 /* Don't change workspace when there is any fullscreen surfaces. */
841 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
842 return;
843
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200844 from = get_current_workspace(shell);
845 to = get_workspace(shell, index);
846
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847 if (shell->workspaces.anim_from == to &&
848 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200849 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200850 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200851 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200852 return;
853 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200854
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 if (shell->workspaces.anim_to != NULL)
856 finish_workspace_change_animation(shell,
857 shell->workspaces.anim_from,
858 shell->workspaces.anim_to);
859
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200860 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200862 if (workspace_is_empty(to) && workspace_is_empty(from))
863 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200864 else
865 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200866
867 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200868}
869
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200870static bool
871workspace_has_only(struct workspace *ws, struct weston_surface *surface)
872{
873 struct wl_list *list = &ws->layer.surface_list;
874 struct wl_list *e;
875
876 if (wl_list_empty(list))
877 return false;
878
879 e = list->next;
880
881 if (e->next != list)
882 return false;
883
884 return container_of(e, struct weston_surface, layer_link) == surface;
885}
886
887static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200888move_surface_to_workspace(struct desktop_shell *shell,
889 struct weston_surface *surface,
890 uint32_t workspace)
891{
892 struct workspace *from;
893 struct workspace *to;
894 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300895 struct weston_surface *focus;
896
897 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200898
899 if (workspace == shell->workspaces.current)
900 return;
901
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200902 if (workspace >= shell->workspaces.num)
903 workspace = shell->workspaces.num - 1;
904
Jonas Ådahle9d22502012-08-29 22:13:01 +0200905 from = get_current_workspace(shell);
906 to = get_workspace(shell, workspace);
907
908 wl_list_remove(&surface->layer_link);
909 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
910
911 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300912 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
913 if (!seat->keyboard)
914 continue;
915
916 focus = weston_surface_get_main_surface(seat->keyboard->focus);
917 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400918 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300919 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200920
921 weston_surface_damage_below(surface);
922}
923
924static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400926 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200927 unsigned int index)
928{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300929 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930 struct shell_surface *shsurf;
931 struct workspace *from;
932 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200933 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200934
Pekka Paalanen01388e22013-04-25 13:57:44 +0300935 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936 if (surface == NULL ||
937 index == shell->workspaces.current)
938 return;
939
940 from = get_current_workspace(shell);
941 to = get_workspace(shell, index);
942
943 wl_list_remove(&surface->layer_link);
944 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
945
Jonas Ådahle9d22502012-08-29 22:13:01 +0200946 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200947 drop_focus_state(shell, from, surface);
948
949 if (shell->workspaces.anim_from == to &&
950 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200951 wl_list_remove(&to->layer.link);
952 wl_list_insert(from->layer.link.prev, &to->layer.link);
953
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200954 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200955 broadcast_current_workspace_state(shell);
956
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200957 return;
958 }
959
960 if (shell->workspaces.anim_to != NULL)
961 finish_workspace_change_animation(shell,
962 shell->workspaces.anim_from,
963 shell->workspaces.anim_to);
964
965 if (workspace_is_empty(from) &&
966 workspace_has_only(to, surface))
967 update_workspace(shell, index, from, to);
968 else {
969 shsurf = get_shell_surface(surface);
970 if (wl_list_empty(&shsurf->workspace_transform.link))
971 wl_list_insert(&shell->workspaces.anim_sticky_list,
972 &shsurf->workspace_transform.link);
973
974 animate_workspace_change(shell, index, from, to);
975 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200976
977 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200978
979 state = ensure_focus_state(shell, seat);
980 if (state != NULL)
981 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200982}
983
984static void
985workspace_manager_move_surface(struct wl_client *client,
986 struct wl_resource *resource,
987 struct wl_resource *surface_resource,
988 uint32_t workspace)
989{
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500990 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200991 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -0500992 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300993 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200994
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 main_surface = weston_surface_get_main_surface(surface);
996 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200997}
998
999static const struct workspace_manager_interface workspace_manager_implementation = {
1000 workspace_manager_move_surface,
1001};
1002
1003static void
1004unbind_resource(struct wl_resource *resource)
1005{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001006 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001007 free(resource);
1008}
1009
1010static void
1011bind_workspace_manager(struct wl_client *client,
1012 void *data, uint32_t version, uint32_t id)
1013{
1014 struct desktop_shell *shell = data;
1015 struct wl_resource *resource;
1016
1017 resource = wl_client_add_object(client, &workspace_manager_interface,
1018 &workspace_manager_implementation,
1019 id, shell);
1020
1021 if (resource == NULL) {
1022 weston_log("couldn't add workspace manager object");
1023 return;
1024 }
1025
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001026 wl_resource_set_destructor(resource, unbind_resource);
1027 wl_list_insert(&shell->workspaces.client_list,
1028 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001029
1030 workspace_manager_send_state(resource,
1031 shell->workspaces.current,
1032 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001033}
1034
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001035static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001036noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001037{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001038}
1039
1040static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001041move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001042{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001043 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001044 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001045 struct shell_surface *shsurf = move->base.shsurf;
1046 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001047 int dx = wl_fixed_to_int(pointer->x + move->dx);
1048 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001049
1050 if (!shsurf)
1051 return;
1052
1053 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001054
Daniel Stone103db7f2012-05-08 17:17:55 +01001055 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001056 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001057
1058 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001059}
1060
1061static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001062move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001063 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001065 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1066 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001067 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001068 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001069
Daniel Stone4dbadb12012-05-30 16:31:51 +01001070 if (pointer->button_count == 0 &&
1071 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001072 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001073 free(grab);
1074 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001075}
1076
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001077static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001078 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001079 move_grab_motion,
1080 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001081};
1082
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001083static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001084surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001085{
1086 struct weston_move_grab *move;
1087
1088 if (!shsurf)
1089 return -1;
1090
1091 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1092 return 0;
1093
1094 move = malloc(sizeof *move);
1095 if (!move)
1096 return -1;
1097
1098 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001099 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001100 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001101 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001102
1103 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001104 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001105
1106 return 0;
1107}
1108
1109static void
1110shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1111 struct wl_resource *seat_resource, uint32_t serial)
1112{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001113 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001114 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001115 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001116
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001117 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001118 if (seat->pointer->button_count == 0 ||
1119 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001120 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001121 return;
1122
Kristian Høgsberge3148752013-05-06 23:19:49 -04001123 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001124 wl_resource_post_no_memory(resource);
1125}
1126
1127struct weston_resize_grab {
1128 struct shell_grab base;
1129 uint32_t edges;
1130 int32_t width, height;
1131};
1132
1133static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001134resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001135{
1136 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001137 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001138 struct shell_surface *shsurf = resize->base.shsurf;
1139 int32_t width, height;
1140 wl_fixed_t from_x, from_y;
1141 wl_fixed_t to_x, to_y;
1142
1143 if (!shsurf)
1144 return;
1145
1146 weston_surface_from_global_fixed(shsurf->surface,
1147 pointer->grab_x, pointer->grab_y,
1148 &from_x, &from_y);
1149 weston_surface_from_global_fixed(shsurf->surface,
1150 pointer->x, pointer->y, &to_x, &to_y);
1151
1152 width = resize->width;
1153 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1154 width += wl_fixed_to_int(from_x - to_x);
1155 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1156 width += wl_fixed_to_int(to_x - from_x);
1157 }
1158
1159 height = resize->height;
1160 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1161 height += wl_fixed_to_int(from_y - to_y);
1162 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1163 height += wl_fixed_to_int(to_y - from_y);
1164 }
1165
1166 shsurf->client->send_configure(shsurf->surface,
1167 resize->edges, width, height);
1168}
1169
1170static void
1171send_configure(struct weston_surface *surface,
1172 uint32_t edges, int32_t width, int32_t height)
1173{
1174 struct shell_surface *shsurf = get_shell_surface(surface);
1175
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001176 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001177 edges, width, height);
1178}
1179
1180static const struct weston_shell_client shell_client = {
1181 send_configure
1182};
1183
1184static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001185resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001186 uint32_t time, uint32_t button, uint32_t state_w)
1187{
1188 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001189 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001190 enum wl_pointer_button_state state = state_w;
1191
1192 if (pointer->button_count == 0 &&
1193 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1194 shell_grab_end(&resize->base);
1195 free(grab);
1196 }
1197}
1198
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001199static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001200 noop_grab_focus,
1201 resize_grab_motion,
1202 resize_grab_button,
1203};
1204
Giulio Camuffob8366642013-04-25 13:57:46 +03001205/*
1206 * Returns the bounding box of a surface and all its sub-surfaces,
1207 * in the surface coordinates system. */
1208static void
1209surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1210 int32_t *y, int32_t *w, int32_t *h) {
1211 pixman_region32_t region;
1212 pixman_box32_t *box;
1213 struct weston_subsurface *subsurface;
1214
1215 pixman_region32_init_rect(&region, 0, 0,
1216 surface->geometry.width,
1217 surface->geometry.height);
1218
1219 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1220 pixman_region32_union_rect(&region, &region,
1221 subsurface->position.x,
1222 subsurface->position.y,
1223 subsurface->surface->geometry.width,
1224 subsurface->surface->geometry.height);
1225 }
1226
1227 box = pixman_region32_extents(&region);
1228 if (x)
1229 *x = box->x1;
1230 if (y)
1231 *y = box->y1;
1232 if (w)
1233 *w = box->x2 - box->x1;
1234 if (h)
1235 *h = box->y2 - box->y1;
1236
1237 pixman_region32_fini(&region);
1238}
1239
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001240static int
1241surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001242 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001243{
1244 struct weston_resize_grab *resize;
1245
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001246 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1247 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001248 return 0;
1249
1250 if (edges == 0 || edges > 15 ||
1251 (edges & 3) == 3 || (edges & 12) == 12)
1252 return 0;
1253
1254 resize = malloc(sizeof *resize);
1255 if (!resize)
1256 return -1;
1257
1258 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001259 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1260 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001261
1262 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001263 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001264
1265 return 0;
1266}
1267
1268static void
1269shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1270 struct wl_resource *seat_resource, uint32_t serial,
1271 uint32_t edges)
1272{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001273 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001274 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001275 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001276
1277 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1278 return;
1279
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001280 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 if (seat->pointer->button_count == 0 ||
1282 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001283 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001284 return;
1285
Kristian Høgsberge3148752013-05-06 23:19:49 -04001286 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001287 wl_resource_post_no_memory(resource);
1288}
1289
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001290static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001291busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001292{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001293 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001294 struct weston_pointer *pointer = base->pointer;
1295 struct weston_surface *surface;
1296 wl_fixed_t sx, sy;
1297
1298 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1299 pointer->x, pointer->y,
1300 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001301
Rob Bradford2f8d9aa2013-05-20 12:09:20 +01001302 if (!grab->shsurf || grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001303 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001304 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001305 }
1306}
1307
1308static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001309busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001310{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001311}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001312
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001313static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001314busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001315 uint32_t time, uint32_t button, uint32_t state)
1316{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001317 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001318 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001319 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001320
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001321 if (shsurf && button == BTN_LEFT && state) {
1322 activate(shsurf->shell, shsurf->surface, seat);
1323 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001324 } else if (shsurf && button == BTN_RIGHT && state) {
1325 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001326 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001327 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001328}
1329
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001330static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001331 busy_cursor_grab_focus,
1332 busy_cursor_grab_motion,
1333 busy_cursor_grab_button,
1334};
1335
1336static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001337set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001338{
1339 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001340
1341 grab = malloc(sizeof *grab);
1342 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001343 return;
1344
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001345 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1346 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001347}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001348
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001349static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001350end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001351{
1352 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1353
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001354 if (grab->grab.interface == &busy_cursor_grab_interface &&
1355 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001356 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001357 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001358 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001359}
1360
Scott Moreau9521d5e2012-04-19 13:06:17 -06001361static void
1362ping_timer_destroy(struct shell_surface *shsurf)
1363{
1364 if (!shsurf || !shsurf->ping_timer)
1365 return;
1366
1367 if (shsurf->ping_timer->source)
1368 wl_event_source_remove(shsurf->ping_timer->source);
1369
1370 free(shsurf->ping_timer);
1371 shsurf->ping_timer = NULL;
1372}
1373
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001374static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001375ping_timeout_handler(void *data)
1376{
1377 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001378 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001379
Scott Moreau9521d5e2012-04-19 13:06:17 -06001380 /* Client is not responding */
1381 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001382
1383 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001384 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001385 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001386
1387 return 1;
1388}
1389
1390static void
1391ping_handler(struct weston_surface *surface, uint32_t serial)
1392{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001393 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001394 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001395 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001396
1397 if (!shsurf)
1398 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001399 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001400 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001401
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001402 if (shsurf->surface == shsurf->shell->grab_surface)
1403 return;
1404
Scott Moreauff1db4a2012-04-17 19:06:18 -06001405 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001406 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001407 if (!shsurf->ping_timer)
1408 return;
1409
1410 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001411 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1412 shsurf->ping_timer->source =
1413 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1414 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1415
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001416 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001417 }
1418}
1419
1420static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001421handle_pointer_focus(struct wl_listener *listener, void *data)
1422{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001423 struct weston_pointer *pointer = data;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001424 struct weston_surface *surface = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001425 struct weston_compositor *compositor;
1426 struct shell_surface *shsurf;
1427 uint32_t serial;
1428
1429 if (!surface)
1430 return;
1431
1432 compositor = surface->compositor;
1433 shsurf = get_shell_surface(surface);
1434
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001435 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001436 set_busy_cursor(shsurf, pointer);
1437 } else {
1438 serial = wl_display_next_serial(compositor->wl_display);
1439 ping_handler(surface, serial);
1440 }
1441}
1442
1443static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001444create_pointer_focus_listener(struct weston_seat *seat)
1445{
1446 struct wl_listener *listener;
1447
Kristian Høgsberge3148752013-05-06 23:19:49 -04001448 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001449 return;
1450
1451 listener = malloc(sizeof *listener);
1452 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001453 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001454}
1455
1456static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001457shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1458 uint32_t serial)
1459{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001460 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001461 struct weston_seat *seat;
1462 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001463
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001464 if (shsurf->ping_timer == NULL)
1465 /* Just ignore unsolicited pong. */
1466 return;
1467
Scott Moreauff1db4a2012-04-17 19:06:18 -06001468 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001469 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001470 wl_list_for_each(seat, &ec->seat_list, link) {
1471 if(seat->pointer)
1472 end_busy_cursor(shsurf, seat->pointer);
1473 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001474 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001475 }
1476}
1477
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001478static void
1479shell_surface_set_title(struct wl_client *client,
1480 struct wl_resource *resource, const char *title)
1481{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001482 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001483
1484 free(shsurf->title);
1485 shsurf->title = strdup(title);
1486}
1487
1488static void
1489shell_surface_set_class(struct wl_client *client,
1490 struct wl_resource *resource, const char *class)
1491{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001492 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001493
1494 free(shsurf->class);
1495 shsurf->class = strdup(class);
1496}
1497
Juan Zhao96879df2012-02-07 08:45:41 +08001498static struct weston_output *
1499get_default_output(struct weston_compositor *compositor)
1500{
1501 return container_of(compositor->output_list.next,
1502 struct weston_output, link);
1503}
1504
Alex Wu4539b082012-03-01 12:57:46 +08001505static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001506restore_output_mode(struct weston_output *output)
1507{
1508 if (output->current != output->origin ||
1509 (int32_t)output->scale != output->origin_scale)
1510 weston_output_switch_mode(output,
1511 output->origin,
1512 output->origin_scale);
1513}
1514
1515static void
1516restore_all_output_modes(struct weston_compositor *compositor)
1517{
1518 struct weston_output *output;
1519
1520 wl_list_for_each(output, &compositor->output_list, link)
1521 restore_output_mode(output);
1522}
1523
1524static void
Alex Wu4539b082012-03-01 12:57:46 +08001525shell_unset_fullscreen(struct shell_surface *shsurf)
1526{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001527 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001528 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001529 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1530 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001531 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001532 }
Alex Wu4539b082012-03-01 12:57:46 +08001533 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1534 shsurf->fullscreen.framerate = 0;
1535 wl_list_remove(&shsurf->fullscreen.transform.link);
1536 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001537 if (shsurf->fullscreen.black_surface)
1538 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001539 shsurf->fullscreen.black_surface = NULL;
1540 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001541 weston_surface_set_position(shsurf->surface,
1542 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001543 if (shsurf->saved_rotation_valid) {
1544 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1545 &shsurf->rotation.transform.link);
1546 shsurf->saved_rotation_valid = false;
1547 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001548
1549 ws = get_current_workspace(shsurf->shell);
1550 wl_list_remove(&shsurf->surface->layer_link);
1551 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001552}
1553
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001554static void
1555shell_unset_maximized(struct shell_surface *shsurf)
1556{
1557 struct workspace *ws;
1558 /* undo all maximized things here */
1559 shsurf->output = get_default_output(shsurf->surface->compositor);
1560 weston_surface_set_position(shsurf->surface,
1561 shsurf->saved_x,
1562 shsurf->saved_y);
1563
1564 if (shsurf->saved_rotation_valid) {
1565 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1566 &shsurf->rotation.transform.link);
1567 shsurf->saved_rotation_valid = false;
1568 }
1569
1570 ws = get_current_workspace(shsurf->shell);
1571 wl_list_remove(&shsurf->surface->layer_link);
1572 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1573}
1574
Pekka Paalanen98262232011-12-01 10:42:22 +02001575static int
1576reset_shell_surface_type(struct shell_surface *surface)
1577{
1578 switch (surface->type) {
1579 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001580 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001581 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001582 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001583 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001584 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001585 case SHELL_SURFACE_NONE:
1586 case SHELL_SURFACE_TOPLEVEL:
1587 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001589 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001590 break;
1591 }
1592
1593 surface->type = SHELL_SURFACE_NONE;
1594 return 0;
1595}
1596
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001597static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001598set_surface_type(struct shell_surface *shsurf)
1599{
1600 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001601 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001602
1603 reset_shell_surface_type(shsurf);
1604
1605 shsurf->type = shsurf->next_type;
1606 shsurf->next_type = SHELL_SURFACE_NONE;
1607
1608 switch (shsurf->type) {
1609 case SHELL_SURFACE_TOPLEVEL:
1610 break;
1611 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001612 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001613 pes->geometry.x + shsurf->transient.x,
1614 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001615 break;
1616
1617 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001618 case SHELL_SURFACE_FULLSCREEN:
1619 shsurf->saved_x = surface->geometry.x;
1620 shsurf->saved_y = surface->geometry.y;
1621 shsurf->saved_position_valid = true;
1622
1623 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1624 wl_list_remove(&shsurf->rotation.transform.link);
1625 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001626 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001627 shsurf->saved_rotation_valid = true;
1628 }
1629 break;
1630
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001631 case SHELL_SURFACE_XWAYLAND:
1632 weston_surface_set_position(surface, shsurf->transient.x,
1633 shsurf->transient.y);
1634 break;
1635
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001636 default:
1637 break;
1638 }
1639}
1640
1641static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001642set_toplevel(struct shell_surface *shsurf)
1643{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001644 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001645}
1646
1647static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001648shell_surface_set_toplevel(struct wl_client *client,
1649 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001650{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001651 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001652
Tiago Vignattibc052c92012-04-19 16:18:18 +03001653 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001654}
1655
1656static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001657set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001658 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001659{
1660 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001661 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001662 shsurf->transient.x = x;
1663 shsurf->transient.y = y;
1664 shsurf->transient.flags = flags;
1665 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1666}
1667
1668static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001669shell_surface_set_transient(struct wl_client *client,
1670 struct wl_resource *resource,
1671 struct wl_resource *parent_resource,
1672 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001673{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001674 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001675 struct weston_surface *parent =
1676 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001677
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001678 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001679}
1680
Tiago Vignattibe143262012-04-16 17:31:41 +03001681static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001682shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001683{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001684 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001685}
1686
1687static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001688get_output_panel_height(struct desktop_shell *shell,
1689 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001690{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001691 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001692 int panel_height = 0;
1693
1694 if (!output)
1695 return 0;
1696
Juan Zhao4ab94682012-07-09 22:24:09 -07001697 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001698 if (surface->output == output) {
1699 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001700 break;
1701 }
1702 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001703
Juan Zhao96879df2012-02-07 08:45:41 +08001704 return panel_height;
1705}
1706
1707static void
1708shell_surface_set_maximized(struct wl_client *client,
1709 struct wl_resource *resource,
1710 struct wl_resource *output_resource )
1711{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001712 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001713 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001714 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001715 uint32_t edges = 0, panel_height = 0;
1716
1717 /* get the default output, if the client set it as NULL
1718 check whether the ouput is available */
1719 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001720 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001721 else if (es->output)
1722 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001723 else
1724 shsurf->output = get_default_output(es->compositor);
1725
Tiago Vignattibe143262012-04-16 17:31:41 +03001726 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001727 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001728 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001729
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001730 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001731 shsurf->output->width,
1732 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001733
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001734 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001735}
1736
Alex Wu21858432012-04-01 20:13:08 +08001737static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001738black_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 +08001739
Alex Wu4539b082012-03-01 12:57:46 +08001740static struct weston_surface *
1741create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001742 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001743 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001744{
1745 struct weston_surface *surface = NULL;
1746
1747 surface = weston_surface_create(ec);
1748 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001749 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001750 return NULL;
1751 }
1752
Alex Wu21858432012-04-01 20:13:08 +08001753 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001754 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001755 weston_surface_configure(surface, x, y, w, h);
1756 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001757 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001758 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001759 pixman_region32_fini(&surface->input);
1760 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001761
Alex Wu4539b082012-03-01 12:57:46 +08001762 return surface;
1763}
1764
1765/* Create black surface and append it to the associated fullscreen surface.
1766 * Handle size dismatch and positioning according to the method. */
1767static void
1768shell_configure_fullscreen(struct shell_surface *shsurf)
1769{
1770 struct weston_output *output = shsurf->fullscreen_output;
1771 struct weston_surface *surface = shsurf->surface;
1772 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001773 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03001774 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08001775
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001776 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1777 restore_output_mode(output);
1778
Alex Wu4539b082012-03-01 12:57:46 +08001779 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001780 shsurf->fullscreen.black_surface =
1781 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001782 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001783 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001784 output->width,
1785 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001786
1787 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1788 wl_list_insert(&surface->layer_link,
1789 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001790 shsurf->fullscreen.black_surface->output = output;
1791
Giulio Camuffob8366642013-04-25 13:57:46 +03001792 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1793 &surf_width, &surf_height);
1794
Alex Wu4539b082012-03-01 12:57:46 +08001795 switch (shsurf->fullscreen.type) {
1796 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001797 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001798 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001799 break;
1800 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001801 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03001802 if (output->width == surf_width &&
1803 output->height == surf_height) {
1804 weston_surface_set_position(surface, output->x - surf_x,
1805 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00001806 break;
1807 }
1808
Alex Wu4539b082012-03-01 12:57:46 +08001809 matrix = &shsurf->fullscreen.transform.matrix;
1810 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001811
Scott Moreau1bad5db2012-08-18 01:04:05 -06001812 output_aspect = (float) output->width /
1813 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001814 surface_aspect = (float) surface->geometry.width /
1815 (float) surface->geometry.height;
1816 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001817 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03001818 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001819 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001820 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03001821 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001822
Alex Wu4539b082012-03-01 12:57:46 +08001823 weston_matrix_scale(matrix, scale, scale, 1);
1824 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001825 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001826 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03001827 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1828 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001829 weston_surface_set_position(surface, x, y);
1830
Alex Wu4539b082012-03-01 12:57:46 +08001831 break;
1832 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001833 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001834 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02001835 surf_width * surface->buffer_scale,
1836 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08001837 shsurf->fullscreen.framerate};
1838
Alexander Larsson355748e2013-05-28 16:23:38 +02001839 if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
Alexander Larsson690f7182013-05-28 16:23:32 +02001840 weston_surface_set_position(surface,
1841 output->x - surf_x,
1842 output->y - surf_y);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001843 weston_surface_configure(shsurf->fullscreen.black_surface,
Giulio Camuffob8366642013-04-25 13:57:46 +03001844 output->x - surf_x,
1845 output->y - surf_y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001846 output->width,
1847 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001848 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02001849 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001850 restore_output_mode(output);
Alexander Larssond622ed32013-05-28 16:23:40 +02001851 center_on_output(surface, output);
1852 }
Alex Wubd3354b2012-04-17 17:20:49 +08001853 }
Alex Wu4539b082012-03-01 12:57:46 +08001854 break;
1855 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Alexander Larssond622ed32013-05-28 16:23:40 +02001856 center_on_output(surface, output);
Alex Wu4539b082012-03-01 12:57:46 +08001857 break;
1858 default:
1859 break;
1860 }
1861}
1862
1863/* make the fullscreen and black surface at the top */
1864static void
1865shell_stack_fullscreen(struct shell_surface *shsurf)
1866{
Alex Wubd3354b2012-04-17 17:20:49 +08001867 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001868 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001869 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001870
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001871 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001872 wl_list_insert(&shell->fullscreen_layer.surface_list,
1873 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001874 weston_surface_damage(surface);
1875
1876 if (!shsurf->fullscreen.black_surface)
1877 shsurf->fullscreen.black_surface =
1878 create_black_surface(surface->compositor,
1879 surface,
1880 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001881 output->width,
1882 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001883
1884 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001885 wl_list_insert(&surface->layer_link,
1886 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001887 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001888}
1889
1890static void
1891shell_map_fullscreen(struct shell_surface *shsurf)
1892{
Alex Wu4539b082012-03-01 12:57:46 +08001893 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001894 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001895}
1896
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001897static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001898set_fullscreen(struct shell_surface *shsurf,
1899 uint32_t method,
1900 uint32_t framerate,
1901 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001902{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001903 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001904
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001905 if (output)
1906 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001907 else if (es->output)
1908 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001909 else
1910 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001911
Alex Wu4539b082012-03-01 12:57:46 +08001912 shsurf->fullscreen_output = shsurf->output;
1913 shsurf->fullscreen.type = method;
1914 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001915 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001916
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001917 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001918 shsurf->output->width,
1919 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001920}
1921
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001922static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001923shell_surface_set_fullscreen(struct wl_client *client,
1924 struct wl_resource *resource,
1925 uint32_t method,
1926 uint32_t framerate,
1927 struct wl_resource *output_resource)
1928{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001929 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001930 struct weston_output *output;
1931
1932 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001933 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001934 else
1935 output = NULL;
1936
1937 set_fullscreen(shsurf, method, framerate, output);
1938}
1939
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001940static void
1941set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
1942{
1943 /* XXX: using the same fields for transient type */
1944 shsurf->transient.x = x;
1945 shsurf->transient.y = y;
1946 shsurf->transient.flags = flags;
1947 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
1948}
1949
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001950static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001951
1952static void
1953destroy_shell_seat(struct wl_listener *listener, void *data)
1954{
1955 struct shell_seat *shseat =
1956 container_of(listener,
1957 struct shell_seat, seat_destroy_listener);
1958 struct shell_surface *shsurf, *prev = NULL;
1959
1960 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001961 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001962 shseat->popup_grab.client = NULL;
1963
1964 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1965 shsurf->popup.shseat = NULL;
1966 if (prev) {
1967 wl_list_init(&prev->popup.grab_link);
1968 }
1969 prev = shsurf;
1970 }
1971 wl_list_init(&prev->popup.grab_link);
1972 }
1973
1974 wl_list_remove(&shseat->seat_destroy_listener.link);
1975 free(shseat);
1976}
1977
1978static struct shell_seat *
1979create_shell_seat(struct weston_seat *seat)
1980{
1981 struct shell_seat *shseat;
1982
1983 shseat = calloc(1, sizeof *shseat);
1984 if (!shseat) {
1985 weston_log("no memory to allocate shell seat\n");
1986 return NULL;
1987 }
1988
1989 shseat->seat = seat;
1990 wl_list_init(&shseat->popup_grab.surfaces_list);
1991
1992 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1993 wl_signal_add(&seat->destroy_signal,
1994 &shseat->seat_destroy_listener);
1995
1996 return shseat;
1997}
1998
1999static struct shell_seat *
2000get_shell_seat(struct weston_seat *seat)
2001{
2002 struct wl_listener *listener;
2003
2004 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2005 if (listener == NULL)
2006 return create_shell_seat(seat);
2007
2008 return container_of(listener,
2009 struct shell_seat, seat_destroy_listener);
2010}
2011
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002012static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002013popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002014{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002015 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002016 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002017 struct shell_seat *shseat =
2018 container_of(grab, struct shell_seat, popup_grab.grab);
2019 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002020 wl_fixed_t sx, sy;
2021
2022 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2023 pointer->x, pointer->y,
2024 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002025
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002026 if (surface && wl_resource_get_client(surface->resource) == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002027 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002028 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002029 weston_pointer_set_focus(pointer, NULL,
2030 wl_fixed_from_int(0),
2031 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002032 }
2033}
2034
2035static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002036popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002037{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002038 struct weston_pointer *pointer = grab->pointer;
2039 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002040
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002041 if (pointer->focus_resource) {
2042 weston_surface_from_global_fixed(pointer->focus,
2043 pointer->x, pointer->y,
2044 &sx, &sy);
2045 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2046 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002047}
2048
2049static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002050popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002051 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002052{
2053 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002054 struct shell_seat *shseat =
2055 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002056 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002057 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002058 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002059
Daniel Stone37816df2012-05-16 18:45:18 +01002060 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002061 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002062 display = wl_client_get_display(wl_resource_get_client(resource));
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002063 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01002064 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01002065 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002066 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002067 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002068 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002069 }
2070
Daniel Stone4dbadb12012-05-30 16:31:51 +01002071 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002072 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002073}
2074
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002075static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002076 popup_grab_focus,
2077 popup_grab_motion,
2078 popup_grab_button,
2079};
2080
2081static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002082popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002083{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002084 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002085 struct shell_seat *shseat =
2086 container_of(grab, struct shell_seat, popup_grab.grab);
2087 struct shell_surface *shsurf;
2088 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002089
2090 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002091 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002092 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002093 shseat->popup_grab.grab.interface = NULL;
2094 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002095 /* Send the popup_done event to all the popups open */
2096 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002097 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002098 shsurf->popup.shseat = NULL;
2099 if (prev) {
2100 wl_list_init(&prev->popup.grab_link);
2101 }
2102 prev = shsurf;
2103 }
2104 wl_list_init(&prev->popup.grab_link);
2105 wl_list_init(&shseat->popup_grab.surfaces_list);
2106 }
2107}
2108
2109static void
2110add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2111{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002112 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002113
2114 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002115 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002116 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002117 /* We must make sure here that this popup was opened after
2118 * a mouse press, and not just by moving around with other
2119 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002120 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002121 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002122
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002123 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002124 }
2125 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2126}
2127
2128static void
2129remove_popup_grab(struct shell_surface *shsurf)
2130{
2131 struct shell_seat *shseat = shsurf->popup.shseat;
2132
2133 wl_list_remove(&shsurf->popup.grab_link);
2134 wl_list_init(&shsurf->popup.grab_link);
2135 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002136 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002137 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002138 }
2139}
2140
2141static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002142shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002143{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002144 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002145 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002146 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147
2148 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149
Pekka Paalanen483243f2013-03-08 14:56:50 +02002150 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002151 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2152 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002153
Kristian Høgsberge3148752013-05-06 23:19:49 -04002154 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002155 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002156 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002157 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002158 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002159 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002160}
2161
2162static void
2163shell_surface_set_popup(struct wl_client *client,
2164 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002165 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002166 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002167 struct wl_resource *parent_resource,
2168 int32_t x, int32_t y, uint32_t flags)
2169{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002170 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002171
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002172 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002173 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002174 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002175 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002176 shsurf->popup.x = x;
2177 shsurf->popup.y = y;
2178}
2179
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002180static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002181 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002182 shell_surface_move,
2183 shell_surface_resize,
2184 shell_surface_set_toplevel,
2185 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002186 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002187 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002188 shell_surface_set_maximized,
2189 shell_surface_set_title,
2190 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002191};
2192
2193static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002194destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002195{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002196 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2197
Giulio Camuffo5085a752013-03-25 21:42:45 +01002198 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2199 remove_popup_grab(shsurf);
2200 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002201
Alex Wubd3354b2012-04-17 17:20:49 +08002202 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002203 shell_surface_is_top_fullscreen(shsurf))
2204 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002205
Alex Wuaa08e2d2012-03-05 11:01:40 +08002206 if (shsurf->fullscreen.black_surface)
2207 weston_surface_destroy(shsurf->fullscreen.black_surface);
2208
Alex Wubd3354b2012-04-17 17:20:49 +08002209 /* As destroy_resource() use wl_list_for_each_safe(),
2210 * we can always remove the listener.
2211 */
2212 wl_list_remove(&shsurf->surface_destroy_listener.link);
2213 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002214 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002215 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002216
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002217 wl_list_remove(&shsurf->link);
2218 free(shsurf);
2219}
2220
2221static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002222shell_destroy_shell_surface(struct wl_resource *resource)
2223{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002224 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002225
2226 destroy_shell_surface(shsurf);
2227}
2228
2229static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002230shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002231{
2232 struct shell_surface *shsurf = container_of(listener,
2233 struct shell_surface,
2234 surface_destroy_listener);
2235
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002236 if (shsurf->resource)
2237 wl_resource_destroy(shsurf->resource);
2238 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002239 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002240}
2241
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002242static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002243shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002244
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002245static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002246get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002247{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002248 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002249 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002250 else
2251 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002252}
2253
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002254static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002255create_shell_surface(void *shell, struct weston_surface *surface,
2256 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002257{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002258 struct shell_surface *shsurf;
2259
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002260 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002261 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002262 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002263 }
2264
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002265 shsurf = calloc(1, sizeof *shsurf);
2266 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002267 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002268 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002269 }
2270
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002271 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002272 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002273
Tiago Vignattibc052c92012-04-19 16:18:18 +03002274 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002275 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002276 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002277 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002278 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002279 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2280 shsurf->fullscreen.framerate = 0;
2281 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002282 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002283 wl_list_init(&shsurf->fullscreen.transform.link);
2284
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002285 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002286 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002287 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002288 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002289
2290 /* init link so its safe to always remove it in destroy_shell_surface */
2291 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002292 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002293
Pekka Paalanen460099f2012-01-20 16:48:25 +02002294 /* empty when not in use */
2295 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002296 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002298 wl_list_init(&shsurf->workspace_transform.link);
2299
Pekka Paalanen98262232011-12-01 10:42:22 +02002300 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002301 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002302
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002303 shsurf->client = client;
2304
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002305 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002306}
2307
2308static void
2309shell_get_shell_surface(struct wl_client *client,
2310 struct wl_resource *resource,
2311 uint32_t id,
2312 struct wl_resource *surface_resource)
2313{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002314 struct weston_surface *surface =
2315 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002316 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002317 struct shell_surface *shsurf;
2318
2319 if (get_shell_surface(surface)) {
2320 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002321 WL_DISPLAY_ERROR_INVALID_OBJECT,
2322 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002323 return;
2324 }
2325
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002326 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002327 if (!shsurf) {
2328 wl_resource_post_error(surface_resource,
2329 WL_DISPLAY_ERROR_INVALID_OBJECT,
2330 "surface->configure already set");
2331 return;
2332 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002333
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002334 shsurf->resource = wl_client_add_object(client,
2335 &wl_shell_surface_interface,
2336 &shell_surface_implementation,
2337 id, shsurf);
2338 wl_resource_set_destructor(shsurf->resource,
2339 shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002340}
2341
2342static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002343 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002344};
2345
Kristian Høgsberg07937562011-04-12 17:25:42 -04002346static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002347shell_fade(struct desktop_shell *shell, enum fade_type type);
2348
2349static int
2350screensaver_timeout(void *data)
2351{
2352 struct desktop_shell *shell = data;
2353
2354 shell_fade(shell, FADE_OUT);
2355
2356 return 1;
2357}
2358
2359static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002360handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002361{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002362 struct desktop_shell *shell =
2363 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002364
Pekka Paalanen18027e52011-12-02 16:31:49 +02002365 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002366
2367 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002368 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002369}
2370
2371static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002372launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002373{
2374 if (shell->screensaver.binding)
2375 return;
2376
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002377 if (!shell->screensaver.path) {
2378 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002379 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002380 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002381
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002382 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002383 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002384 return;
2385 }
2386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002387 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002388 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002389 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002390 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002391}
2392
2393static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002394terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002395{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002396 if (shell->screensaver.process.pid == 0)
2397 return;
2398
2399 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002400}
2401
2402static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002403configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002404{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002405 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002406
Giulio Camuffo184df502013-02-21 11:29:21 +01002407 if (width == 0)
2408 return;
2409
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002410 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2411 if (s->output == es->output && s != es) {
2412 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002413 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002414 }
2415 }
2416
Giulio Camuffo184df502013-02-21 11:29:21 +01002417 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002418
2419 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002420 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002421 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002422 }
2423}
2424
2425static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002426background_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 -04002427{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002428 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002429
Giulio Camuffo184df502013-02-21 11:29:21 +01002430 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002431}
2432
2433static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002434desktop_shell_set_background(struct wl_client *client,
2435 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002436 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002437 struct wl_resource *surface_resource)
2438{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002439 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002440 struct weston_surface *surface =
2441 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002442
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002443 if (surface->configure) {
2444 wl_resource_post_error(surface_resource,
2445 WL_DISPLAY_ERROR_INVALID_OBJECT,
2446 "surface role already assigned");
2447 return;
2448 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002449
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002450 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002451 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002452 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002453 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002454 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002455 surface->output->width,
2456 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002457}
2458
2459static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002460panel_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 -04002461{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002462 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002463
Giulio Camuffo184df502013-02-21 11:29:21 +01002464 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002465}
2466
2467static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002468desktop_shell_set_panel(struct wl_client *client,
2469 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002470 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002471 struct wl_resource *surface_resource)
2472{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002473 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002474 struct weston_surface *surface =
2475 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002476
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002477 if (surface->configure) {
2478 wl_resource_post_error(surface_resource,
2479 WL_DISPLAY_ERROR_INVALID_OBJECT,
2480 "surface role already assigned");
2481 return;
2482 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002483
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002484 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002485 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002486 surface->output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002487 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002488 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002489 surface->output->width,
2490 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002491}
2492
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002493static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002494lock_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 -04002495{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002496 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002497
Giulio Camuffo184df502013-02-21 11:29:21 +01002498 if (width == 0)
2499 return;
2500
Kristian Høgsbergb9f1c522013-05-14 20:59:02 -04002501 surface->geometry.width = width;
2502 surface->geometry.height = height;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002503 center_on_output(surface, get_default_output(shell->compositor));
2504
2505 if (!weston_surface_is_mapped(surface)) {
2506 wl_list_insert(&shell->lock_layer.surface_list,
2507 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002508 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002509 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002510 }
2511}
2512
2513static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002514handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002515{
Tiago Vignattibe143262012-04-16 17:31:41 +03002516 struct desktop_shell *shell =
2517 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002518
Martin Minarik6d118362012-06-07 18:01:59 +02002519 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002520 shell->lock_surface = NULL;
2521}
2522
2523static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002524desktop_shell_set_lock_surface(struct wl_client *client,
2525 struct wl_resource *resource,
2526 struct wl_resource *surface_resource)
2527{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002528 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002529 struct weston_surface *surface =
2530 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002531
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002532 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002533
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002534 if (!shell->locked)
2535 return;
2536
Pekka Paalanen98262232011-12-01 10:42:22 +02002537 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002538
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002539 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002540 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002541 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002542
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002543 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002544 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002545}
2546
2547static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002548resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002549{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002550 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002551
Pekka Paalanen77346a62011-11-30 16:26:35 +02002552 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002553
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002554 wl_list_remove(&shell->lock_layer.link);
2555 wl_list_insert(&shell->compositor->cursor_layer.link,
2556 &shell->fullscreen_layer.link);
2557 wl_list_insert(&shell->fullscreen_layer.link,
2558 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002559 if (shell->showing_input_panels) {
2560 wl_list_insert(&shell->panel_layer.link,
2561 &shell->input_panel_layer.link);
2562 wl_list_insert(&shell->input_panel_layer.link,
2563 &ws->layer.link);
2564 } else {
2565 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2566 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002567
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002568 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002569
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002570 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002571 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002572 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002573}
2574
2575static void
2576desktop_shell_unlock(struct wl_client *client,
2577 struct wl_resource *resource)
2578{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002579 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002580
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002581 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002582
2583 if (shell->locked)
2584 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002585}
2586
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002587static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002588desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002589 struct wl_resource *resource,
2590 struct wl_resource *surface_resource)
2591{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002592 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002593
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002594 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002595}
2596
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002597static void
2598desktop_shell_desktop_ready(struct wl_client *client,
2599 struct wl_resource *resource)
2600{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002601 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002602
2603 shell_fade_startup(shell);
2604}
2605
Kristian Høgsberg75840622011-09-06 13:48:16 -04002606static const struct desktop_shell_interface desktop_shell_implementation = {
2607 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002608 desktop_shell_set_panel,
2609 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002610 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002611 desktop_shell_set_grab_surface,
2612 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002613};
2614
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002615static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002616get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002617{
2618 struct shell_surface *shsurf;
2619
2620 shsurf = get_shell_surface(surface);
2621 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002622 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002623 return shsurf->type;
2624}
2625
Kristian Høgsberg75840622011-09-06 13:48:16 -04002626static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002627move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002628{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002629 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002630 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002631 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002632 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002633
Pekka Paalanen01388e22013-04-25 13:57:44 +03002634 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002635 if (surface == NULL)
2636 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002637
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002638 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002639 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2640 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002641 return;
2642
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002643 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002644}
2645
2646static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002647resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002648{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002649 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002650 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002651 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002652 uint32_t edges = 0;
2653 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002654 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002655
Pekka Paalanen01388e22013-04-25 13:57:44 +03002656 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002657 if (surface == NULL)
2658 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002659
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002660 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002661 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2662 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002663 return;
2664
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002665 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002666 wl_fixed_to_int(seat->pointer->grab_x),
2667 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002668 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002669
Pekka Paalanen60921e52012-01-25 15:55:43 +02002670 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002671 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002672 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002673 edges |= 0;
2674 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002675 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002676
Pekka Paalanen60921e52012-01-25 15:55:43 +02002677 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002678 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002679 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002680 edges |= 0;
2681 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002682 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002683
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002684 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002685}
2686
2687static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002688surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002689 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002690{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002691 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002692 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002693 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002694 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002695 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002696
Pekka Paalanen01388e22013-04-25 13:57:44 +03002697 /* XXX: broken for windows containing sub-surfaces */
2698 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002699 if (surface == NULL)
2700 return;
2701
2702 shsurf = get_shell_surface(surface);
2703 if (!shsurf)
2704 return;
2705
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002706 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002707
Scott Moreau02709af2012-05-22 01:54:10 -06002708 if (surface->alpha > 1.0)
2709 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002710 if (surface->alpha < step)
2711 surface->alpha = step;
2712
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002713 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002714 weston_surface_damage(surface);
2715}
2716
2717static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002718do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002719 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002720{
Daniel Stone37816df2012-05-16 18:45:18 +01002721 struct weston_seat *ws = (struct weston_seat *) seat;
2722 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002723 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002724 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002725
2726 wl_list_for_each(output, &compositor->output_list, link) {
2727 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002728 wl_fixed_to_double(seat->pointer->x),
2729 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002730 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002731 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002732 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002733 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002734 increment = -output->zoom.increment;
2735 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002736 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002737 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002738 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002739 else
2740 increment = 0;
2741
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002742 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002743
Scott Moreaue6603982012-06-11 13:07:51 -06002744 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002745 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002746 else if (output->zoom.level > output->zoom.max_level)
2747 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002748 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002749 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002750 output->disable_planes++;
2751 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002752
Scott Moreaue6603982012-06-11 13:07:51 -06002753 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002754
Scott Moreau8dacaab2012-06-17 18:10:58 -06002755 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002756 }
2757 }
2758}
2759
Scott Moreauccbf29d2012-02-22 14:21:41 -07002760static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002761zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002762 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002763{
2764 do_zoom(seat, time, 0, axis, value);
2765}
2766
2767static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002768zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002769 void *data)
2770{
2771 do_zoom(seat, time, key, 0, 0);
2772}
2773
2774static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002775terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002776 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002777{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002778 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002779
Daniel Stone325fc2d2012-05-30 16:31:58 +01002780 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002781}
2782
2783static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002784rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002785{
2786 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002787 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002788 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002789 struct shell_surface *shsurf = rotate->base.shsurf;
2790 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002791 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002792
2793 if (!shsurf)
2794 return;
2795
2796 surface = shsurf->surface;
2797
2798 cx = 0.5f * surface->geometry.width;
2799 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002800
Daniel Stone37816df2012-05-16 18:45:18 +01002801 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2802 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002803 r = sqrtf(dx * dx + dy * dy);
2804
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002805 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002806 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002807
2808 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002809 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002810 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002811
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002812 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002813 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002814
2815 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002816 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002817 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002818 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002819 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002820
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002821 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002822 &shsurf->surface->geometry.transformation_list,
2823 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002824 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002825 wl_list_init(&shsurf->rotation.transform.link);
2826 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002827 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002828 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002829
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002830 /* We need to adjust the position of the surface
2831 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002832 cposx = surface->geometry.x + cx;
2833 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002834 dposx = rotate->center.x - cposx;
2835 dposy = rotate->center.y - cposy;
2836 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002837 weston_surface_set_position(surface,
2838 surface->geometry.x + dposx,
2839 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002840 }
2841
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002842 /* Repaint implies weston_surface_update_transform(), which
2843 * lazily applies the damage due to rotation update.
2844 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002845 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002846}
2847
2848static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002849rotate_grab_button(struct weston_pointer_grab *grab,
2850 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002851{
2852 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002853 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002854 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002855 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002856 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002857
Daniel Stone4dbadb12012-05-30 16:31:51 +01002858 if (pointer->button_count == 0 &&
2859 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002860 if (shsurf)
2861 weston_matrix_multiply(&shsurf->rotation.rotation,
2862 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002863 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002864 free(rotate);
2865 }
2866}
2867
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002868static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002869 noop_grab_focus,
2870 rotate_grab_motion,
2871 rotate_grab_button,
2872};
2873
2874static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002875surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002876{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002877 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002878 float dx, dy;
2879 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002880
Pekka Paalanen460099f2012-01-20 16:48:25 +02002881 rotate = malloc(sizeof *rotate);
2882 if (!rotate)
2883 return;
2884
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002885 weston_surface_to_global_float(surface->surface,
2886 surface->surface->geometry.width / 2,
2887 surface->surface->geometry.height / 2,
2888 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002889
Daniel Stone37816df2012-05-16 18:45:18 +01002890 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2891 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002892 r = sqrtf(dx * dx + dy * dy);
2893 if (r > 20.0f) {
2894 struct weston_matrix inverse;
2895
2896 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002897 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002898 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002899
2900 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002901 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002902 } else {
2903 weston_matrix_init(&surface->rotation.rotation);
2904 weston_matrix_init(&rotate->rotation);
2905 }
2906
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002907 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2908 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002909}
2910
2911static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002912rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002913 void *data)
2914{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002915 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002916 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002917 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002918 struct shell_surface *surface;
2919
Pekka Paalanen01388e22013-04-25 13:57:44 +03002920 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002921 if (base_surface == NULL)
2922 return;
2923
2924 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002925 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2926 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002927 return;
2928
2929 surface_rotate(surface, seat);
2930}
2931
2932static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002933lower_fullscreen_layer(struct desktop_shell *shell)
2934{
2935 struct workspace *ws;
2936 struct weston_surface *surface, *prev;
2937
2938 ws = get_current_workspace(shell);
2939 wl_list_for_each_reverse_safe(surface, prev,
2940 &shell->fullscreen_layer.surface_list,
2941 layer_link)
2942 weston_surface_restack(surface, &ws->layer.surface_list);
2943}
2944
2945static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002946activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002947 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002948{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002949 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002950 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002951 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002952
Pekka Paalanen01388e22013-04-25 13:57:44 +03002953 main_surface = weston_surface_get_main_surface(es);
2954
Daniel Stone37816df2012-05-16 18:45:18 +01002955 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002956
Jonas Ådahl8538b222012-08-29 22:13:03 +02002957 state = ensure_focus_state(shell, seat);
2958 if (state == NULL)
2959 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002960
2961 state->keyboard_focus = es;
2962 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002963 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002964
Pekka Paalanen01388e22013-04-25 13:57:44 +03002965 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002966 case SHELL_SURFACE_FULLSCREEN:
2967 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002968 shell_stack_fullscreen(get_shell_surface(main_surface));
2969 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002970 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002971 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002972 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002973 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002974 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002975 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002976 }
2977}
2978
Alex Wu21858432012-04-01 20:13:08 +08002979/* no-op func for checking black surface */
2980static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002981black_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 +08002982{
2983}
2984
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002985static bool
Alex Wu21858432012-04-01 20:13:08 +08002986is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2987{
2988 if (es->configure == black_surface_configure) {
2989 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002990 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002991 return true;
2992 }
2993 return false;
2994}
2995
Kristian Høgsberg75840622011-09-06 13:48:16 -04002996static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002997click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002998 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002999{
Daniel Stone37816df2012-05-16 18:45:18 +01003000 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003001 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003002 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003003 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003004
Daniel Stone37816df2012-05-16 18:45:18 +01003005 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003006 if (!focus)
3007 return;
3008
Pekka Paalanen01388e22013-04-25 13:57:44 +03003009 if (is_black_surface(focus, &main_surface))
3010 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003011
Pekka Paalanen01388e22013-04-25 13:57:44 +03003012 main_surface = weston_surface_get_main_surface(focus);
3013 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003014 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003015
Daniel Stone325fc2d2012-05-30 16:31:58 +01003016 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01003017 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003018}
3019
3020static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003021lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003022{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003023 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003024
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003025 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003026 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003027 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003028 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003029
3030 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003031
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003032 /* Hide all surfaces by removing the fullscreen, panel and
3033 * toplevel layers. This way nothing else can show or receive
3034 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003035
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003036 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003037 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003038 if (shell->showing_input_panels)
3039 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003040 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003041 wl_list_insert(&shell->compositor->cursor_layer.link,
3042 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003043
Pekka Paalanen77346a62011-11-30 16:26:35 +02003044 launch_screensaver(shell);
3045
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003046 /* TODO: disable bindings that should not work while locked. */
3047
3048 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003049}
3050
3051static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003052unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003053{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003054 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003055 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003056 return;
3057 }
3058
3059 /* If desktop-shell client has gone away, unlock immediately. */
3060 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003061 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003062 return;
3063 }
3064
3065 if (shell->prepare_event_sent)
3066 return;
3067
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003068 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003069 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003070}
3071
3072static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003073shell_fade_done(struct weston_surface_animation *animation, void *data)
3074{
3075 struct desktop_shell *shell = data;
3076
3077 shell->fade.animation = NULL;
3078
3079 switch (shell->fade.type) {
3080 case FADE_IN:
3081 weston_surface_destroy(shell->fade.surface);
3082 shell->fade.surface = NULL;
3083 break;
3084 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003085 lock(shell);
3086 break;
3087 }
3088}
3089
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003090static struct weston_surface *
3091shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003092{
3093 struct weston_compositor *compositor = shell->compositor;
3094 struct weston_surface *surface;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003095
3096 surface = weston_surface_create(compositor);
3097 if (!surface)
3098 return NULL;
3099
3100 weston_surface_configure(surface, 0, 0, 8192, 8192);
3101 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3102 wl_list_insert(&compositor->fade_layer.surface_list,
3103 &surface->layer_link);
3104 pixman_region32_init(&surface->input);
3105
3106 return surface;
3107}
3108
3109static void
3110shell_fade(struct desktop_shell *shell, enum fade_type type)
3111{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003112 float tint;
3113
3114 switch (type) {
3115 case FADE_IN:
3116 tint = 0.0;
3117 break;
3118 case FADE_OUT:
3119 tint = 1.0;
3120 break;
3121 default:
3122 weston_log("shell: invalid fade type\n");
3123 return;
3124 }
3125
3126 shell->fade.type = type;
3127
3128 if (shell->fade.surface == NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003129 shell->fade.surface = shell_fade_create_surface(shell);
3130 if (!shell->fade.surface)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003131 return;
3132
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003133 shell->fade.surface->alpha = 1.0 - tint;
3134 weston_surface_update_transform(shell->fade.surface);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003135 }
3136
3137 if (shell->fade.animation)
3138 weston_fade_update(shell->fade.animation,
3139 shell->fade.surface->alpha, tint, 30.0);
3140 else
3141 shell->fade.animation =
3142 weston_fade_run(shell->fade.surface,
3143 1.0 - tint, tint, 30.0,
3144 shell_fade_done, shell);
3145}
3146
3147static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003148do_shell_fade_startup(void *data)
3149{
3150 struct desktop_shell *shell = data;
3151
3152 shell_fade(shell, FADE_IN);
3153}
3154
3155static void
3156shell_fade_startup(struct desktop_shell *shell)
3157{
3158 struct wl_event_loop *loop;
3159
3160 if (!shell->fade.startup_timer)
3161 return;
3162
3163 wl_event_source_remove(shell->fade.startup_timer);
3164 shell->fade.startup_timer = NULL;
3165
3166 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3167 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3168}
3169
3170static int
3171fade_startup_timeout(void *data)
3172{
3173 struct desktop_shell *shell = data;
3174
3175 shell_fade_startup(shell);
3176 return 0;
3177}
3178
3179static void
3180shell_fade_init(struct desktop_shell *shell)
3181{
3182 /* Make compositor output all black, and wait for the desktop-shell
3183 * client to signal it is ready, then fade in. The timer triggers a
3184 * fade-in, in case the desktop-shell client takes too long.
3185 */
3186
3187 struct wl_event_loop *loop;
3188
3189 if (shell->fade.surface != NULL) {
3190 weston_log("%s: warning: fade surface already exists\n",
3191 __func__);
3192 return;
3193 }
3194
3195 shell->fade.surface = shell_fade_create_surface(shell);
3196 if (!shell->fade.surface)
3197 return;
3198
3199 weston_surface_update_transform(shell->fade.surface);
3200 weston_surface_damage(shell->fade.surface);
3201
3202 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3203 shell->fade.startup_timer =
3204 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3205 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3206}
3207
3208static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003209idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003210{
3211 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003212 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003213
3214 shell_fade(shell, FADE_OUT);
3215 /* lock() is called from shell_fade_done() */
3216}
3217
3218static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003219wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003220{
3221 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003222 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003223
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003224 unlock(shell);
3225}
3226
3227static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003228show_input_panels(struct wl_listener *listener, void *data)
3229{
3230 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003231 container_of(listener, struct desktop_shell,
3232 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003233 struct input_panel_surface *surface, *next;
3234 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003235
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003236 shell->text_input.surface = (struct weston_surface*)data;
3237
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003238 if (shell->showing_input_panels)
3239 return;
3240
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003241 shell->showing_input_panels = true;
3242
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003243 if (!shell->locked)
3244 wl_list_insert(&shell->panel_layer.link,
3245 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003246
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003247 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003248 &shell->input_panel.surfaces, link) {
3249 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003250 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003251 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003252 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003253 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003254 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003255 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003256 weston_surface_damage(ws);
3257 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003258 }
3259}
3260
3261static void
3262hide_input_panels(struct wl_listener *listener, void *data)
3263{
3264 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003265 container_of(listener, struct desktop_shell,
3266 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003267 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003268
Jan Arne Petersen61381972013-01-31 15:52:21 +01003269 if (!shell->showing_input_panels)
3270 return;
3271
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003272 shell->showing_input_panels = false;
3273
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003274 if (!shell->locked)
3275 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003276
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003277 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003278 &shell->input_panel_layer.surface_list, layer_link)
3279 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003280}
3281
3282static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003283update_input_panels(struct wl_listener *listener, void *data)
3284{
3285 struct desktop_shell *shell =
3286 container_of(listener, struct desktop_shell,
3287 update_input_panel_listener);
3288
3289 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3290}
3291
3292static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003293center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003294{
Giulio Camuffob8366642013-04-25 13:57:46 +03003295 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003296 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003297
Giulio Camuffob8366642013-04-25 13:57:46 +03003298 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3299
3300 x = output->x + (output->width - width) / 2 - surf_x / 2;
3301 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003302
3303 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003304}
3305
3306static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003307weston_surface_set_initial_position (struct weston_surface *surface,
3308 struct desktop_shell *shell)
3309{
3310 struct weston_compositor *compositor = shell->compositor;
3311 int ix = 0, iy = 0;
3312 int range_x, range_y;
3313 int dx, dy, x, y, panel_height;
3314 struct weston_output *output, *target_output = NULL;
3315 struct weston_seat *seat;
3316
3317 /* As a heuristic place the new window on the same output as the
3318 * pointer. Falling back to the output containing 0, 0.
3319 *
3320 * TODO: Do something clever for touch too?
3321 */
3322 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003323 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003324 ix = wl_fixed_to_int(seat->pointer->x);
3325 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003326 break;
3327 }
3328 }
3329
3330 wl_list_for_each(output, &compositor->output_list, link) {
3331 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3332 target_output = output;
3333 break;
3334 }
3335 }
3336
3337 if (!target_output) {
3338 weston_surface_set_position(surface, 10 + random() % 400,
3339 10 + random() % 400);
3340 return;
3341 }
3342
3343 /* Valid range within output where the surface will still be onscreen.
3344 * If this is negative it means that the surface is bigger than
3345 * output.
3346 */
3347 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003348 range_x = target_output->width - surface->geometry.width;
3349 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003350 surface->geometry.height;
3351
Rob Bradford4cb88c72012-08-13 15:18:44 +01003352 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003353 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003354 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003355 dx = 0;
3356
3357 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003358 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003359 else
3360 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003361
3362 x = target_output->x + dx;
3363 y = target_output->y + dy;
3364
3365 weston_surface_set_position (surface, x, y);
3366}
3367
3368static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003369map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003370 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003371{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003372 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003373 struct shell_surface *shsurf = get_shell_surface(surface);
3374 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003375 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003376 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003377 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003378 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003379 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003380
Pekka Paalanen60921e52012-01-25 15:55:43 +02003381 surface->geometry.width = width;
3382 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003383 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003384
3385 /* initial positioning, see also configure() */
3386 switch (surface_type) {
3387 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003388 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003389 break;
Alex Wu4539b082012-03-01 12:57:46 +08003390 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003391 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003392 shell_map_fullscreen(shsurf);
3393 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003394 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003395 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003396 panel_height = get_output_panel_height(shell,surface->output);
Giulio Camuffob8366642013-04-25 13:57:46 +03003397 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3398 NULL, NULL);
3399 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3400 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003401 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003402 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003403 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003404 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003405 case SHELL_SURFACE_NONE:
3406 weston_surface_set_position(surface,
3407 surface->geometry.x + sx,
3408 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003409 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003410 default:
3411 ;
3412 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003413
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003414 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003415 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003416 case SHELL_SURFACE_POPUP:
3417 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003418 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003419 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3420 break;
Alex Wu4539b082012-03-01 12:57:46 +08003421 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003422 case SHELL_SURFACE_NONE:
3423 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003424 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003425 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003426 ws = get_current_workspace(shell);
3427 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003428 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003429 }
3430
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003431 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003432 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003433 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3434 surface->output = shsurf->output;
3435 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003436
Juan Zhao7bb92f02011-12-15 11:31:51 -05003437 switch (surface_type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003438 /* XXX: xwayland's using the same fields for transient type */
3439 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003440 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003441 if (shsurf->transient.flags ==
3442 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3443 break;
3444 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003445 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003446 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003447 if (!shell->locked) {
3448 wl_list_for_each(seat, &compositor->seat_list, link)
3449 activate(shell, surface, seat);
3450 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003451 break;
3452 default:
3453 break;
3454 }
3455
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003456 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003457 {
3458 switch (shell->win_animation_type) {
3459 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003460 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003461 break;
3462 case ANIMATION_ZOOM:
3463 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3464 break;
3465 default:
3466 break;
3467 }
3468 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003469}
3470
3471static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003472configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003473 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003474{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003475 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3476 struct shell_surface *shsurf;
Giulio Camuffob8366642013-04-25 13:57:46 +03003477 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003478
Pekka Paalanen77346a62011-11-30 16:26:35 +02003479 shsurf = get_shell_surface(surface);
3480 if (shsurf)
3481 surface_type = shsurf->type;
3482
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003483 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003484
3485 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003486 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003487 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003488 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003489 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003490 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003491 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003492 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3493 NULL, NULL);
3494 surface->geometry.x = surface->output->x - surf_x;
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003495 surface->geometry.y = surface->output->y +
Giulio Camuffob8366642013-04-25 13:57:46 +03003496 get_output_panel_height(shell,surface->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003497 break;
Alex Wu4539b082012-03-01 12:57:46 +08003498 case SHELL_SURFACE_TOPLEVEL:
3499 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003500 default:
3501 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003502 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003503
Alex Wu4539b082012-03-01 12:57:46 +08003504 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003505 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003506 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003507
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003508 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003509 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003510 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003511}
3512
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003513static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003514shell_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 +03003515{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003516 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003517 struct desktop_shell *shell = shsurf->shell;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003518 struct weston_compositor *compositor = shsurf->surface->compositor;
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
Giulio Camuffo5085a752013-03-25 21:42:45 +01003522 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3523 remove_popup_grab(shsurf);
3524 }
3525
Giulio Camuffo184df502013-02-21 11:29:21 +01003526 if (width == 0)
3527 return;
3528
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003529 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003530 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003531 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003532 type_changed = 1;
3533 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003534
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003535 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003536 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003537 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003538 es->geometry.width != width ||
3539 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003540 float from_x, from_y;
3541 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003542
3543 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3544 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3545 configure(shell, es,
3546 es->geometry.x + to_x - from_x,
3547 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003548 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003549 }
3550}
3551
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003552static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003553
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003554static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003555desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003556{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003557 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003558 struct desktop_shell *shell =
3559 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003560
3561 shell->child.process.pid = 0;
3562 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003563
3564 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3565 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003566 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003567 shell->child.deathstamp = time;
3568 shell->child.deathcount = 0;
3569 }
3570
3571 shell->child.deathcount++;
3572 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003573 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003574 return;
3575 }
3576
Martin Minarik6d118362012-06-07 18:01:59 +02003577 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003578 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003579 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003580}
3581
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003582static void
3583launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003584{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003585 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003586 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003587
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003588 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003589 &shell->child.process,
3590 shell_exe,
3591 desktop_shell_sigchld);
3592
3593 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003594 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003595}
3596
3597static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003598bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3599{
Tiago Vignattibe143262012-04-16 17:31:41 +03003600 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003601
3602 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003603 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003604}
3605
Kristian Høgsberg75840622011-09-06 13:48:16 -04003606static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003607unbind_desktop_shell(struct wl_resource *resource)
3608{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003609 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003610
3611 if (shell->locked)
3612 resume_desktop(shell);
3613
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003614 shell->child.desktop_shell = NULL;
3615 shell->prepare_event_sent = false;
3616 free(resource);
3617}
3618
3619static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003620bind_desktop_shell(struct wl_client *client,
3621 void *data, uint32_t version, uint32_t id)
3622{
Tiago Vignattibe143262012-04-16 17:31:41 +03003623 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003624 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003625
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003626 resource = wl_client_add_object(client, &desktop_shell_interface,
3627 &desktop_shell_implementation,
3628 id, shell);
3629
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003630 if (client == shell->child.client) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003631 wl_resource_set_destructor(resource, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003632 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003633
3634 if (version < 2)
3635 shell_fade_startup(shell);
3636
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003637 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003638 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003639
3640 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3641 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003642 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003643}
3644
Pekka Paalanen6e168112011-11-24 11:34:05 +02003645static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003646screensaver_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 -04003647{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003648 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003649
Giulio Camuffo184df502013-02-21 11:29:21 +01003650 if (width == 0)
3651 return;
3652
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003653 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003654 if (!shell->locked)
3655 return;
3656
3657 center_on_output(surface, surface->output);
3658
3659 if (wl_list_empty(&surface->layer_link)) {
3660 wl_list_insert(shell->lock_layer.surface_list.prev,
3661 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003662 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003663 wl_event_source_timer_update(shell->screensaver.timer,
3664 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003665 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003666 }
3667}
3668
3669static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003670screensaver_set_surface(struct wl_client *client,
3671 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003672 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003673 struct wl_resource *output_resource)
3674{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003675 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003676 struct weston_surface *surface =
3677 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003678 struct weston_output *output = wl_resource_get_user_data(output_resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003679
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003680 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003681 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003682 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003683}
3684
3685static const struct screensaver_interface screensaver_implementation = {
3686 screensaver_set_surface
3687};
3688
3689static void
3690unbind_screensaver(struct wl_resource *resource)
3691{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003692 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003693
Pekka Paalanen77346a62011-11-30 16:26:35 +02003694 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003695 free(resource);
3696}
3697
3698static void
3699bind_screensaver(struct wl_client *client,
3700 void *data, uint32_t version, uint32_t id)
3701{
Tiago Vignattibe143262012-04-16 17:31:41 +03003702 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003703 struct wl_resource *resource;
3704
3705 resource = wl_client_add_object(client, &screensaver_interface,
3706 &screensaver_implementation,
3707 id, shell);
3708
Pekka Paalanen77346a62011-11-30 16:26:35 +02003709 if (shell->screensaver.binding == NULL) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003710 wl_resource_set_destructor(resource, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003711 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003712 return;
3713 }
3714
3715 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3716 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003717 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003718}
3719
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003720static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003721input_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 -04003722{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003723 struct input_panel_surface *ip_surface = surface->configure_private;
3724 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003725 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003726 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003727 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003728
Giulio Camuffo184df502013-02-21 11:29:21 +01003729 if (width == 0)
3730 return;
3731
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003732 if (!weston_surface_is_mapped(surface)) {
3733 if (!shell->showing_input_panels)
3734 return;
3735
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003736 show_surface = 1;
3737 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003738
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003739 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003740
3741 if (ip_surface->panel) {
3742 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3743 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3744 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003745 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003746
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003747 x = ip_surface->output->x + (mode->width - width) / 2;
3748 y = ip_surface->output->y + mode->height - height;
3749 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003750
3751 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003752 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003753 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003754
3755 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003756 wl_list_insert(&shell->input_panel_layer.surface_list,
3757 &surface->layer_link);
3758 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003759 weston_surface_damage(surface);
3760 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3761 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003762}
3763
3764static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003765destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003766{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003767 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3768
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003769 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003770 wl_list_remove(&input_panel_surface->link);
3771
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003772 input_panel_surface->surface->configure = NULL;
3773
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003774 free(input_panel_surface);
3775}
3776
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003777static struct input_panel_surface *
3778get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003779{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003780 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003781 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003782 } else {
3783 return NULL;
3784 }
3785}
3786
3787static void
3788input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3789{
3790 struct input_panel_surface *ipsurface = container_of(listener,
3791 struct input_panel_surface,
3792 surface_destroy_listener);
3793
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003794 if (ipsurface->resource) {
3795 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003796 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003797 destroy_input_panel_surface(ipsurface);
3798 }
3799}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003800
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003801static struct input_panel_surface *
3802create_input_panel_surface(struct desktop_shell *shell,
3803 struct weston_surface *surface)
3804{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003805 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003806
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003807 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3808 if (!input_panel_surface)
3809 return NULL;
3810
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003811 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003812 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003813
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003814 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003815
3816 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003817
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003818 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003819 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003820 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003821 &input_panel_surface->surface_destroy_listener);
3822
3823 wl_list_init(&input_panel_surface->link);
3824
3825 return input_panel_surface;
3826}
3827
3828static void
3829input_panel_surface_set_toplevel(struct wl_client *client,
3830 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003831 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003832 uint32_t position)
3833{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003834 struct input_panel_surface *input_panel_surface =
3835 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003836 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003837
3838 wl_list_insert(&shell->input_panel.surfaces,
3839 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003840
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003841 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003842 input_panel_surface->panel = 0;
3843}
3844
3845static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003846input_panel_surface_set_overlay_panel(struct wl_client *client,
3847 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003848{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003849 struct input_panel_surface *input_panel_surface =
3850 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003851 struct desktop_shell *shell = input_panel_surface->shell;
3852
3853 wl_list_insert(&shell->input_panel.surfaces,
3854 &input_panel_surface->link);
3855
3856 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003857}
3858
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003859static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003860 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003861 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003862};
3863
3864static void
3865destroy_input_panel_surface_resource(struct wl_resource *resource)
3866{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003867 struct input_panel_surface *ipsurf =
3868 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003869
3870 destroy_input_panel_surface(ipsurf);
3871}
3872
3873static void
3874input_panel_get_input_panel_surface(struct wl_client *client,
3875 struct wl_resource *resource,
3876 uint32_t id,
3877 struct wl_resource *surface_resource)
3878{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003879 struct weston_surface *surface =
3880 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003881 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003882 struct input_panel_surface *ipsurf;
3883
3884 if (get_input_panel_surface(surface)) {
3885 wl_resource_post_error(surface_resource,
3886 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003887 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003888 return;
3889 }
3890
3891 ipsurf = create_input_panel_surface(shell, surface);
3892 if (!ipsurf) {
3893 wl_resource_post_error(surface_resource,
3894 WL_DISPLAY_ERROR_INVALID_OBJECT,
3895 "surface->configure already set");
3896 return;
3897 }
3898
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003899 ipsurf->resource = wl_client_add_object(client,
3900 &wl_input_panel_surface_interface,
3901 &input_panel_surface_implementation,
3902 id, ipsurf);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003903
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003904 wl_resource_set_destructor(ipsurf->resource,
3905 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003906}
3907
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003908static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003909 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003910};
3911
3912static void
3913unbind_input_panel(struct wl_resource *resource)
3914{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003915 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003916
3917 shell->input_panel.binding = NULL;
3918 free(resource);
3919}
3920
3921static void
3922bind_input_panel(struct wl_client *client,
3923 void *data, uint32_t version, uint32_t id)
3924{
3925 struct desktop_shell *shell = data;
3926 struct wl_resource *resource;
3927
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003928 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003929 &input_panel_implementation,
3930 id, shell);
3931
3932 if (shell->input_panel.binding == NULL) {
Jason Ekstrand51e5b142013-06-14 10:07:58 -05003933 wl_resource_set_destructor(resource, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003934 shell->input_panel.binding = resource;
3935 return;
3936 }
3937
3938 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3939 "interface object already bound");
3940 wl_resource_destroy(resource);
3941}
3942
Kristian Høgsberg07045392012-02-19 18:52:44 -05003943struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003944 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003945 struct weston_surface *current;
3946 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003947 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003948};
3949
3950static void
3951switcher_next(struct switcher *switcher)
3952{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003953 struct weston_surface *surface;
3954 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003955 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003956 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003957
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003958 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003959 switch (get_shell_surface_type(surface)) {
3960 case SHELL_SURFACE_TOPLEVEL:
3961 case SHELL_SURFACE_FULLSCREEN:
3962 case SHELL_SURFACE_MAXIMIZED:
3963 if (first == NULL)
3964 first = surface;
3965 if (prev == switcher->current)
3966 next = surface;
3967 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003968 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003969 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003970 weston_surface_damage(surface);
3971 break;
3972 default:
3973 break;
3974 }
Alex Wu1659daa2012-04-01 20:13:09 +08003975
3976 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003977 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003978 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003979 weston_surface_damage(surface);
3980 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003981 }
3982
3983 if (next == NULL)
3984 next = first;
3985
Alex Wu07b26062012-03-12 16:06:01 +08003986 if (next == NULL)
3987 return;
3988
Kristian Høgsberg07045392012-02-19 18:52:44 -05003989 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003990 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003991
3992 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003993 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003994
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003995 shsurf = get_shell_surface(switcher->current);
3996 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003997 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003998}
3999
4000static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004001switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004002{
4003 struct switcher *switcher =
4004 container_of(listener, struct switcher, listener);
4005
4006 switcher_next(switcher);
4007}
4008
4009static void
Daniel Stone351eb612012-05-31 15:27:47 -04004010switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004011{
Kristian Høgsberg07045392012-02-19 18:52:44 -05004012 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004013 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004014 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004015
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004016 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04004017 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004018 weston_surface_damage(surface);
4019 }
4020
Alex Wu07b26062012-03-12 16:06:01 +08004021 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004022 activate(switcher->shell, switcher->current,
4023 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004024 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004025 weston_keyboard_end_grab(keyboard);
4026 if (keyboard->input_method_resource)
4027 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004028 free(switcher);
4029}
4030
4031static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004032switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004033 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004034{
4035 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004036 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004037
Daniel Stonec9785ea2012-05-30 16:31:52 +01004038 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004039 switcher_next(switcher);
4040}
4041
4042static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004043switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004044 uint32_t mods_depressed, uint32_t mods_latched,
4045 uint32_t mods_locked, uint32_t group)
4046{
4047 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004048 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004049
Daniel Stone351eb612012-05-31 15:27:47 -04004050 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4051 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004052}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004053
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004054static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004055 switcher_key,
4056 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004057};
4058
4059static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004060switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004061 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004062{
Tiago Vignattibe143262012-04-16 17:31:41 +03004063 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004064 struct switcher *switcher;
4065
4066 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004067 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004068 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004069 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004070 wl_list_init(&switcher->listener.link);
4071
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004072 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004073 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004074 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004075 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4076 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004077 switcher_next(switcher);
4078}
4079
Pekka Paalanen3c647232011-12-22 13:43:43 +02004080static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004081backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004082 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004083{
4084 struct weston_compositor *compositor = data;
4085 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004086 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004087
4088 /* TODO: we're limiting to simple use cases, where we assume just
4089 * control on the primary display. We'd have to extend later if we
4090 * ever get support for setting backlights on random desktop LCD
4091 * panels though */
4092 output = get_default_output(compositor);
4093 if (!output)
4094 return;
4095
4096 if (!output->set_backlight)
4097 return;
4098
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004099 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4100 backlight_new = output->backlight_current - 25;
4101 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4102 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004103
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004104 if (backlight_new < 5)
4105 backlight_new = 5;
4106 if (backlight_new > 255)
4107 backlight_new = 255;
4108
4109 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004110 output->set_backlight(output, output->backlight_current);
4111}
4112
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004113struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004114 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004115 struct weston_seat *seat;
4116 uint32_t key[2];
4117 int key_released[2];
4118};
4119
4120static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004121debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004122 uint32_t key, uint32_t state)
4123{
4124 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4125 struct wl_resource *resource;
4126 struct wl_display *display;
4127 uint32_t serial;
4128 int send = 0, terminate = 0;
4129 int check_binding = 1;
4130 int i;
4131
4132 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4133 /* Do not run bindings on key releases */
4134 check_binding = 0;
4135
4136 for (i = 0; i < 2; i++)
4137 if (key == db->key[i])
4138 db->key_released[i] = 1;
4139
4140 if (db->key_released[0] && db->key_released[1]) {
4141 /* All key releases been swalled so end the grab */
4142 terminate = 1;
4143 } else if (key != db->key[0] && key != db->key[1]) {
4144 /* Should not swallow release of other keys */
4145 send = 1;
4146 }
4147 } else if (key == db->key[0] && !db->key_released[0]) {
4148 /* Do not check bindings for the first press of the binding
4149 * key. This allows it to be used as a debug shortcut.
4150 * We still need to swallow this event. */
4151 check_binding = 0;
4152 } else if (db->key[1]) {
4153 /* If we already ran a binding don't process another one since
4154 * we can't keep track of all the binding keys that were
4155 * pressed in order to swallow the release events. */
4156 send = 1;
4157 check_binding = 0;
4158 }
4159
4160 if (check_binding) {
4161 struct weston_compositor *ec = db->seat->compositor;
4162
4163 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4164 key, state)) {
4165 /* We ran a binding so swallow the press and keep the
4166 * grab to swallow the released too. */
4167 send = 0;
4168 terminate = 0;
4169 db->key[1] = key;
4170 } else {
4171 /* Terminate the grab since the key pressed is not a
4172 * debug binding key. */
4173 send = 1;
4174 terminate = 1;
4175 }
4176 }
4177
4178 if (send) {
4179 resource = grab->keyboard->focus_resource;
4180
4181 if (resource) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004182 display = wl_client_get_display(wl_resource_get_client(resource));
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004183 serial = wl_display_next_serial(display);
4184 wl_keyboard_send_key(resource, serial, time, key, state);
4185 }
4186 }
4187
4188 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004189 weston_keyboard_end_grab(grab->keyboard);
4190 if (grab->keyboard->input_method_resource)
4191 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004192 free(db);
4193 }
4194}
4195
4196static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004197debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004198 uint32_t mods_depressed, uint32_t mods_latched,
4199 uint32_t mods_locked, uint32_t group)
4200{
4201 struct wl_resource *resource;
4202
4203 resource = grab->keyboard->focus_resource;
4204 if (!resource)
4205 return;
4206
4207 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4208 mods_latched, mods_locked, group);
4209}
4210
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004211struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004212 debug_binding_key,
4213 debug_binding_modifiers
4214};
4215
4216static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004217debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004218{
4219 struct debug_binding_grab *grab;
4220
4221 grab = calloc(1, sizeof *grab);
4222 if (!grab)
4223 return;
4224
4225 grab->seat = (struct weston_seat *) seat;
4226 grab->key[0] = key;
4227 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004228 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004229}
4230
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004231static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004232force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004233 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004234{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004235 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004236 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004237 struct desktop_shell *shell = data;
4238 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004239 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004240
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004241 focus_surface = seat->keyboard->focus;
4242 if (!focus_surface)
4243 return;
4244
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004245 wl_signal_emit(&compositor->kill_signal, focus_surface);
4246
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004247 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004248 wl_client_get_credentials(client, &pid, NULL, NULL);
4249
4250 /* Skip clients that we launched ourselves (the credentials of
4251 * the socketpair is ours) */
4252 if (pid == getpid())
4253 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004254
Daniel Stone325fc2d2012-05-30 16:31:58 +01004255 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004256}
4257
4258static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004259workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004260 uint32_t key, void *data)
4261{
4262 struct desktop_shell *shell = data;
4263 unsigned int new_index = shell->workspaces.current;
4264
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004265 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004266 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004267 if (new_index != 0)
4268 new_index--;
4269
4270 change_workspace(shell, new_index);
4271}
4272
4273static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004274workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004275 uint32_t key, void *data)
4276{
4277 struct desktop_shell *shell = data;
4278 unsigned int new_index = shell->workspaces.current;
4279
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004280 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004281 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004282 if (new_index < shell->workspaces.num - 1)
4283 new_index++;
4284
4285 change_workspace(shell, new_index);
4286}
4287
4288static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004289workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004290 uint32_t key, void *data)
4291{
4292 struct desktop_shell *shell = data;
4293 unsigned int new_index;
4294
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004295 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004296 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004297 new_index = key - KEY_F1;
4298 if (new_index >= shell->workspaces.num)
4299 new_index = shell->workspaces.num - 1;
4300
4301 change_workspace(shell, new_index);
4302}
4303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004304static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004305workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004306 uint32_t key, void *data)
4307{
4308 struct desktop_shell *shell = data;
4309 unsigned int new_index = shell->workspaces.current;
4310
4311 if (shell->locked)
4312 return;
4313
4314 if (new_index != 0)
4315 new_index--;
4316
4317 take_surface_to_workspace_by_seat(shell, seat, new_index);
4318}
4319
4320static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004321workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004322 uint32_t key, void *data)
4323{
4324 struct desktop_shell *shell = data;
4325 unsigned int new_index = shell->workspaces.current;
4326
4327 if (shell->locked)
4328 return;
4329
4330 if (new_index < shell->workspaces.num - 1)
4331 new_index++;
4332
4333 take_surface_to_workspace_by_seat(shell, seat, new_index);
4334}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004335
4336static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004337shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004338{
Tiago Vignattibe143262012-04-16 17:31:41 +03004339 struct desktop_shell *shell =
4340 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004341 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004342
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004343 if (shell->child.client)
4344 wl_client_destroy(shell->child.client);
4345
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004346 wl_list_remove(&shell->idle_listener.link);
4347 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004348 wl_list_remove(&shell->show_input_panel_listener.link);
4349 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004350
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004351 wl_array_for_each(ws, &shell->workspaces.array)
4352 workspace_destroy(*ws);
4353 wl_array_release(&shell->workspaces.array);
4354
Pekka Paalanen3c647232011-12-22 13:43:43 +02004355 free(shell->screensaver.path);
4356 free(shell);
4357}
4358
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004359static void
4360shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4361{
4362 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004363 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004364
4365 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004366 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4367 MODIFIER_CTRL | MODIFIER_ALT,
4368 terminate_binding, ec);
4369 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4370 click_to_activate_binding,
4371 shell);
4372 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4373 MODIFIER_SUPER | MODIFIER_ALT,
4374 surface_opacity_binding, NULL);
4375 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4376 MODIFIER_SUPER, zoom_axis_binding,
4377 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004378
4379 /* configurable bindings */
4380 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004381 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4382 zoom_key_binding, NULL);
4383 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4384 zoom_key_binding, NULL);
4385 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4386 shell);
4387 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4388 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004389
4390 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4391 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4392 rotate_binding, NULL);
4393
Daniel Stone325fc2d2012-05-30 16:31:58 +01004394 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4395 shell);
4396 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4397 ec);
4398 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4399 backlight_binding, ec);
4400 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4401 ec);
4402 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4403 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004404 weston_compositor_add_key_binding(ec, KEY_K, mod,
4405 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004406 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4407 workspace_up_binding, shell);
4408 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4409 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004410 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4411 workspace_move_surface_up_binding,
4412 shell);
4413 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4414 workspace_move_surface_down_binding,
4415 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004416
4417 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4418 if (shell->workspaces.num > 1) {
4419 num_workspace_bindings = shell->workspaces.num;
4420 if (num_workspace_bindings > 6)
4421 num_workspace_bindings = 6;
4422 for (i = 0; i < num_workspace_bindings; i++)
4423 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4424 workspace_f_binding,
4425 shell);
4426 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004427
4428 /* Debug bindings */
4429 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4430 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004431}
4432
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004433WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004434module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004435 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004436{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004437 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004438 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004439 struct workspace **pws;
4440 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004441 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004442
4443 shell = malloc(sizeof *shell);
4444 if (shell == NULL)
4445 return -1;
4446
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004447 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004448 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004449
4450 shell->destroy_listener.notify = shell_destroy;
4451 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004452 shell->idle_listener.notify = idle_handler;
4453 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4454 shell->wake_listener.notify = wake_handler;
4455 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004456 shell->show_input_panel_listener.notify = show_input_panels;
4457 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4458 shell->hide_input_panel_listener.notify = hide_input_panels;
4459 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004460 shell->update_input_panel_listener.notify = update_input_panels;
4461 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004462 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004463 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004464 ec->shell_interface.create_shell_surface = create_shell_surface;
4465 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004466 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004467 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004468 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004469 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004470 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004471
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004472 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004473
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004474 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4475 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004476 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4477 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004478 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004479
4480 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004481 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004482
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004483 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004484
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004485 for (i = 0; i < shell->workspaces.num; i++) {
4486 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4487 if (pws == NULL)
4488 return -1;
4489
4490 *pws = workspace_create();
4491 if (*pws == NULL)
4492 return -1;
4493 }
4494 activate_workspace(shell, 0);
4495
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004496 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004497 wl_list_init(&shell->workspaces.animation.link);
4498 shell->workspaces.animation.frame = animate_workspace_change_frame;
4499
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004500 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4501 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004502 return -1;
4503
Kristian Høgsberg75840622011-09-06 13:48:16 -04004504 if (wl_display_add_global(ec->wl_display,
4505 &desktop_shell_interface,
4506 shell, bind_desktop_shell) == NULL)
4507 return -1;
4508
Pekka Paalanen6e168112011-11-24 11:34:05 +02004509 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4510 shell, bind_screensaver) == NULL)
4511 return -1;
4512
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004513 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004514 shell, bind_input_panel) == NULL)
4515 return -1;
4516
Jonas Ådahle9d22502012-08-29 22:13:01 +02004517 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4518 shell, bind_workspace_manager) == NULL)
4519 return -1;
4520
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004521 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004522
4523 loop = wl_display_get_event_loop(ec->wl_display);
4524 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004525
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004526 shell->screensaver.timer =
4527 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4528
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004529 wl_list_for_each(seat, &ec->seat_list, link)
4530 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004531
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004532 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004533
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004534 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004535
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004536 return 0;
4537}