blob: 6596e95d82a7d98be72877e9b69efe70d48df321 [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
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010039#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020041#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Juan Zhaoe10d2792012-04-25 19:09:52 +080046enum animation_type {
47 ANIMATION_NONE,
48
49 ANIMATION_ZOOM,
50 ANIMATION_FADE
51};
52
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020053enum fade_type {
54 FADE_IN,
55 FADE_OUT
56};
57
Jonas Ådahl04769742012-06-13 00:01:24 +020058struct focus_state {
59 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040060 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020061 struct weston_surface *keyboard_focus;
62 struct wl_list link;
63 struct wl_listener seat_destroy_listener;
64 struct wl_listener surface_destroy_listener;
65};
66
Jonas Ådahle3cddce2012-06-13 00:01:22 +020067struct workspace {
68 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020069
70 struct wl_list focus_list;
71 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020072};
73
Philipp Brüschweiler88013572012-08-06 13:44:42 +020074struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050075 struct wl_resource *resource;
76 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010077
78 struct desktop_shell *shell;
79
Philipp Brüschweiler88013572012-08-06 13:44:42 +020080 struct wl_list link;
81 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050082 struct weston_view *view;
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
Xiong Zhang6b481422013-10-23 13:58:32 +080089struct shell_output {
90 struct desktop_shell *shell;
91 struct weston_output *output;
92 struct wl_listener destroy_listener;
93 struct wl_list link;
94};
95
Tiago Vignattibe143262012-04-16 17:31:41 +030096struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040098
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020099 struct wl_listener idle_listener;
100 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400101 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200102 struct wl_listener show_input_panel_listener;
103 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200104 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200105
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500106 struct weston_layer fullscreen_layer;
107 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500108 struct weston_layer background_layer;
109 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200110 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500111
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400112 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300113 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400114
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200115 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200117 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200118 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200119
120 unsigned deathcount;
121 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200122 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200123
124 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200125 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200126 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200127
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200128 struct {
129 struct weston_surface *surface;
130 pixman_box32_t cursor_rectangle;
131 } text_input;
132
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400133 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500134 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100135
Pekka Paalanen77346a62011-11-30 16:26:35 +0200136 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200137 struct wl_array array;
138 unsigned int current;
139 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200140
Jonas Ådahle9d22502012-08-29 22:13:01 +0200141 struct wl_list client_list;
142
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200143 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200144 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200145 int anim_dir;
146 uint32_t anim_timestamp;
147 double anim_current;
148 struct workspace *anim_from;
149 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200150 } workspaces;
151
152 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200153 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200154 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200155 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200157 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200158 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500159
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200160 struct {
161 struct wl_resource *binding;
162 struct wl_list surfaces;
163 } input_panel;
164
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200165 struct {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500166 struct weston_view *view;
167 struct weston_view_animation *animation;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200168 enum fade_type type;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300169 struct wl_event_source *startup_timer;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200170 } fade;
171
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300172 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800173 enum animation_type win_animation_type;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700174 enum animation_type startup_animation_type;
Xiong Zhang6b481422013-10-23 13:58:32 +0800175
176 struct wl_listener output_create_listener;
177 struct wl_list output_list;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400178};
179
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500180enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200181 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500182 SHELL_SURFACE_TOPLEVEL,
183 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500184 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800185 SHELL_SURFACE_MAXIMIZED,
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300186 SHELL_SURFACE_POPUP,
187 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200188};
189
Scott Moreauff1db4a2012-04-17 19:06:18 -0600190struct ping_timer {
191 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 uint32_t serial;
193};
194
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200195struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500196 struct wl_resource *resource;
197 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200198
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500200 struct weston_view *view;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200201 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400202 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300203 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200204
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400205 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400206 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500207 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800208 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800209 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700210 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100211
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500212 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500214 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } rotation;
216
217 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100218 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500219 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400221 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500222 } popup;
223
Alex Wu4539b082012-03-01 12:57:46 +0800224 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300225 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400226 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300227 } transient;
228
229 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800230 enum wl_shell_surface_fullscreen_method type;
231 struct weston_transform transform; /* matrix from x, y */
232 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500233 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800234 } fullscreen;
235
Scott Moreauff1db4a2012-04-17 19:06:18 -0600236 struct ping_timer *ping_timer;
237
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200238 struct weston_transform workspace_transform;
239
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500240 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100242 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400243
244 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200245};
246
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300247struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400248 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300249 struct shell_surface *shsurf;
250 struct wl_listener shsurf_destroy_listener;
251};
252
Rusty Lynch1084da52013-08-15 09:10:08 -0700253struct shell_touch_grab {
254 struct weston_touch_grab grab;
255 struct shell_surface *shsurf;
256 struct wl_listener shsurf_destroy_listener;
257 struct weston_touch *touch;
258};
259
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300260struct weston_move_grab {
261 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100262 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263};
264
Rusty Lynch1084da52013-08-15 09:10:08 -0700265struct weston_touch_move_grab {
266 struct shell_touch_grab base;
267 wl_fixed_t dx, dy;
268};
269
Pekka Paalanen460099f2012-01-20 16:48:25 +0200270struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500272 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200273 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200274 float x;
275 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200276 } center;
277};
278
Giulio Camuffo5085a752013-03-25 21:42:45 +0100279struct shell_seat {
280 struct weston_seat *seat;
281 struct wl_listener seat_destroy_listener;
282
283 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400284 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100285 struct wl_list surfaces_list;
286 struct wl_client *client;
287 int32_t initial_up;
288 } popup_grab;
289};
290
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400291static void
292activate(struct desktop_shell *shell, struct weston_surface *es,
293 struct weston_seat *seat);
294
295static struct workspace *
296get_current_workspace(struct desktop_shell *shell);
297
Alex Wubd3354b2012-04-17 17:20:49 +0800298static struct shell_surface *
299get_shell_surface(struct weston_surface *surface);
300
301static struct desktop_shell *
302shell_surface_get_shell(struct shell_surface *shsurf);
303
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500304static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400305surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500306
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300307static void
308shell_fade_startup(struct desktop_shell *shell);
309
Alex Wubd3354b2012-04-17 17:20:49 +0800310static bool
311shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
312{
313 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800315
316 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800319 return false;
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
322 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800323 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800325}
326
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500327static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400328destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329{
330 struct shell_grab *grab;
331
332 grab = container_of(listener, struct shell_grab,
333 shsurf_destroy_listener);
334
335 grab->shsurf = NULL;
336}
337
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338static struct weston_view *
339get_default_view(struct weston_surface *surface)
340{
341 struct shell_surface *shsurf;
342 struct weston_view *view;
343
344 if (!surface || wl_list_empty(&surface->views))
345 return NULL;
346
347 shsurf = get_shell_surface(surface);
348 if (shsurf)
349 return shsurf->view;
350
351 wl_list_for_each(view, &surface->views, surface_link)
352 if (weston_view_is_mapped(view))
353 return view;
354
355 return container_of(surface->views.next, struct weston_view, surface_link);
356}
357
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300358static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400359popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400360
361static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400363 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400365 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300368 struct desktop_shell *shell = shsurf->shell;
369
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400370 popup_grab_end(pointer);
371
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300372 grab->grab.interface = interface;
373 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400374 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500375 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400376 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300377
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400379 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400380 if (shell->child.desktop_shell) {
381 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
382 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500383 weston_pointer_set_focus(pointer,
384 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400385 wl_fixed_from_int(0),
386 wl_fixed_from_int(0));
387 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300388}
389
390static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300391shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300392{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400394 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 grab->shsurf->grabbed = 0;
396 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300397
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700398 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300399}
400
401static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700402shell_touch_grab_start(struct shell_touch_grab *grab,
403 const struct weston_touch_grab_interface *interface,
404 struct shell_surface *shsurf,
405 struct weston_touch *touch)
406{
407 struct desktop_shell *shell = shsurf->shell;
408
409 grab->grab.interface = interface;
410 grab->shsurf = shsurf;
411 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
412 wl_signal_add(&shsurf->destroy_signal,
413 &grab->shsurf_destroy_listener);
414
415 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700416 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700417
418 weston_touch_start_grab(touch, &grab->grab);
419 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420 weston_touch_set_focus(touch->seat,
421 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700422}
423
424static void
425shell_touch_grab_end(struct shell_touch_grab *grab)
426{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700427 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700428 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700429 grab->shsurf->grabbed = 0;
430 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700431
432 weston_touch_end_grab(grab->touch);
433}
434
435static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500436center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800437 struct weston_output *output);
438
Daniel Stone496ca172012-05-30 16:31:42 +0100439static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300440get_modifier(char *modifier)
441{
442 if (!modifier)
443 return MODIFIER_SUPER;
444
445 if (!strcmp("ctrl", modifier))
446 return MODIFIER_CTRL;
447 else if (!strcmp("alt", modifier))
448 return MODIFIER_ALT;
449 else if (!strcmp("super", modifier))
450 return MODIFIER_SUPER;
451 else
452 return MODIFIER_SUPER;
453}
454
Juan Zhaoe10d2792012-04-25 19:09:52 +0800455static enum animation_type
456get_animation_type(char *animation)
457{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800458 if (!strcmp("zoom", animation))
459 return ANIMATION_ZOOM;
460 else if (!strcmp("fade", animation))
461 return ANIMATION_FADE;
462 else
463 return ANIMATION_NONE;
464}
465
Alex Wu4539b082012-03-01 12:57:46 +0800466static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400467shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200468{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 struct weston_config_section *section;
470 int duration;
471 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200472
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 section = weston_config_get_section(shell->compositor->config,
474 "screensaver", NULL, NULL);
475 weston_config_section_get_string(section,
476 "path", &shell->screensaver.path, NULL);
477 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200478 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400479
480 section = weston_config_get_section(shell->compositor->config,
481 "shell", NULL, NULL);
482 weston_config_section_get_string(section,
483 "binding-modifier", &s, "super");
484 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200485 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400486 weston_config_section_get_string(section, "animation", &s, "none");
487 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200488 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700489 weston_config_section_get_string(section,
490 "startup-animation", &s, "fade");
491 shell->startup_animation_type = get_animation_type(s);
492 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400493 weston_config_section_get_uint(section, "num-workspaces",
494 &shell->workspaces.num,
495 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200496}
497
498static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200499focus_state_destroy(struct focus_state *state)
500{
501 wl_list_remove(&state->seat_destroy_listener.link);
502 wl_list_remove(&state->surface_destroy_listener.link);
503 free(state);
504}
505
506static void
507focus_state_seat_destroy(struct wl_listener *listener, void *data)
508{
509 struct focus_state *state = container_of(listener,
510 struct focus_state,
511 seat_destroy_listener);
512
513 wl_list_remove(&state->link);
514 focus_state_destroy(state);
515}
516
517static void
518focus_state_surface_destroy(struct wl_listener *listener, void *data)
519{
520 struct focus_state *state = container_of(listener,
521 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400522 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400523 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 struct weston_surface *main_surface, *next;
525 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200526
Pekka Paalanen01388e22013-04-25 13:57:44 +0300527 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
528
Kristian Høgsberge3778222012-07-31 17:29:30 -0400529 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
531 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400532 continue;
533
Jason Ekstranda7af7042013-10-12 22:38:11 -0500534 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400535 break;
536 }
537
Pekka Paalanen01388e22013-04-25 13:57:44 +0300538 /* if the focus was a sub-surface, activate its main surface */
539 if (main_surface != state->keyboard_focus)
540 next = main_surface;
541
Kristian Høgsberge3778222012-07-31 17:29:30 -0400542 if (next) {
543 shell = state->seat->compositor->shell_interface.shell;
544 activate(shell, next, state->seat);
545 } else {
546 wl_list_remove(&state->link);
547 focus_state_destroy(state);
548 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200549}
550
551static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400552focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200553{
Jonas Ådahl04769742012-06-13 00:01:24 +0200554 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200555
556 state = malloc(sizeof *state);
557 if (state == NULL)
558 return NULL;
559
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400560 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200561 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400562 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200563
564 state->seat_destroy_listener.notify = focus_state_seat_destroy;
565 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400566 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200567 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400568 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200569
570 return state;
571}
572
Jonas Ådahl8538b222012-08-29 22:13:03 +0200573static struct focus_state *
574ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
575{
576 struct workspace *ws = get_current_workspace(shell);
577 struct focus_state *state;
578
579 wl_list_for_each(state, &ws->focus_list, link)
580 if (state->seat == seat)
581 break;
582
583 if (&state->link == &ws->focus_list)
584 state = focus_state_create(seat, ws);
585
586 return state;
587}
588
Jonas Ådahl04769742012-06-13 00:01:24 +0200589static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400590restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200591{
592 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400593 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200594
595 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400596 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200597
Kristian Høgsberge3148752013-05-06 23:19:49 -0400598 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200599 }
600}
601
602static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200603replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
604 struct weston_seat *seat)
605{
606 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400607 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200608
609 wl_list_for_each(state, &ws->focus_list, link) {
610 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400611 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500612 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200613 return;
614 }
615 }
616}
617
618static void
619drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
620 struct weston_surface *surface)
621{
622 struct focus_state *state;
623
624 wl_list_for_each(state, &ws->focus_list, link)
625 if (state->keyboard_focus == surface)
626 state->keyboard_focus = NULL;
627}
628
629static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200630workspace_destroy(struct workspace *ws)
631{
Jonas Ådahl04769742012-06-13 00:01:24 +0200632 struct focus_state *state, *next;
633
634 wl_list_for_each_safe(state, next, &ws->focus_list, link)
635 focus_state_destroy(state);
636
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200637 free(ws);
638}
639
Jonas Ådahl04769742012-06-13 00:01:24 +0200640static void
641seat_destroyed(struct wl_listener *listener, void *data)
642{
643 struct weston_seat *seat = data;
644 struct focus_state *state, *next;
645 struct workspace *ws = container_of(listener,
646 struct workspace,
647 seat_destroyed_listener);
648
649 wl_list_for_each_safe(state, next, &ws->focus_list, link)
650 if (state->seat == seat)
651 wl_list_remove(&state->link);
652}
653
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200654static struct workspace *
655workspace_create(void)
656{
657 struct workspace *ws = malloc(sizeof *ws);
658 if (ws == NULL)
659 return NULL;
660
661 weston_layer_init(&ws->layer, NULL);
662
Jonas Ådahl04769742012-06-13 00:01:24 +0200663 wl_list_init(&ws->focus_list);
664 wl_list_init(&ws->seat_destroyed_listener.link);
665 ws->seat_destroyed_listener.notify = seat_destroyed;
666
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200667 return ws;
668}
669
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200670static int
671workspace_is_empty(struct workspace *ws)
672{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500673 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200674}
675
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200676static struct workspace *
677get_workspace(struct desktop_shell *shell, unsigned int index)
678{
679 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200680 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200681 pws += index;
682 return *pws;
683}
684
685static struct workspace *
686get_current_workspace(struct desktop_shell *shell)
687{
688 return get_workspace(shell, shell->workspaces.current);
689}
690
691static void
692activate_workspace(struct desktop_shell *shell, unsigned int index)
693{
694 struct workspace *ws;
695
696 ws = get_workspace(shell, index);
697 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
698
699 shell->workspaces.current = index;
700}
701
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200702static unsigned int
703get_output_height(struct weston_output *output)
704{
705 return abs(output->region.extents.y1 - output->region.extents.y2);
706}
707
708static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500709view_translate(struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200710{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 struct shell_surface *shsurf = get_shell_surface(view->surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200712 struct weston_transform *transform;
713
714 transform = &shsurf->workspace_transform;
715 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500716 wl_list_insert(view->geometry.transformation_list.prev,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200717 &shsurf->workspace_transform.link);
718
719 weston_matrix_init(&shsurf->workspace_transform.matrix);
720 weston_matrix_translate(&shsurf->workspace_transform.matrix,
721 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500722 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200723}
724
725static void
726workspace_translate_out(struct workspace *ws, double fraction)
727{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500728 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200729 unsigned int height;
730 double d;
731
Jason Ekstranda7af7042013-10-12 22:38:11 -0500732 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
733 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200734 d = height * fraction;
735
Jason Ekstranda7af7042013-10-12 22:38:11 -0500736 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200737 }
738}
739
740static void
741workspace_translate_in(struct workspace *ws, double fraction)
742{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500743 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200744 unsigned int height;
745 double d;
746
Jason Ekstranda7af7042013-10-12 22:38:11 -0500747 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
748 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749
750 if (fraction > 0)
751 d = -(height - height * fraction);
752 else
753 d = height + height * fraction;
754
Jason Ekstranda7af7042013-10-12 22:38:11 -0500755 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200756 }
757}
758
759static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200760broadcast_current_workspace_state(struct desktop_shell *shell)
761{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700762 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200763
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700764 wl_resource_for_each(resource, &shell->workspaces.client_list)
765 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200766 shell->workspaces.current,
767 shell->workspaces.num);
768}
769
770static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200771reverse_workspace_change_animation(struct desktop_shell *shell,
772 unsigned int index,
773 struct workspace *from,
774 struct workspace *to)
775{
776 shell->workspaces.current = index;
777
778 shell->workspaces.anim_to = to;
779 shell->workspaces.anim_from = from;
780 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
781 shell->workspaces.anim_timestamp = 0;
782
Scott Moreau4272e632012-08-13 09:58:41 -0600783 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200784}
785
786static void
787workspace_deactivate_transforms(struct workspace *ws)
788{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200790 struct shell_surface *shsurf;
791
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
793 shsurf = get_shell_surface(view->surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
795 wl_list_remove(&shsurf->workspace_transform.link);
796 wl_list_init(&shsurf->workspace_transform.link);
797 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500798 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200799 }
800}
801
802static void
803finish_workspace_change_animation(struct desktop_shell *shell,
804 struct workspace *from,
805 struct workspace *to)
806{
Scott Moreau4272e632012-08-13 09:58:41 -0600807 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200808
809 wl_list_remove(&shell->workspaces.animation.link);
810 workspace_deactivate_transforms(from);
811 workspace_deactivate_transforms(to);
812 shell->workspaces.anim_to = NULL;
813
814 wl_list_remove(&shell->workspaces.anim_from->layer.link);
815}
816
817static void
818animate_workspace_change_frame(struct weston_animation *animation,
819 struct weston_output *output, uint32_t msecs)
820{
821 struct desktop_shell *shell =
822 container_of(animation, struct desktop_shell,
823 workspaces.animation);
824 struct workspace *from = shell->workspaces.anim_from;
825 struct workspace *to = shell->workspaces.anim_to;
826 uint32_t t;
827 double x, y;
828
829 if (workspace_is_empty(from) && workspace_is_empty(to)) {
830 finish_workspace_change_animation(shell, from, to);
831 return;
832 }
833
834 if (shell->workspaces.anim_timestamp == 0) {
835 if (shell->workspaces.anim_current == 0.0)
836 shell->workspaces.anim_timestamp = msecs;
837 else
838 shell->workspaces.anim_timestamp =
839 msecs -
840 /* Invers of movement function 'y' below. */
841 (asin(1.0 - shell->workspaces.anim_current) *
842 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
843 M_2_PI);
844 }
845
846 t = msecs - shell->workspaces.anim_timestamp;
847
848 /*
849 * x = [0, π/2]
850 * y(x) = sin(x)
851 */
852 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
853 y = sin(x);
854
855 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600856 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857
858 workspace_translate_out(from, shell->workspaces.anim_dir * y);
859 workspace_translate_in(to, shell->workspaces.anim_dir * y);
860 shell->workspaces.anim_current = y;
861
Scott Moreau4272e632012-08-13 09:58:41 -0600862 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200864 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200865 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866}
867
868static void
869animate_workspace_change(struct desktop_shell *shell,
870 unsigned int index,
871 struct workspace *from,
872 struct workspace *to)
873{
874 struct weston_output *output;
875
876 int dir;
877
878 if (index > shell->workspaces.current)
879 dir = -1;
880 else
881 dir = 1;
882
883 shell->workspaces.current = index;
884
885 shell->workspaces.anim_dir = dir;
886 shell->workspaces.anim_from = from;
887 shell->workspaces.anim_to = to;
888 shell->workspaces.anim_current = 0.0;
889 shell->workspaces.anim_timestamp = 0;
890
891 output = container_of(shell->compositor->output_list.next,
892 struct weston_output, link);
893 wl_list_insert(&output->animation_list,
894 &shell->workspaces.animation.link);
895
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200896 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200897
898 workspace_translate_in(to, 0);
899
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400900 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200901
Scott Moreau4272e632012-08-13 09:58:41 -0600902 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200906update_workspace(struct desktop_shell *shell, unsigned int index,
907 struct workspace *from, struct workspace *to)
908{
909 shell->workspaces.current = index;
910 wl_list_insert(&from->layer.link, &to->layer.link);
911 wl_list_remove(&from->layer.link);
912}
913
914static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200915change_workspace(struct desktop_shell *shell, unsigned int index)
916{
917 struct workspace *from;
918 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919
920 if (index == shell->workspaces.current)
921 return;
922
923 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200925 return;
926
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200927 from = get_current_workspace(shell);
928 to = get_workspace(shell, index);
929
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930 if (shell->workspaces.anim_from == to &&
931 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200933 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200934 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 return;
936 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200938 if (shell->workspaces.anim_to != NULL)
939 finish_workspace_change_animation(shell,
940 shell->workspaces.anim_from,
941 shell->workspaces.anim_to);
942
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200943 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200944
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200945 if (workspace_is_empty(to) && workspace_is_empty(from))
946 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947 else
948 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949
950 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200951}
952
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200953static bool
954workspace_has_only(struct workspace *ws, struct weston_surface *surface)
955{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200957 struct wl_list *e;
958
959 if (wl_list_empty(list))
960 return false;
961
962 e = list->next;
963
964 if (e->next != list)
965 return false;
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968}
969
970static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971move_view_to_workspace(struct desktop_shell *shell,
972 struct weston_view *view,
973 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +0200974{
975 struct workspace *from;
976 struct workspace *to;
977 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300978 struct weston_surface *focus;
979
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200981
982 if (workspace == shell->workspaces.current)
983 return;
984
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200985 if (workspace >= shell->workspaces.num)
986 workspace = shell->workspaces.num - 1;
987
Jonas Ådahle9d22502012-08-29 22:13:01 +0200988 from = get_current_workspace(shell);
989 to = get_workspace(shell, workspace);
990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_remove(&view->layer_link);
992 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300995 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
996 if (!seat->keyboard)
997 continue;
998
999 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001001 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001002 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001003
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001005}
1006
1007static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001008take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001009 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001010 unsigned int index)
1011{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001012 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001014 struct shell_surface *shsurf;
1015 struct workspace *from;
1016 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001017 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001018
Pekka Paalanen01388e22013-04-25 13:57:44 +03001019 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 view = get_default_view(surface);
1021 if (view == NULL ||
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001022 index == shell->workspaces.current)
1023 return;
1024
1025 from = get_current_workspace(shell);
1026 to = get_workspace(shell, index);
1027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 wl_list_remove(&view->layer_link);
1029 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001030
Jonas Ådahle9d22502012-08-29 22:13:01 +02001031 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001032 drop_focus_state(shell, from, surface);
1033
1034 if (shell->workspaces.anim_from == to &&
1035 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001036 wl_list_remove(&to->layer.link);
1037 wl_list_insert(from->layer.link.prev, &to->layer.link);
1038
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001039 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001040 broadcast_current_workspace_state(shell);
1041
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001042 return;
1043 }
1044
1045 if (shell->workspaces.anim_to != NULL)
1046 finish_workspace_change_animation(shell,
1047 shell->workspaces.anim_from,
1048 shell->workspaces.anim_to);
1049
1050 if (workspace_is_empty(from) &&
1051 workspace_has_only(to, surface))
1052 update_workspace(shell, index, from, to);
1053 else {
1054 shsurf = get_shell_surface(surface);
1055 if (wl_list_empty(&shsurf->workspace_transform.link))
1056 wl_list_insert(&shell->workspaces.anim_sticky_list,
1057 &shsurf->workspace_transform.link);
1058
1059 animate_workspace_change(shell, index, from, to);
1060 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001061
1062 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001063
1064 state = ensure_focus_state(shell, seat);
1065 if (state != NULL)
1066 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001067}
1068
1069static void
1070workspace_manager_move_surface(struct wl_client *client,
1071 struct wl_resource *resource,
1072 struct wl_resource *surface_resource,
1073 uint32_t workspace)
1074{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001075 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001076 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001077 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001078 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001080
Pekka Paalanen01388e22013-04-25 13:57:44 +03001081 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 view = get_default_view(main_surface);
1083 if (!view)
1084 return;
1085 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001086}
1087
1088static const struct workspace_manager_interface workspace_manager_implementation = {
1089 workspace_manager_move_surface,
1090};
1091
1092static void
1093unbind_resource(struct wl_resource *resource)
1094{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001095 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001096}
1097
1098static void
1099bind_workspace_manager(struct wl_client *client,
1100 void *data, uint32_t version, uint32_t id)
1101{
1102 struct desktop_shell *shell = data;
1103 struct wl_resource *resource;
1104
Jason Ekstranda85118c2013-06-27 20:17:02 -05001105 resource = wl_resource_create(client,
1106 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001107
1108 if (resource == NULL) {
1109 weston_log("couldn't add workspace manager object");
1110 return;
1111 }
1112
Jason Ekstranda85118c2013-06-27 20:17:02 -05001113 wl_resource_set_implementation(resource,
1114 &workspace_manager_implementation,
1115 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001116 wl_list_insert(&shell->workspaces.client_list,
1117 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001118
1119 workspace_manager_send_state(resource,
1120 shell->workspaces.current,
1121 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001122}
1123
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001124static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001125touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1126 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1127{
1128}
1129
1130static void
1131touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1132{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001133 struct weston_touch_move_grab *move =
1134 (struct weston_touch_move_grab *) container_of(
1135 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001136
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001137 if (grab->touch->seat->num_tp == 0) {
1138 shell_touch_grab_end(&move->base);
1139 free(move);
1140 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001141}
1142
1143static void
1144touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1145 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1146{
1147 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1148 struct shell_surface *shsurf = move->base.shsurf;
1149 struct weston_surface *es;
1150 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1151 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1152
1153 if (!shsurf)
1154 return;
1155
1156 es = shsurf->surface;
1157
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 weston_view_configure(shsurf->view, dx, dy,
1159 shsurf->view->geometry.width,
1160 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001161
1162 weston_compositor_schedule_repaint(es->compositor);
1163}
1164
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001165static void
1166touch_move_grab_cancel(struct weston_touch_grab *grab)
1167{
1168 struct weston_touch_move_grab *move =
1169 (struct weston_touch_move_grab *) container_of(
1170 grab, struct shell_touch_grab, grab);
1171
1172 shell_touch_grab_end(&move->base);
1173 free(move);
1174}
1175
Rusty Lynch1084da52013-08-15 09:10:08 -07001176static const struct weston_touch_grab_interface touch_move_grab_interface = {
1177 touch_move_grab_down,
1178 touch_move_grab_up,
1179 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001180 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001181};
1182
1183static int
1184surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1185{
1186 struct weston_touch_move_grab *move;
1187
1188 if (!shsurf)
1189 return -1;
1190
1191 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1192 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001193 if (shsurf->grabbed)
1194 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001195
1196 move = malloc(sizeof *move);
1197 if (!move)
1198 return -1;
1199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001201 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001203 seat->touch->grab_y;
1204
1205 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1206 seat->touch);
1207
1208 return 0;
1209}
1210
1211static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001212noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001213{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001214}
1215
1216static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001217move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001218{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001219 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001220 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001221 struct shell_surface *shsurf = move->base.shsurf;
Daniel Stone37816df2012-05-16 18:45:18 +01001222 int dx = wl_fixed_to_int(pointer->x + move->dx);
1223 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001224
1225 if (!shsurf)
1226 return;
1227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_configure(shsurf->view, dx, dy,
1229 shsurf->view->geometry.width,
1230 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001233}
1234
1235static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001236move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001237 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001238{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001239 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1240 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001241 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001242 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001243
Daniel Stone4dbadb12012-05-30 16:31:51 +01001244 if (pointer->button_count == 0 &&
1245 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001246 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001247 free(grab);
1248 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001249}
1250
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001251static void
1252move_grab_cancel(struct weston_pointer_grab *grab)
1253{
1254 struct shell_grab *shell_grab =
1255 container_of(grab, struct shell_grab, grab);
1256
1257 shell_grab_end(shell_grab);
1258 free(grab);
1259}
1260
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001261static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001262 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001263 move_grab_motion,
1264 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001265 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001266};
1267
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001268static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001269surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001270{
1271 struct weston_move_grab *move;
1272
1273 if (!shsurf)
1274 return -1;
1275
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001276 if (shsurf->grabbed)
1277 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001278 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1279 return 0;
1280
1281 move = malloc(sizeof *move);
1282 if (!move)
1283 return -1;
1284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001286 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001288 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001289
1290 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001291 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001292
1293 return 0;
1294}
1295
1296static void
1297shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1298 struct wl_resource *seat_resource, uint32_t serial)
1299{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001300 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001301 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001302 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001303
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001304 if (seat->pointer &&
1305 seat->pointer->button_count > 0 &&
1306 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001308 if ((surface == shsurf->surface) &&
1309 (surface_move(shsurf, seat) < 0))
1310 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001311 } else if (seat->touch &&
1312 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001314 if ((surface == shsurf->surface) &&
1315 (surface_touch_move(shsurf, seat) < 0))
1316 wl_resource_post_no_memory(resource);
1317 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001318}
1319
1320struct weston_resize_grab {
1321 struct shell_grab base;
1322 uint32_t edges;
1323 int32_t width, height;
1324};
1325
1326static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001327resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001328{
1329 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001330 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001331 struct shell_surface *shsurf = resize->base.shsurf;
1332 int32_t width, height;
1333 wl_fixed_t from_x, from_y;
1334 wl_fixed_t to_x, to_y;
1335
1336 if (!shsurf)
1337 return;
1338
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 weston_view_from_global_fixed(shsurf->view,
1340 pointer->grab_x, pointer->grab_y,
1341 &from_x, &from_y);
1342 weston_view_from_global_fixed(shsurf->view,
1343 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001344
1345 width = resize->width;
1346 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1347 width += wl_fixed_to_int(from_x - to_x);
1348 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1349 width += wl_fixed_to_int(to_x - from_x);
1350 }
1351
1352 height = resize->height;
1353 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1354 height += wl_fixed_to_int(from_y - to_y);
1355 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1356 height += wl_fixed_to_int(to_y - from_y);
1357 }
1358
1359 shsurf->client->send_configure(shsurf->surface,
1360 resize->edges, width, height);
1361}
1362
1363static void
1364send_configure(struct weston_surface *surface,
1365 uint32_t edges, int32_t width, int32_t height)
1366{
1367 struct shell_surface *shsurf = get_shell_surface(surface);
1368
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001369 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001370 edges, width, height);
1371}
1372
1373static const struct weston_shell_client shell_client = {
1374 send_configure
1375};
1376
1377static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001378resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001379 uint32_t time, uint32_t button, uint32_t state_w)
1380{
1381 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001382 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001383 enum wl_pointer_button_state state = state_w;
1384
1385 if (pointer->button_count == 0 &&
1386 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1387 shell_grab_end(&resize->base);
1388 free(grab);
1389 }
1390}
1391
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001392static void
1393resize_grab_cancel(struct weston_pointer_grab *grab)
1394{
1395 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1396
1397 shell_grab_end(&resize->base);
1398 free(grab);
1399}
1400
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001401static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001402 noop_grab_focus,
1403 resize_grab_motion,
1404 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001405 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001406};
1407
Giulio Camuffob8366642013-04-25 13:57:46 +03001408/*
1409 * Returns the bounding box of a surface and all its sub-surfaces,
1410 * in the surface coordinates system. */
1411static void
1412surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1413 int32_t *y, int32_t *w, int32_t *h) {
1414 pixman_region32_t region;
1415 pixman_box32_t *box;
1416 struct weston_subsurface *subsurface;
1417
1418 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 surface->width,
1420 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001421
1422 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1423 pixman_region32_union_rect(&region, &region,
1424 subsurface->position.x,
1425 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 subsurface->surface->width,
1427 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001428 }
1429
1430 box = pixman_region32_extents(&region);
1431 if (x)
1432 *x = box->x1;
1433 if (y)
1434 *y = box->y1;
1435 if (w)
1436 *w = box->x2 - box->x1;
1437 if (h)
1438 *h = box->y2 - box->y1;
1439
1440 pixman_region32_fini(&region);
1441}
1442
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001443static int
1444surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001445 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001446{
1447 struct weston_resize_grab *resize;
1448
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001449 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1450 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001451 return 0;
1452
1453 if (edges == 0 || edges > 15 ||
1454 (edges & 3) == 3 || (edges & 12) == 12)
1455 return 0;
1456
1457 resize = malloc(sizeof *resize);
1458 if (!resize)
1459 return -1;
1460
1461 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001462 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1463 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001464
1465 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001466 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001467
1468 return 0;
1469}
1470
1471static void
1472shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1473 struct wl_resource *seat_resource, uint32_t serial,
1474 uint32_t edges)
1475{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001476 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001477 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001478 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001479
1480 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1481 return;
1482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001484 if (seat->pointer->button_count == 0 ||
1485 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001486 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001487 return;
1488
Kristian Høgsberge3148752013-05-06 23:19:49 -04001489 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001490 wl_resource_post_no_memory(resource);
1491}
1492
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001493static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001494end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1495
1496static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001497busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001498{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001499 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001500 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001502 wl_fixed_t sx, sy;
1503
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 view = weston_compositor_pick_view(pointer->seat->compositor,
1505 pointer->x, pointer->y,
1506 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001509 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001510}
1511
1512static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001513busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001514{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001515}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001516
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001517static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001518busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001519 uint32_t time, uint32_t button, uint32_t state)
1520{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001521 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001522 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001523 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001524
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001525 if (shsurf && button == BTN_LEFT && state) {
1526 activate(shsurf->shell, shsurf->surface, seat);
1527 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001528 } else if (shsurf && button == BTN_RIGHT && state) {
1529 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001530 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001531 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001532}
1533
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001534static void
1535busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1536{
1537 struct shell_grab *grab = (struct shell_grab *) base;
1538
1539 shell_grab_end(grab);
1540 free(grab);
1541}
1542
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001543static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001544 busy_cursor_grab_focus,
1545 busy_cursor_grab_motion,
1546 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001547 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001548};
1549
1550static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001551set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001552{
1553 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001554
1555 grab = malloc(sizeof *grab);
1556 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001557 return;
1558
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001559 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1560 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001561}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001562
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001563static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001564end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001565{
1566 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1567
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001568 if (grab->grab.interface == &busy_cursor_grab_interface &&
1569 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001570 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001571 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001572 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001573}
1574
Scott Moreau9521d5e2012-04-19 13:06:17 -06001575static void
1576ping_timer_destroy(struct shell_surface *shsurf)
1577{
1578 if (!shsurf || !shsurf->ping_timer)
1579 return;
1580
1581 if (shsurf->ping_timer->source)
1582 wl_event_source_remove(shsurf->ping_timer->source);
1583
1584 free(shsurf->ping_timer);
1585 shsurf->ping_timer = NULL;
1586}
1587
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001588static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001589ping_timeout_handler(void *data)
1590{
1591 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001592 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001593
Scott Moreau9521d5e2012-04-19 13:06:17 -06001594 /* Client is not responding */
1595 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001596
1597 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001599 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001600
1601 return 1;
1602}
1603
1604static void
1605ping_handler(struct weston_surface *surface, uint32_t serial)
1606{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001607 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001608 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001609 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001610
1611 if (!shsurf)
1612 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001613 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001614 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001615
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001616 if (shsurf->surface == shsurf->shell->grab_surface)
1617 return;
1618
Scott Moreauff1db4a2012-04-17 19:06:18 -06001619 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001620 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001621 if (!shsurf->ping_timer)
1622 return;
1623
1624 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001625 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1626 shsurf->ping_timer->source =
1627 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1628 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1629
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001630 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001631 }
1632}
1633
1634static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001635handle_pointer_focus(struct wl_listener *listener, void *data)
1636{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001637 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001639 struct weston_compositor *compositor;
1640 struct shell_surface *shsurf;
1641 uint32_t serial;
1642
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001644 return;
1645
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 compositor = view->surface->compositor;
1647 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001648
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001649 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001650 set_busy_cursor(shsurf, pointer);
1651 } else {
1652 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001654 }
1655}
1656
1657static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001658create_pointer_focus_listener(struct weston_seat *seat)
1659{
1660 struct wl_listener *listener;
1661
Kristian Høgsberge3148752013-05-06 23:19:49 -04001662 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001663 return;
1664
1665 listener = malloc(sizeof *listener);
1666 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001667 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001668}
1669
1670static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001671shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1672 uint32_t serial)
1673{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001674 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001675 struct weston_seat *seat;
1676 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001677
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001678 if (shsurf->ping_timer == NULL)
1679 /* Just ignore unsolicited pong. */
1680 return;
1681
Scott Moreauff1db4a2012-04-17 19:06:18 -06001682 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001683 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001684 wl_list_for_each(seat, &ec->seat_list, link) {
1685 if(seat->pointer)
1686 end_busy_cursor(shsurf, seat->pointer);
1687 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001688 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001689 }
1690}
1691
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001692static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001693set_title(struct shell_surface *shsurf, const char *title)
1694{
1695 free(shsurf->title);
1696 shsurf->title = strdup(title);
1697}
1698
1699static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001700shell_surface_set_title(struct wl_client *client,
1701 struct wl_resource *resource, const char *title)
1702{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001703 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001704
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001705 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001706}
1707
1708static void
1709shell_surface_set_class(struct wl_client *client,
1710 struct wl_resource *resource, const char *class)
1711{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001712 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001713
1714 free(shsurf->class);
1715 shsurf->class = strdup(class);
1716}
1717
Juan Zhao96879df2012-02-07 08:45:41 +08001718static struct weston_output *
1719get_default_output(struct weston_compositor *compositor)
1720{
1721 return container_of(compositor->output_list.next,
1722 struct weston_output, link);
1723}
1724
Alex Wu4539b082012-03-01 12:57:46 +08001725static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001726restore_output_mode(struct weston_output *output)
1727{
Hardening57388e42013-09-18 23:56:36 +02001728 if (output->current_mode != output->original_mode ||
1729 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001730 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001731 output->original_mode,
1732 output->original_scale,
1733 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001734}
1735
1736static void
1737restore_all_output_modes(struct weston_compositor *compositor)
1738{
1739 struct weston_output *output;
1740
1741 wl_list_for_each(output, &compositor->output_list, link)
1742 restore_output_mode(output);
1743}
1744
1745static void
Alex Wu4539b082012-03-01 12:57:46 +08001746shell_unset_fullscreen(struct shell_surface *shsurf)
1747{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001748 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001749 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001750 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1751 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001752 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001753 }
Alex Wu4539b082012-03-01 12:57:46 +08001754 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1755 shsurf->fullscreen.framerate = 0;
1756 wl_list_remove(&shsurf->fullscreen.transform.link);
1757 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 if (shsurf->fullscreen.black_view)
1759 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1760 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001761 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 weston_view_set_position(shsurf->view,
1763 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001764 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001766 &shsurf->rotation.transform.link);
1767 shsurf->saved_rotation_valid = false;
1768 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001769
1770 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 wl_list_remove(&shsurf->view->layer_link);
1772 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001773}
1774
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001775static void
1776shell_unset_maximized(struct shell_surface *shsurf)
1777{
1778 struct workspace *ws;
1779 /* undo all maximized things here */
1780 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 weston_view_set_position(shsurf->view,
1782 shsurf->saved_x,
1783 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001784
1785 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 wl_list_insert(&shsurf->view->geometry.transformation_list,
1787 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001788 shsurf->saved_rotation_valid = false;
1789 }
1790
1791 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 wl_list_remove(&shsurf->view->layer_link);
1793 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001794}
1795
Pekka Paalanen98262232011-12-01 10:42:22 +02001796static int
1797reset_shell_surface_type(struct shell_surface *surface)
1798{
1799 switch (surface->type) {
1800 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001801 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001802 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001803 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001804 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001805 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001806 case SHELL_SURFACE_NONE:
1807 case SHELL_SURFACE_TOPLEVEL:
1808 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001809 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001810 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001811 break;
1812 }
1813
1814 surface->type = SHELL_SURFACE_NONE;
1815 return 0;
1816}
1817
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001818static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001819set_surface_type(struct shell_surface *shsurf)
1820{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001821 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001823
1824 reset_shell_surface_type(shsurf);
1825
1826 shsurf->type = shsurf->next_type;
1827 shsurf->next_type = SHELL_SURFACE_NONE;
1828
1829 switch (shsurf->type) {
1830 case SHELL_SURFACE_TOPLEVEL:
1831 break;
1832 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 if (pev)
1834 weston_view_set_position(shsurf->view,
1835 pev->geometry.x + shsurf->transient.x,
1836 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001837 break;
1838
1839 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001840 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841 shsurf->saved_x = shsurf->view->geometry.x;
1842 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001843 shsurf->saved_position_valid = true;
1844
1845 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1846 wl_list_remove(&shsurf->rotation.transform.link);
1847 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001848 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001849 shsurf->saved_rotation_valid = true;
1850 }
1851 break;
1852
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001853 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001854 weston_view_set_position(shsurf->view, shsurf->transient.x,
1855 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001856 break;
1857
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001858 default:
1859 break;
1860 }
1861}
1862
1863static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001864set_toplevel(struct shell_surface *shsurf)
1865{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001866 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001867}
1868
1869static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001870shell_surface_set_toplevel(struct wl_client *client,
1871 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001872{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001873 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001874
Tiago Vignattibc052c92012-04-19 16:18:18 +03001875 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001876}
1877
1878static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001879set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001880 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001881{
1882 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001883 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001884 shsurf->transient.x = x;
1885 shsurf->transient.y = y;
1886 shsurf->transient.flags = flags;
1887 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1888}
1889
1890static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001891shell_surface_set_transient(struct wl_client *client,
1892 struct wl_resource *resource,
1893 struct wl_resource *parent_resource,
1894 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001895{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001896 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001897 struct weston_surface *parent =
1898 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001899
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001900 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001901}
1902
Tiago Vignattibe143262012-04-16 17:31:41 +03001903static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001904shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001905{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001906 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001907}
1908
1909static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001910get_output_panel_height(struct desktop_shell *shell,
1911 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001912{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08001914 int panel_height = 0;
1915
1916 if (!output)
1917 return 0;
1918
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1920 if (view->surface->output == output) {
1921 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001922 break;
1923 }
1924 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001925
Juan Zhao96879df2012-02-07 08:45:41 +08001926 return panel_height;
1927}
1928
1929static void
1930shell_surface_set_maximized(struct wl_client *client,
1931 struct wl_resource *resource,
1932 struct wl_resource *output_resource )
1933{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001934 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001935 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001936 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001937 uint32_t edges = 0, panel_height = 0;
1938
1939 /* get the default output, if the client set it as NULL
1940 check whether the ouput is available */
1941 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001942 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001943 else if (es->output)
1944 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001945 else
1946 shsurf->output = get_default_output(es->compositor);
1947
Tiago Vignattibe143262012-04-16 17:31:41 +03001948 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001949 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001950 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001951
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001952 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001953 shsurf->output->width,
1954 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001955
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001956 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001957}
1958
Alex Wu21858432012-04-01 20:13:08 +08001959static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001960black_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 +08001961
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08001963create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001964 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001965 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001966{
1967 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001968 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08001969
1970 surface = weston_surface_create(ec);
1971 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001972 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001973 return NULL;
1974 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001975 view = weston_view_create(surface);
1976 if (surface == NULL) {
1977 weston_log("no memory\n");
1978 weston_surface_destroy(surface);
1979 return NULL;
1980 }
Alex Wu4539b082012-03-01 12:57:46 +08001981
Alex Wu21858432012-04-01 20:13:08 +08001982 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001983 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001984 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001985 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001986 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001987 pixman_region32_fini(&surface->input);
1988 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001989
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 weston_view_configure(view, x, y, w, h);
1991
1992 return view;
Alex Wu4539b082012-03-01 12:57:46 +08001993}
1994
1995/* Create black surface and append it to the associated fullscreen surface.
1996 * Handle size dismatch and positioning according to the method. */
1997static void
1998shell_configure_fullscreen(struct shell_surface *shsurf)
1999{
2000 struct weston_output *output = shsurf->fullscreen_output;
2001 struct weston_surface *surface = shsurf->surface;
2002 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002003 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002004 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002005
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002006 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2007 restore_output_mode(output);
2008
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 if (!shsurf->fullscreen.black_view)
2010 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002011 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002012 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002013 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002014 output->width,
2015 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002016
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2018 wl_list_insert(&shsurf->view->layer_link,
2019 &shsurf->fullscreen.black_view->layer_link);
2020 shsurf->fullscreen.black_view->surface->output = output;
2021 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002022
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002024 &surf_width, &surf_height);
2025
Alex Wu4539b082012-03-01 12:57:46 +08002026 switch (shsurf->fullscreen.type) {
2027 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002028 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002030 break;
2031 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002032 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002033 if (output->width == surf_width &&
2034 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 weston_view_set_position(shsurf->view,
2036 output->x - surf_x,
2037 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002038 break;
2039 }
2040
Alex Wu4539b082012-03-01 12:57:46 +08002041 matrix = &shsurf->fullscreen.transform.matrix;
2042 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002043
Scott Moreau1bad5db2012-08-18 01:04:05 -06002044 output_aspect = (float) output->width /
2045 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 /* XXX: Use surf_width and surf_height here? */
2047 surface_aspect = (float) surface->width /
2048 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002049 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002050 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002051 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002052 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002053 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002054 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002055
Alex Wu4539b082012-03-01 12:57:46 +08002056 weston_matrix_scale(matrix, scale, scale, 1);
2057 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002059 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002060 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2061 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002063
Alex Wu4539b082012-03-01 12:57:46 +08002064 break;
2065 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002066 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002067 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002068 surf_width * surface->buffer_scale,
2069 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002070 shsurf->fullscreen.framerate};
2071
Hardening57388e42013-09-18 23:56:36 +02002072 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2073 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074 weston_view_set_position(shsurf->view,
2075 output->x - surf_x,
2076 output->y - surf_y);
2077 weston_view_configure(shsurf->fullscreen.black_view,
2078 output->x - surf_x,
2079 output->y - surf_y,
2080 output->width,
2081 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002082 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002083 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002084 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002086 }
Alex Wubd3354b2012-04-17 17:20:49 +08002087 }
Alex Wu4539b082012-03-01 12:57:46 +08002088 break;
2089 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002091 break;
2092 default:
2093 break;
2094 }
2095}
2096
2097/* make the fullscreen and black surface at the top */
2098static void
2099shell_stack_fullscreen(struct shell_surface *shsurf)
2100{
Alex Wubd3354b2012-04-17 17:20:49 +08002101 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002102 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002103
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104 wl_list_remove(&shsurf->view->layer_link);
2105 wl_list_insert(&shell->fullscreen_layer.view_list,
2106 &shsurf->view->layer_link);
2107 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002108
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 if (!shsurf->fullscreen.black_view)
2110 shsurf->fullscreen.black_view =
2111 create_black_surface(shsurf->surface->compositor,
2112 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002113 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002114 output->width,
2115 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002116
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2118 wl_list_insert(&shsurf->view->layer_link,
2119 &shsurf->fullscreen.black_view->layer_link);
2120 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002121}
2122
2123static void
2124shell_map_fullscreen(struct shell_surface *shsurf)
2125{
Alex Wu4539b082012-03-01 12:57:46 +08002126 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002127 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002128}
2129
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002130static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002131set_fullscreen(struct shell_surface *shsurf,
2132 uint32_t method,
2133 uint32_t framerate,
2134 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002135{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002136 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002137
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002138 if (output)
2139 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002140 else if (es->output)
2141 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002142 else
2143 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002144
Alex Wu4539b082012-03-01 12:57:46 +08002145 shsurf->fullscreen_output = shsurf->output;
2146 shsurf->fullscreen.type = method;
2147 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002148 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002149
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002150 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002151 shsurf->output->width,
2152 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002153}
2154
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002155static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002156shell_surface_set_fullscreen(struct wl_client *client,
2157 struct wl_resource *resource,
2158 uint32_t method,
2159 uint32_t framerate,
2160 struct wl_resource *output_resource)
2161{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002162 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002163 struct weston_output *output;
2164
2165 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002166 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002167 else
2168 output = NULL;
2169
2170 set_fullscreen(shsurf, method, framerate, output);
2171}
2172
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002173static void
2174set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2175{
2176 /* XXX: using the same fields for transient type */
2177 shsurf->transient.x = x;
2178 shsurf->transient.y = y;
2179 shsurf->transient.flags = flags;
2180 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2181}
2182
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002183static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002184
2185static void
2186destroy_shell_seat(struct wl_listener *listener, void *data)
2187{
2188 struct shell_seat *shseat =
2189 container_of(listener,
2190 struct shell_seat, seat_destroy_listener);
2191 struct shell_surface *shsurf, *prev = NULL;
2192
2193 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002194 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002195 shseat->popup_grab.client = NULL;
2196
2197 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2198 shsurf->popup.shseat = NULL;
2199 if (prev) {
2200 wl_list_init(&prev->popup.grab_link);
2201 }
2202 prev = shsurf;
2203 }
2204 wl_list_init(&prev->popup.grab_link);
2205 }
2206
2207 wl_list_remove(&shseat->seat_destroy_listener.link);
2208 free(shseat);
2209}
2210
2211static struct shell_seat *
2212create_shell_seat(struct weston_seat *seat)
2213{
2214 struct shell_seat *shseat;
2215
2216 shseat = calloc(1, sizeof *shseat);
2217 if (!shseat) {
2218 weston_log("no memory to allocate shell seat\n");
2219 return NULL;
2220 }
2221
2222 shseat->seat = seat;
2223 wl_list_init(&shseat->popup_grab.surfaces_list);
2224
2225 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2226 wl_signal_add(&seat->destroy_signal,
2227 &shseat->seat_destroy_listener);
2228
2229 return shseat;
2230}
2231
2232static struct shell_seat *
2233get_shell_seat(struct weston_seat *seat)
2234{
2235 struct wl_listener *listener;
2236
2237 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2238 if (listener == NULL)
2239 return create_shell_seat(seat);
2240
2241 return container_of(listener,
2242 struct shell_seat, seat_destroy_listener);
2243}
2244
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002245static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002246popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002247{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002248 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002250 struct shell_seat *shseat =
2251 container_of(grab, struct shell_seat, popup_grab.grab);
2252 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002253 wl_fixed_t sx, sy;
2254
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255 view = weston_compositor_pick_view(pointer->seat->compositor,
2256 pointer->x, pointer->y,
2257 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002258
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 if (view && view->surface->resource &&
2260 wl_resource_get_client(view->surface->resource) == client) {
2261 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002262 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002263 weston_pointer_set_focus(pointer, NULL,
2264 wl_fixed_from_int(0),
2265 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002266 }
2267}
2268
2269static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002270popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002271{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002272 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002273 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002274 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002275
Neil Roberts96d790e2013-09-19 17:32:00 +01002276 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 weston_view_from_global_fixed(pointer->focus,
2278 pointer->x, pointer->y,
2279 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002280 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002281 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002282}
2283
2284static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002285popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002286 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002287{
2288 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002289 struct shell_seat *shseat =
2290 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002291 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002292 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002293 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002294 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002295
Neil Roberts96d790e2013-09-19 17:32:00 +01002296 resource_list = &grab->pointer->focus_resource_list;
2297 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002298 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002299 wl_resource_for_each(resource, resource_list) {
2300 wl_pointer_send_button(resource, serial,
2301 time, button, state);
2302 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002303 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002304 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002305 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002306 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002307 }
2308
Daniel Stone4dbadb12012-05-30 16:31:51 +01002309 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002310 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002311}
2312
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002313static void
2314popup_grab_cancel(struct weston_pointer_grab *grab)
2315{
2316 popup_grab_end(grab->pointer);
2317}
2318
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002319static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002320 popup_grab_focus,
2321 popup_grab_motion,
2322 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002323 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002324};
2325
2326static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002327popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002328{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002329 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002330 struct shell_seat *shseat =
2331 container_of(grab, struct shell_seat, popup_grab.grab);
2332 struct shell_surface *shsurf;
2333 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002334
2335 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002336 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002337 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002338 shseat->popup_grab.grab.interface = NULL;
2339 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002340 /* Send the popup_done event to all the popups open */
2341 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002342 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002343 shsurf->popup.shseat = NULL;
2344 if (prev) {
2345 wl_list_init(&prev->popup.grab_link);
2346 }
2347 prev = shsurf;
2348 }
2349 wl_list_init(&prev->popup.grab_link);
2350 wl_list_init(&shseat->popup_grab.surfaces_list);
2351 }
2352}
2353
2354static void
2355add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2356{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002357 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002358
2359 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002360 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002361 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002362 /* We must make sure here that this popup was opened after
2363 * a mouse press, and not just by moving around with other
2364 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002365 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002366 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002367
Rob Bradforddfe31052013-06-26 19:49:11 +01002368 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002369 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002370 } else {
2371 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002372 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002373}
2374
2375static void
2376remove_popup_grab(struct shell_surface *shsurf)
2377{
2378 struct shell_seat *shseat = shsurf->popup.shseat;
2379
2380 wl_list_remove(&shsurf->popup.grab_link);
2381 wl_list_init(&shsurf->popup.grab_link);
2382 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002383 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002384 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002385 }
2386}
2387
2388static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002389shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002390{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002391 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002393
Jason Ekstranda7af7042013-10-12 22:38:11 -05002394 shsurf->surface->output = parent_view->output;
2395 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002396
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397 weston_view_set_transform_parent(shsurf->view, parent_view);
2398 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2399 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002400
Kristian Høgsberge3148752013-05-06 23:19:49 -04002401 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002402 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002403 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002404 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002405 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002406 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002407}
2408
2409static void
2410shell_surface_set_popup(struct wl_client *client,
2411 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002412 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002413 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002414 struct wl_resource *parent_resource,
2415 int32_t x, int32_t y, uint32_t flags)
2416{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002417 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002418
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002419 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002420 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002421 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002422 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002423 shsurf->popup.x = x;
2424 shsurf->popup.y = y;
2425}
2426
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002427static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002428 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002429 shell_surface_move,
2430 shell_surface_resize,
2431 shell_surface_set_toplevel,
2432 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002433 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002434 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002435 shell_surface_set_maximized,
2436 shell_surface_set_title,
2437 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002438};
2439
2440static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002441destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002442{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002443 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2444
Giulio Camuffo5085a752013-03-25 21:42:45 +01002445 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2446 remove_popup_grab(shsurf);
2447 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002448
Alex Wubd3354b2012-04-17 17:20:49 +08002449 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002450 shell_surface_is_top_fullscreen(shsurf))
2451 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002452
Jason Ekstranda7af7042013-10-12 22:38:11 -05002453 if (shsurf->fullscreen.black_view)
2454 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002455
Alex Wubd3354b2012-04-17 17:20:49 +08002456 /* As destroy_resource() use wl_list_for_each_safe(),
2457 * we can always remove the listener.
2458 */
2459 wl_list_remove(&shsurf->surface_destroy_listener.link);
2460 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002461 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002462 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002463
Jason Ekstranda7af7042013-10-12 22:38:11 -05002464 weston_view_destroy(shsurf->view);
2465
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002466 wl_list_remove(&shsurf->link);
2467 free(shsurf);
2468}
2469
2470static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002471shell_destroy_shell_surface(struct wl_resource *resource)
2472{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002473 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002474
2475 destroy_shell_surface(shsurf);
2476}
2477
2478static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002479shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002480{
2481 struct shell_surface *shsurf = container_of(listener,
2482 struct shell_surface,
2483 surface_destroy_listener);
2484
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002485 if (shsurf->resource)
2486 wl_resource_destroy(shsurf->resource);
2487 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002488 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002489}
2490
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002491static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002492shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002493
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002494static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002495get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002496{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002497 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002498 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002499 else
2500 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002501}
2502
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002503static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002504create_shell_surface(void *shell, struct weston_surface *surface,
2505 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002506{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002507 struct shell_surface *shsurf;
2508
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002509 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002510 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002511 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002512 }
2513
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002514 shsurf = calloc(1, sizeof *shsurf);
2515 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002516 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002517 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002518 }
2519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 shsurf->view = weston_view_create(surface);
2521 if (!shsurf->view) {
2522 weston_log("no memory to allocate shell surface\n");
2523 free(shsurf);
2524 return NULL;
2525 }
2526
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002527 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002528 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002529
Tiago Vignattibc052c92012-04-19 16:18:18 +03002530 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002531 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002532 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002533 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002534 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002535 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2536 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002537 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002538 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002539 wl_list_init(&shsurf->fullscreen.transform.link);
2540
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002541 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002542 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002543 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002544 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002545
2546 /* init link so its safe to always remove it in destroy_shell_surface */
2547 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002548 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002549
Pekka Paalanen460099f2012-01-20 16:48:25 +02002550 /* empty when not in use */
2551 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002552 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002553
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002554 wl_list_init(&shsurf->workspace_transform.link);
2555
Pekka Paalanen98262232011-12-01 10:42:22 +02002556 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002557 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002558
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002559 shsurf->client = client;
2560
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002561 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002562}
2563
Jason Ekstranda7af7042013-10-12 22:38:11 -05002564static struct weston_view *
2565get_primary_view(void *shell, struct shell_surface *shsurf)
2566{
2567 return shsurf->view;
2568}
2569
Tiago Vignattibc052c92012-04-19 16:18:18 +03002570static void
2571shell_get_shell_surface(struct wl_client *client,
2572 struct wl_resource *resource,
2573 uint32_t id,
2574 struct wl_resource *surface_resource)
2575{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002576 struct weston_surface *surface =
2577 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002578 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002579 struct shell_surface *shsurf;
2580
2581 if (get_shell_surface(surface)) {
2582 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002583 WL_DISPLAY_ERROR_INVALID_OBJECT,
2584 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002585 return;
2586 }
2587
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002588 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002589 if (!shsurf) {
2590 wl_resource_post_error(surface_resource,
2591 WL_DISPLAY_ERROR_INVALID_OBJECT,
2592 "surface->configure already set");
2593 return;
2594 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002595
Jason Ekstranda85118c2013-06-27 20:17:02 -05002596 shsurf->resource =
2597 wl_resource_create(client,
2598 &wl_shell_surface_interface, 1, id);
2599 wl_resource_set_implementation(shsurf->resource,
2600 &shell_surface_implementation,
2601 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002602}
2603
2604static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002605 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002606};
2607
Kristian Høgsberg07937562011-04-12 17:25:42 -04002608static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002609shell_fade(struct desktop_shell *shell, enum fade_type type);
2610
2611static int
2612screensaver_timeout(void *data)
2613{
2614 struct desktop_shell *shell = data;
2615
2616 shell_fade(shell, FADE_OUT);
2617
2618 return 1;
2619}
2620
2621static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002622handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002623{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002624 struct desktop_shell *shell =
2625 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002626
Pekka Paalanen18027e52011-12-02 16:31:49 +02002627 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002628
2629 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002630 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002631}
2632
2633static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002634launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002635{
2636 if (shell->screensaver.binding)
2637 return;
2638
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002639 if (!shell->screensaver.path) {
2640 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002641 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002642 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002643
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002644 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002645 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002646 return;
2647 }
2648
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002649 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002650 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002651 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002652 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002653}
2654
2655static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002656terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002657{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002658 if (shell->screensaver.process.pid == 0)
2659 return;
2660
2661 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002662}
2663
2664static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002666{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002668
Giulio Camuffo184df502013-02-21 11:29:21 +01002669 if (width == 0)
2670 return;
2671
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2673 if (v->output == ev->output && v != ev) {
2674 weston_view_unmap(v);
2675 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002676 }
2677 }
2678
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002680
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681 if (wl_list_empty(&ev->layer_link)) {
2682 wl_list_insert(&layer->view_list, &ev->layer_link);
2683 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002684 }
2685}
2686
2687static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002688background_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 -04002689{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002690 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002692
Jason Ekstranda7af7042013-10-12 22:38:11 -05002693 view = container_of(es->views.next, struct weston_view, surface_link);
2694
2695 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002696}
2697
2698static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002699desktop_shell_set_background(struct wl_client *client,
2700 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002701 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002702 struct wl_resource *surface_resource)
2703{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002704 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002705 struct weston_surface *surface =
2706 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002708
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002709 if (surface->configure) {
2710 wl_resource_post_error(surface_resource,
2711 WL_DISPLAY_ERROR_INVALID_OBJECT,
2712 "surface role already assigned");
2713 return;
2714 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002715
Jason Ekstranda7af7042013-10-12 22:38:11 -05002716 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2717 weston_view_destroy(view);
2718 view = weston_view_create(surface);
2719
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002720 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002721 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002722 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002723 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002724 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002725 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002726 surface->output->width,
2727 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002728}
2729
2730static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002731panel_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 -04002732{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002733 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002735
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736 view = container_of(es->views.next, struct weston_view, surface_link);
2737
2738 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002739}
2740
2741static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002742desktop_shell_set_panel(struct wl_client *client,
2743 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002744 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002745 struct wl_resource *surface_resource)
2746{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002747 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002748 struct weston_surface *surface =
2749 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002751
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002752 if (surface->configure) {
2753 wl_resource_post_error(surface_resource,
2754 WL_DISPLAY_ERROR_INVALID_OBJECT,
2755 "surface role already assigned");
2756 return;
2757 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002758
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2760 weston_view_destroy(view);
2761 view = weston_view_create(surface);
2762
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002763 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002764 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002765 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002766 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002767 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002768 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002769 surface->output->width,
2770 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002771}
2772
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002773static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002774lock_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 -04002775{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002776 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002777 struct weston_view *view;
2778
2779 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002780
Giulio Camuffo184df502013-02-21 11:29:21 +01002781 if (width == 0)
2782 return;
2783
Jason Ekstranda7af7042013-10-12 22:38:11 -05002784 surface->width = width;
2785 surface->height = height;
2786 view->geometry.width = width;
2787 view->geometry.height = height;
2788 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002789
2790 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 wl_list_insert(&shell->lock_layer.view_list,
2792 &view->layer_link);
2793 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002794 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002795 }
2796}
2797
2798static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002799handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002800{
Tiago Vignattibe143262012-04-16 17:31:41 +03002801 struct desktop_shell *shell =
2802 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002803
Martin Minarik6d118362012-06-07 18:01:59 +02002804 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002805 shell->lock_surface = NULL;
2806}
2807
2808static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002809desktop_shell_set_lock_surface(struct wl_client *client,
2810 struct wl_resource *resource,
2811 struct wl_resource *surface_resource)
2812{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002813 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002814 struct weston_surface *surface =
2815 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002816
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002817 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002818
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002819 if (!shell->locked)
2820 return;
2821
Pekka Paalanen98262232011-12-01 10:42:22 +02002822 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002823
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002824 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002825 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002826 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002827
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002828 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002829 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002830}
2831
2832static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002833resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002834{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002835 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002836
Pekka Paalanen77346a62011-11-30 16:26:35 +02002837 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002838
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002839 wl_list_remove(&shell->lock_layer.link);
2840 wl_list_insert(&shell->compositor->cursor_layer.link,
2841 &shell->fullscreen_layer.link);
2842 wl_list_insert(&shell->fullscreen_layer.link,
2843 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002844 if (shell->showing_input_panels) {
2845 wl_list_insert(&shell->panel_layer.link,
2846 &shell->input_panel_layer.link);
2847 wl_list_insert(&shell->input_panel_layer.link,
2848 &ws->layer.link);
2849 } else {
2850 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2851 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002852
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002853 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002854
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002855 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002856 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002857 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002858}
2859
2860static void
2861desktop_shell_unlock(struct wl_client *client,
2862 struct wl_resource *resource)
2863{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002864 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002865
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002866 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002867
2868 if (shell->locked)
2869 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002870}
2871
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002872static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002873desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002874 struct wl_resource *resource,
2875 struct wl_resource *surface_resource)
2876{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002877 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002878
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002879 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07002880 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002881}
2882
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002883static void
2884desktop_shell_desktop_ready(struct wl_client *client,
2885 struct wl_resource *resource)
2886{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002887 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002888
2889 shell_fade_startup(shell);
2890}
2891
Kristian Høgsberg75840622011-09-06 13:48:16 -04002892static const struct desktop_shell_interface desktop_shell_implementation = {
2893 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002894 desktop_shell_set_panel,
2895 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002896 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002897 desktop_shell_set_grab_surface,
2898 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002899};
2900
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002901static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002902get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002903{
2904 struct shell_surface *shsurf;
2905
2906 shsurf = get_shell_surface(surface);
2907 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002908 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002909 return shsurf->type;
2910}
2911
Kristian Høgsberg75840622011-09-06 13:48:16 -04002912static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002913move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002914{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002915 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002916 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002917 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002918
Pekka Paalanen01388e22013-04-25 13:57:44 +03002919 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002920 if (surface == NULL)
2921 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002922
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002923 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002924 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2925 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002926 return;
2927
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002928 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002929}
2930
2931static void
Neil Robertsaba0f252013-10-03 16:43:05 +01002932touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2933{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002934 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01002935 struct weston_surface *surface;
2936 struct shell_surface *shsurf;
2937
2938 surface = weston_surface_get_main_surface(focus);
2939 if (surface == NULL)
2940 return;
2941
2942 shsurf = get_shell_surface(surface);
2943 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2944 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2945 return;
2946
2947 surface_touch_move(shsurf, (struct weston_seat *) seat);
2948}
2949
2950static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002951resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002952{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002953 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002954 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002955 uint32_t edges = 0;
2956 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002957 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002958
Pekka Paalanen01388e22013-04-25 13:57:44 +03002959 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002960 if (surface == NULL)
2961 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002962
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002963 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002964 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2965 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002966 return;
2967
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 weston_view_from_global(shsurf->view,
2969 wl_fixed_to_int(seat->pointer->grab_x),
2970 wl_fixed_to_int(seat->pointer->grab_y),
2971 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002972
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002974 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002976 edges |= 0;
2977 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002978 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002979
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002981 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002982 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002983 edges |= 0;
2984 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002985 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002986
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002987 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002988}
2989
2990static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002991surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002992 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002993{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002994 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002995 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002996 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002997 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002998
Pekka Paalanen01388e22013-04-25 13:57:44 +03002999 /* XXX: broken for windows containing sub-surfaces */
3000 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003001 if (surface == NULL)
3002 return;
3003
3004 shsurf = get_shell_surface(surface);
3005 if (!shsurf)
3006 return;
3007
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003009
Jason Ekstranda7af7042013-10-12 22:38:11 -05003010 if (shsurf->view->alpha > 1.0)
3011 shsurf->view->alpha = 1.0;
3012 if (shsurf->view->alpha < step)
3013 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003014
Jason Ekstranda7af7042013-10-12 22:38:11 -05003015 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003016 weston_surface_damage(surface);
3017}
3018
3019static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003020do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003021 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003022{
Daniel Stone37816df2012-05-16 18:45:18 +01003023 struct weston_seat *ws = (struct weston_seat *) seat;
3024 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003025 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003026 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003027
3028 wl_list_for_each(output, &compositor->output_list, link) {
3029 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003030 wl_fixed_to_double(seat->pointer->x),
3031 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003032 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003033 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003034 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003035 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003036 increment = -output->zoom.increment;
3037 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003038 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003039 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003040 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003041 else
3042 increment = 0;
3043
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003044 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003045
Scott Moreaue6603982012-06-11 13:07:51 -06003046 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003047 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003048 else if (output->zoom.level > output->zoom.max_level)
3049 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003050 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003051 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003052 output->disable_planes++;
3053 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003054
Scott Moreaue6603982012-06-11 13:07:51 -06003055 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003056
Jason Ekstranda7af7042013-10-12 22:38:11 -05003057 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003058 }
3059 }
3060}
3061
Scott Moreauccbf29d2012-02-22 14:21:41 -07003062static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003063zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003064 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003065{
3066 do_zoom(seat, time, 0, axis, value);
3067}
3068
3069static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003070zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003071 void *data)
3072{
3073 do_zoom(seat, time, key, 0, 0);
3074}
3075
3076static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003077terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003078 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003079{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003080 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003081
Daniel Stone325fc2d2012-05-30 16:31:58 +01003082 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003083}
3084
3085static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003086rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003087{
3088 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003089 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003090 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003091 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003092 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003093
3094 if (!shsurf)
3095 return;
3096
Jason Ekstranda7af7042013-10-12 22:38:11 -05003097 cx = 0.5f * shsurf->view->geometry.width;
3098 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003099
Daniel Stone37816df2012-05-16 18:45:18 +01003100 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3101 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003102 r = sqrtf(dx * dx + dy * dy);
3103
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003104 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003105 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003106
3107 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003108 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003109 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003110
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003111 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003112 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003113
3114 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003115 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003116 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003117 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003118 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003119
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003120 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003121 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003122 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003123 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003124 wl_list_init(&shsurf->rotation.transform.link);
3125 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003126 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003127 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003128
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003129 /* We need to adjust the position of the surface
3130 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003131 cposx = shsurf->view->geometry.x + cx;
3132 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003133 dposx = rotate->center.x - cposx;
3134 dposy = rotate->center.y - cposy;
3135 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003136 weston_view_set_position(shsurf->view,
3137 shsurf->view->geometry.x + dposx,
3138 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003139 }
3140
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003141 /* Repaint implies weston_surface_update_transform(), which
3142 * lazily applies the damage due to rotation update.
3143 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003144 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003145}
3146
3147static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003148rotate_grab_button(struct weston_pointer_grab *grab,
3149 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003150{
3151 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003152 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003153 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003154 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003155 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003156
Daniel Stone4dbadb12012-05-30 16:31:51 +01003157 if (pointer->button_count == 0 &&
3158 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003159 if (shsurf)
3160 weston_matrix_multiply(&shsurf->rotation.rotation,
3161 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003162 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003163 free(rotate);
3164 }
3165}
3166
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003167static void
3168rotate_grab_cancel(struct weston_pointer_grab *grab)
3169{
3170 struct rotate_grab *rotate =
3171 container_of(grab, struct rotate_grab, base.grab);
3172
3173 shell_grab_end(&rotate->base);
3174 free(rotate);
3175}
3176
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003177static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003178 noop_grab_focus,
3179 rotate_grab_motion,
3180 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003181 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003182};
3183
3184static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003185surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003186{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003187 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003188 float dx, dy;
3189 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003190
Pekka Paalanen460099f2012-01-20 16:48:25 +02003191 rotate = malloc(sizeof *rotate);
3192 if (!rotate)
3193 return;
3194
Jason Ekstranda7af7042013-10-12 22:38:11 -05003195 weston_view_to_global_float(surface->view,
3196 surface->view->geometry.width * 0.5f,
3197 surface->view->geometry.height * 0.5f,
3198 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003199
Daniel Stone37816df2012-05-16 18:45:18 +01003200 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3201 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003202 r = sqrtf(dx * dx + dy * dy);
3203 if (r > 20.0f) {
3204 struct weston_matrix inverse;
3205
3206 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003207 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003208 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003209
3210 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003211 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003212 } else {
3213 weston_matrix_init(&surface->rotation.rotation);
3214 weston_matrix_init(&rotate->rotation);
3215 }
3216
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003217 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3218 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003219}
3220
3221static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003222rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003223 void *data)
3224{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003225 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003226 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003227 struct shell_surface *surface;
3228
Pekka Paalanen01388e22013-04-25 13:57:44 +03003229 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003230 if (base_surface == NULL)
3231 return;
3232
3233 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003234 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3235 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003236 return;
3237
3238 surface_rotate(surface, seat);
3239}
3240
3241static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003242lower_fullscreen_layer(struct desktop_shell *shell)
3243{
3244 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003246
3247 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003248 wl_list_for_each_reverse_safe(view, prev,
3249 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003250 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003252}
3253
3254static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003255activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003256 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003257{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003258 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003259 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003260 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003261 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003262
Pekka Paalanen01388e22013-04-25 13:57:44 +03003263 main_surface = weston_surface_get_main_surface(es);
3264
Daniel Stone37816df2012-05-16 18:45:18 +01003265 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003266
Jonas Ådahl8538b222012-08-29 22:13:03 +02003267 state = ensure_focus_state(shell, seat);
3268 if (state == NULL)
3269 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003270
3271 state->keyboard_focus = es;
3272 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003273 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003274
Pekka Paalanen01388e22013-04-25 13:57:44 +03003275 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003276 case SHELL_SURFACE_FULLSCREEN:
3277 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003278 shell_stack_fullscreen(get_shell_surface(main_surface));
3279 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003280 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003281 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003282 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003283 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003284 main_view = get_default_view(main_surface);
3285 if (main_view)
3286 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003287 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003288 }
3289}
3290
Alex Wu21858432012-04-01 20:13:08 +08003291/* no-op func for checking black surface */
3292static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003293black_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 +08003294{
3295}
3296
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003297static bool
Alex Wu21858432012-04-01 20:13:08 +08003298is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3299{
3300 if (es->configure == black_surface_configure) {
3301 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003302 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003303 return true;
3304 }
3305 return false;
3306}
3307
Kristian Høgsberg75840622011-09-06 13:48:16 -04003308static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003309activate_binding(struct weston_seat *seat,
3310 struct desktop_shell *shell,
3311 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003312{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003313 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003314
Alex Wu9c35e6b2012-03-05 14:13:13 +08003315 if (!focus)
3316 return;
3317
Pekka Paalanen01388e22013-04-25 13:57:44 +03003318 if (is_black_surface(focus, &main_surface))
3319 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003320
Pekka Paalanen01388e22013-04-25 13:57:44 +03003321 main_surface = weston_surface_get_main_surface(focus);
3322 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003323 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003324
Neil Robertsa28c6932013-10-03 16:43:04 +01003325 activate(shell, focus, seat);
3326}
3327
3328static void
3329click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3330 void *data)
3331{
3332 if (seat->pointer->grab != &seat->pointer->default_grab)
3333 return;
3334
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003335 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003336}
3337
3338static void
3339touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3340{
3341 if (seat->touch->grab != &seat->touch->default_grab)
3342 return;
3343
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003344 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003345}
3346
3347static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003348lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003349{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003350 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003351
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003352 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003353 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003354 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003355 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003356
3357 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003358
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003359 /* Hide all surfaces by removing the fullscreen, panel and
3360 * toplevel layers. This way nothing else can show or receive
3361 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003362
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003363 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003364 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003365 if (shell->showing_input_panels)
3366 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003367 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003368 wl_list_insert(&shell->compositor->cursor_layer.link,
3369 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003370
Pekka Paalanen77346a62011-11-30 16:26:35 +02003371 launch_screensaver(shell);
3372
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003373 /* TODO: disable bindings that should not work while locked. */
3374
3375 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003376}
3377
3378static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003379unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003380{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003381 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003382 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003383 return;
3384 }
3385
3386 /* If desktop-shell client has gone away, unlock immediately. */
3387 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003388 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003389 return;
3390 }
3391
3392 if (shell->prepare_event_sent)
3393 return;
3394
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003395 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003396 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003397}
3398
3399static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003400shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003401{
3402 struct desktop_shell *shell = data;
3403
3404 shell->fade.animation = NULL;
3405
3406 switch (shell->fade.type) {
3407 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003408 weston_surface_destroy(shell->fade.view->surface);
3409 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003410 break;
3411 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003412 lock(shell);
3413 break;
3414 }
3415}
3416
Jason Ekstranda7af7042013-10-12 22:38:11 -05003417static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003418shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003419{
3420 struct weston_compositor *compositor = shell->compositor;
3421 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003422 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003423
3424 surface = weston_surface_create(compositor);
3425 if (!surface)
3426 return NULL;
3427
Jason Ekstranda7af7042013-10-12 22:38:11 -05003428 view = weston_view_create(surface);
3429 if (!view) {
3430 weston_surface_destroy(surface);
3431 return NULL;
3432 }
3433
3434 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003435 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003436 wl_list_insert(&compositor->fade_layer.view_list,
3437 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003438 pixman_region32_init(&surface->input);
3439
Jason Ekstranda7af7042013-10-12 22:38:11 -05003440 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003441}
3442
3443static void
3444shell_fade(struct desktop_shell *shell, enum fade_type type)
3445{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003446 float tint;
3447
3448 switch (type) {
3449 case FADE_IN:
3450 tint = 0.0;
3451 break;
3452 case FADE_OUT:
3453 tint = 1.0;
3454 break;
3455 default:
3456 weston_log("shell: invalid fade type\n");
3457 return;
3458 }
3459
3460 shell->fade.type = type;
3461
Jason Ekstranda7af7042013-10-12 22:38:11 -05003462 if (shell->fade.view == NULL) {
3463 shell->fade.view = shell_fade_create_surface(shell);
3464 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003465 return;
3466
Jason Ekstranda7af7042013-10-12 22:38:11 -05003467 shell->fade.view->alpha = 1.0 - tint;
3468 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003469 }
3470
3471 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003472 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003473 else
3474 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003475 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003476 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003477 shell_fade_done, shell);
3478}
3479
3480static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003481do_shell_fade_startup(void *data)
3482{
3483 struct desktop_shell *shell = data;
3484
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003485 if (shell->startup_animation_type == ANIMATION_FADE)
3486 shell_fade(shell, FADE_IN);
3487 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003488 weston_surface_destroy(shell->fade.view->surface);
3489 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003490 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003491}
3492
3493static void
3494shell_fade_startup(struct desktop_shell *shell)
3495{
3496 struct wl_event_loop *loop;
3497
3498 if (!shell->fade.startup_timer)
3499 return;
3500
3501 wl_event_source_remove(shell->fade.startup_timer);
3502 shell->fade.startup_timer = NULL;
3503
3504 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3505 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3506}
3507
3508static int
3509fade_startup_timeout(void *data)
3510{
3511 struct desktop_shell *shell = data;
3512
3513 shell_fade_startup(shell);
3514 return 0;
3515}
3516
3517static void
3518shell_fade_init(struct desktop_shell *shell)
3519{
3520 /* Make compositor output all black, and wait for the desktop-shell
3521 * client to signal it is ready, then fade in. The timer triggers a
3522 * fade-in, in case the desktop-shell client takes too long.
3523 */
3524
3525 struct wl_event_loop *loop;
3526
Jason Ekstranda7af7042013-10-12 22:38:11 -05003527 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003528 weston_log("%s: warning: fade surface already exists\n",
3529 __func__);
3530 return;
3531 }
3532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 shell->fade.view = shell_fade_create_surface(shell);
3534 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003535 return;
3536
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 weston_view_update_transform(shell->fade.view);
3538 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003539
3540 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3541 shell->fade.startup_timer =
3542 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3543 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3544}
3545
3546static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003547idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003548{
3549 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003550 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003551
3552 shell_fade(shell, FADE_OUT);
3553 /* lock() is called from shell_fade_done() */
3554}
3555
3556static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003557wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003558{
3559 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003560 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003561
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003562 unlock(shell);
3563}
3564
3565static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003566show_input_panels(struct wl_listener *listener, void *data)
3567{
3568 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003569 container_of(listener, struct desktop_shell,
3570 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003571 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003572
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003573 shell->text_input.surface = (struct weston_surface*)data;
3574
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003575 if (shell->showing_input_panels)
3576 return;
3577
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003578 shell->showing_input_panels = true;
3579
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003580 if (!shell->locked)
3581 wl_list_insert(&shell->panel_layer.link,
3582 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003583
Jason Ekstranda7af7042013-10-12 22:38:11 -05003584 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003585 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003586 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003587 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003588 wl_list_insert(&shell->input_panel_layer.view_list,
3589 &ipsurf->view->layer_link);
3590 weston_view_geometry_dirty(ipsurf->view);
3591 weston_view_update_transform(ipsurf->view);
3592 weston_surface_damage(ipsurf->surface);
3593 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3594 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003595 }
3596}
3597
3598static void
3599hide_input_panels(struct wl_listener *listener, void *data)
3600{
3601 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003602 container_of(listener, struct desktop_shell,
3603 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003604 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003605
Jan Arne Petersen61381972013-01-31 15:52:21 +01003606 if (!shell->showing_input_panels)
3607 return;
3608
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003609 shell->showing_input_panels = false;
3610
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003611 if (!shell->locked)
3612 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003613
Jason Ekstranda7af7042013-10-12 22:38:11 -05003614 wl_list_for_each_safe(view, next,
3615 &shell->input_panel_layer.view_list, layer_link)
3616 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003617}
3618
3619static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003620update_input_panels(struct wl_listener *listener, void *data)
3621{
3622 struct desktop_shell *shell =
3623 container_of(listener, struct desktop_shell,
3624 update_input_panel_listener);
3625
3626 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3627}
3628
3629static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003631{
Giulio Camuffob8366642013-04-25 13:57:46 +03003632 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003633 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003634
Jason Ekstranda7af7042013-10-12 22:38:11 -05003635 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003636
3637 x = output->x + (output->width - width) / 2 - surf_x / 2;
3638 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003639
Jason Ekstranda7af7042013-10-12 22:38:11 -05003640 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003641}
3642
3643static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003644weston_view_set_initial_position(struct weston_view *view,
3645 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003646{
3647 struct weston_compositor *compositor = shell->compositor;
3648 int ix = 0, iy = 0;
3649 int range_x, range_y;
3650 int dx, dy, x, y, panel_height;
3651 struct weston_output *output, *target_output = NULL;
3652 struct weston_seat *seat;
3653
3654 /* As a heuristic place the new window on the same output as the
3655 * pointer. Falling back to the output containing 0, 0.
3656 *
3657 * TODO: Do something clever for touch too?
3658 */
3659 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003660 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003661 ix = wl_fixed_to_int(seat->pointer->x);
3662 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003663 break;
3664 }
3665 }
3666
3667 wl_list_for_each(output, &compositor->output_list, link) {
3668 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3669 target_output = output;
3670 break;
3671 }
3672 }
3673
3674 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003675 weston_view_set_position(view, 10 + random() % 400,
3676 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003677 return;
3678 }
3679
3680 /* Valid range within output where the surface will still be onscreen.
3681 * If this is negative it means that the surface is bigger than
3682 * output.
3683 */
3684 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003685 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003686 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003687 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003688
Rob Bradford4cb88c72012-08-13 15:18:44 +01003689 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003690 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003691 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003692 dx = 0;
3693
3694 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003695 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003696 else
3697 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003698
3699 x = target_output->x + dx;
3700 y = target_output->y + dy;
3701
Jason Ekstranda7af7042013-10-12 22:38:11 -05003702 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003703}
3704
3705static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003706map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003707 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003708{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003709 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003710 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003711 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003712 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003713 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003714 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003715
Jason Ekstranda7af7042013-10-12 22:38:11 -05003716 shsurf->view->geometry.width = width;
3717 shsurf->view->geometry.height = height;
3718 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003719
3720 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003721 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003722 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003723 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003724 break;
Alex Wu4539b082012-03-01 12:57:46 +08003725 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003726 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003727 shell_map_fullscreen(shsurf);
3728 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003729 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003730 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003731 panel_height = get_output_panel_height(shell, shsurf->output);
3732 surface_subsurfaces_boundingbox(shsurf->surface,
3733 &surf_x, &surf_y, NULL, NULL);
3734 weston_view_set_position(shsurf->view,
3735 shsurf->output->x - surf_x,
3736 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003737 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003738 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003739 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003740 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003741 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003742 weston_view_set_position(shsurf->view,
3743 shsurf->view->geometry.x + sx,
3744 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003745 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003746 default:
3747 ;
3748 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003749
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003750 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003751 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003752 case SHELL_SURFACE_POPUP:
3753 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003754 /* TODO: Handle a parent with multiple views */
3755 parent = get_default_view(shsurf->parent);
3756 if (parent) {
3757 wl_list_remove(&shsurf->view->layer_link);
3758 wl_list_insert(parent->layer_link.prev,
3759 &shsurf->view->layer_link);
3760 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003761 break;
Alex Wu4539b082012-03-01 12:57:46 +08003762 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003763 case SHELL_SURFACE_NONE:
3764 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003765 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003766 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003767 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003768 wl_list_remove(&shsurf->view->layer_link);
3769 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003770 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003771 }
3772
Jason Ekstranda7af7042013-10-12 22:38:11 -05003773 if (shsurf->type != SHELL_SURFACE_NONE) {
3774 weston_view_update_transform(shsurf->view);
3775 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
3776 shsurf->surface->output = shsurf->output;
3777 shsurf->view->output = shsurf->output;
3778 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003779 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003780
Jason Ekstranda7af7042013-10-12 22:38:11 -05003781 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003782 /* XXX: xwayland's using the same fields for transient type */
3783 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003784 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003785 if (shsurf->transient.flags ==
3786 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3787 break;
3788 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003789 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003790 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003791 if (!shell->locked) {
3792 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003793 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01003794 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003795 break;
3796 default:
3797 break;
3798 }
3799
Jason Ekstranda7af7042013-10-12 22:38:11 -05003800 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003801 {
3802 switch (shell->win_animation_type) {
3803 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003804 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003805 break;
3806 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003807 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003808 break;
3809 default:
3810 break;
3811 }
3812 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003813}
3814
3815static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003816configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003817 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003818{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003819 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3820 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003821 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03003822 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003823
Pekka Paalanen77346a62011-11-30 16:26:35 +02003824 shsurf = get_shell_surface(surface);
3825 if (shsurf)
3826 surface_type = shsurf->type;
3827
Jason Ekstranda7af7042013-10-12 22:38:11 -05003828 /* TODO:
3829 * This should probably be changed to be more shell_surface
3830 * dependent
3831 */
3832 wl_list_for_each(view, &surface->views, surface_link)
3833 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003834
3835 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003836 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003837 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003838 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003839 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003840 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003841 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003842 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3843 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003844 shsurf->view->geometry.x = shsurf->output->x - surf_x;
3845 shsurf->view->geometry.y = shsurf->output->y +
3846 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003847 break;
Alex Wu4539b082012-03-01 12:57:46 +08003848 case SHELL_SURFACE_TOPLEVEL:
3849 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003850 default:
3851 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003852 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003853
Alex Wu4539b082012-03-01 12:57:46 +08003854 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003855 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003856 wl_list_for_each(view, &surface->views, surface_link)
3857 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003858
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003859 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003860 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003861 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003862}
3863
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003864static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003865shell_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 +03003866{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003867 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003868 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003869
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003870 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003871
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003872 if (!weston_surface_is_mapped(es) &&
3873 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003874 remove_popup_grab(shsurf);
3875 }
3876
Giulio Camuffo184df502013-02-21 11:29:21 +01003877 if (width == 0)
3878 return;
3879
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003880 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003881 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003882 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003883 type_changed = 1;
3884 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003885
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003886 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003887 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003888 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05003889 shsurf->view->geometry.width != width ||
3890 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003891 float from_x, from_y;
3892 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003893
Jason Ekstranda7af7042013-10-12 22:38:11 -05003894 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
3895 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003896 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 shsurf->view->geometry.x + to_x - from_x,
3898 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003899 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003900 }
3901}
3902
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003903static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003904
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003905static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003906desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003907{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003908 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003909 struct desktop_shell *shell =
3910 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003911
3912 shell->child.process.pid = 0;
3913 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003914
3915 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3916 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003917 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003918 shell->child.deathstamp = time;
3919 shell->child.deathcount = 0;
3920 }
3921
3922 shell->child.deathcount++;
3923 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003924 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003925 return;
3926 }
3927
Martin Minarik6d118362012-06-07 18:01:59 +02003928 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003929 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003930 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003931}
3932
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003933static void
3934launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003935{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003936 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003937 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003938
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003939 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003940 &shell->child.process,
3941 shell_exe,
3942 desktop_shell_sigchld);
3943
3944 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003945 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003946}
3947
3948static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003949bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3950{
Tiago Vignattibe143262012-04-16 17:31:41 +03003951 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003952 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003953
Jason Ekstranda85118c2013-06-27 20:17:02 -05003954 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3955 if (resource)
3956 wl_resource_set_implementation(resource, &shell_implementation,
3957 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003958}
3959
Kristian Høgsberg75840622011-09-06 13:48:16 -04003960static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003961unbind_desktop_shell(struct wl_resource *resource)
3962{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003963 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003964
3965 if (shell->locked)
3966 resume_desktop(shell);
3967
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003968 shell->child.desktop_shell = NULL;
3969 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003970}
3971
3972static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003973bind_desktop_shell(struct wl_client *client,
3974 void *data, uint32_t version, uint32_t id)
3975{
Tiago Vignattibe143262012-04-16 17:31:41 +03003976 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003977 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003978
Jason Ekstranda85118c2013-06-27 20:17:02 -05003979 resource = wl_resource_create(client, &desktop_shell_interface,
3980 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003981
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003982 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05003983 wl_resource_set_implementation(resource,
3984 &desktop_shell_implementation,
3985 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003986 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003987
3988 if (version < 2)
3989 shell_fade_startup(shell);
3990
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003991 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003992 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003993
3994 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3995 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003996 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003997}
3998
Pekka Paalanen6e168112011-11-24 11:34:05 +02003999static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004000screensaver_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 -04004001{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004002 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004003 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004004
Giulio Camuffo184df502013-02-21 11:29:21 +01004005 if (width == 0)
4006 return;
4007
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004008 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004009 if (!shell->locked)
4010 return;
4011
Jason Ekstranda7af7042013-10-12 22:38:11 -05004012 view = container_of(surface->views.next, struct weston_view, surface_link);
4013 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004014
Jason Ekstranda7af7042013-10-12 22:38:11 -05004015 if (wl_list_empty(&view->layer_link)) {
4016 wl_list_insert(shell->lock_layer.view_list.prev,
4017 &view->layer_link);
4018 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004019 wl_event_source_timer_update(shell->screensaver.timer,
4020 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004021 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004022 }
4023}
4024
4025static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004026screensaver_set_surface(struct wl_client *client,
4027 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004028 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004029 struct wl_resource *output_resource)
4030{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004031 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004032 struct weston_surface *surface =
4033 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004034 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004035 struct weston_view *view, *next;
4036
4037 /* Make sure we only have one view */
4038 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4039 weston_view_destroy(view);
4040 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004041
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004042 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004043 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004044 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004045}
4046
4047static const struct screensaver_interface screensaver_implementation = {
4048 screensaver_set_surface
4049};
4050
4051static void
4052unbind_screensaver(struct wl_resource *resource)
4053{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004054 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004055
Pekka Paalanen77346a62011-11-30 16:26:35 +02004056 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004057}
4058
4059static void
4060bind_screensaver(struct wl_client *client,
4061 void *data, uint32_t version, uint32_t id)
4062{
Tiago Vignattibe143262012-04-16 17:31:41 +03004063 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004064 struct wl_resource *resource;
4065
Jason Ekstranda85118c2013-06-27 20:17:02 -05004066 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004067
Pekka Paalanen77346a62011-11-30 16:26:35 +02004068 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004069 wl_resource_set_implementation(resource,
4070 &screensaver_implementation,
4071 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004072 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004073 return;
4074 }
4075
4076 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4077 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004078 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004079}
4080
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004081static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004082input_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 -04004083{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004084 struct input_panel_surface *ip_surface = surface->configure_private;
4085 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004086 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004087 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004088
Giulio Camuffo184df502013-02-21 11:29:21 +01004089 if (width == 0)
4090 return;
4091
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004092 if (!weston_surface_is_mapped(surface)) {
4093 if (!shell->showing_input_panels)
4094 return;
4095
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004096 show_surface = 1;
4097 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004098
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004099 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004100
4101 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004102 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4103 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004104 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004105 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4106 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004107 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004108
Jason Ekstranda7af7042013-10-12 22:38:11 -05004109 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004110
4111 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112 wl_list_insert(&shell->input_panel_layer.view_list,
4113 &ip_surface->view->layer_link);
4114 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004115 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004116 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004117 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004118}
4119
4120static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004121destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004122{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004123 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4124
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004125 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004126 wl_list_remove(&input_panel_surface->link);
4127
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004128 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004129 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004130
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004131 free(input_panel_surface);
4132}
4133
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004134static struct input_panel_surface *
4135get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004136{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004137 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004138 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004139 } else {
4140 return NULL;
4141 }
4142}
4143
4144static void
4145input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4146{
4147 struct input_panel_surface *ipsurface = container_of(listener,
4148 struct input_panel_surface,
4149 surface_destroy_listener);
4150
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004151 if (ipsurface->resource) {
4152 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004153 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004154 destroy_input_panel_surface(ipsurface);
4155 }
4156}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004157
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004158static struct input_panel_surface *
4159create_input_panel_surface(struct desktop_shell *shell,
4160 struct weston_surface *surface)
4161{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004162 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004163
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004164 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4165 if (!input_panel_surface)
4166 return NULL;
4167
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004168 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004169 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004170
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004171 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004172
4173 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004174 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004175
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004176 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004177 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004178 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004179 &input_panel_surface->surface_destroy_listener);
4180
4181 wl_list_init(&input_panel_surface->link);
4182
4183 return input_panel_surface;
4184}
4185
4186static void
4187input_panel_surface_set_toplevel(struct wl_client *client,
4188 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004189 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004190 uint32_t position)
4191{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004192 struct input_panel_surface *input_panel_surface =
4193 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004194 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004195
4196 wl_list_insert(&shell->input_panel.surfaces,
4197 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004198
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004199 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004200 input_panel_surface->panel = 0;
4201}
4202
4203static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004204input_panel_surface_set_overlay_panel(struct wl_client *client,
4205 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004206{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004207 struct input_panel_surface *input_panel_surface =
4208 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004209 struct desktop_shell *shell = input_panel_surface->shell;
4210
4211 wl_list_insert(&shell->input_panel.surfaces,
4212 &input_panel_surface->link);
4213
4214 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004215}
4216
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004217static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004218 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004219 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004220};
4221
4222static void
4223destroy_input_panel_surface_resource(struct wl_resource *resource)
4224{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004225 struct input_panel_surface *ipsurf =
4226 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004227
4228 destroy_input_panel_surface(ipsurf);
4229}
4230
4231static void
4232input_panel_get_input_panel_surface(struct wl_client *client,
4233 struct wl_resource *resource,
4234 uint32_t id,
4235 struct wl_resource *surface_resource)
4236{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004237 struct weston_surface *surface =
4238 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004239 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004240 struct input_panel_surface *ipsurf;
4241
4242 if (get_input_panel_surface(surface)) {
4243 wl_resource_post_error(surface_resource,
4244 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004245 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004246 return;
4247 }
4248
4249 ipsurf = create_input_panel_surface(shell, surface);
4250 if (!ipsurf) {
4251 wl_resource_post_error(surface_resource,
4252 WL_DISPLAY_ERROR_INVALID_OBJECT,
4253 "surface->configure already set");
4254 return;
4255 }
4256
Jason Ekstranda85118c2013-06-27 20:17:02 -05004257 ipsurf->resource =
4258 wl_resource_create(client,
4259 &wl_input_panel_surface_interface, 1, id);
4260 wl_resource_set_implementation(ipsurf->resource,
4261 &input_panel_surface_implementation,
4262 ipsurf,
4263 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004264}
4265
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004266static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004267 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004268};
4269
4270static void
4271unbind_input_panel(struct wl_resource *resource)
4272{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004273 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004274
4275 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004276}
4277
4278static void
4279bind_input_panel(struct wl_client *client,
4280 void *data, uint32_t version, uint32_t id)
4281{
4282 struct desktop_shell *shell = data;
4283 struct wl_resource *resource;
4284
Jason Ekstranda85118c2013-06-27 20:17:02 -05004285 resource = wl_resource_create(client,
4286 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004287
4288 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004289 wl_resource_set_implementation(resource,
4290 &input_panel_implementation,
4291 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004292 shell->input_panel.binding = resource;
4293 return;
4294 }
4295
4296 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4297 "interface object already bound");
4298 wl_resource_destroy(resource);
4299}
4300
Kristian Høgsberg07045392012-02-19 18:52:44 -05004301struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004302 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004303 struct weston_surface *current;
4304 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004305 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004306};
4307
4308static void
4309switcher_next(struct switcher *switcher)
4310{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004311 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004312 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004313 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004314 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004315
Jason Ekstranda7af7042013-10-12 22:38:11 -05004316 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4317 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004318 case SHELL_SURFACE_TOPLEVEL:
4319 case SHELL_SURFACE_FULLSCREEN:
4320 case SHELL_SURFACE_MAXIMIZED:
4321 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004323 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 next = view->surface;
4325 prev = view->surface;
4326 view->alpha = 0.25;
4327 weston_view_geometry_dirty(view);
4328 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004329 break;
4330 default:
4331 break;
4332 }
Alex Wu1659daa2012-04-01 20:13:09 +08004333
Jason Ekstranda7af7042013-10-12 22:38:11 -05004334 if (is_black_surface(view->surface, NULL)) {
4335 view->alpha = 0.25;
4336 weston_view_geometry_dirty(view);
4337 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004338 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004339 }
4340
4341 if (next == NULL)
4342 next = first;
4343
Alex Wu07b26062012-03-12 16:06:01 +08004344 if (next == NULL)
4345 return;
4346
Kristian Høgsberg07045392012-02-19 18:52:44 -05004347 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004348 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004349
4350 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 wl_list_for_each(view, &next->views, surface_link)
4352 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004353
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004354 shsurf = get_shell_surface(switcher->current);
4355 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004356 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004357}
4358
4359static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004360switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004361{
4362 struct switcher *switcher =
4363 container_of(listener, struct switcher, listener);
4364
4365 switcher_next(switcher);
4366}
4367
4368static void
Daniel Stone351eb612012-05-31 15:27:47 -04004369switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004370{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004372 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004373 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004374
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4376 view->alpha = 1.0;
4377 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004378 }
4379
Alex Wu07b26062012-03-12 16:06:01 +08004380 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004381 activate(switcher->shell, switcher->current,
4382 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004384 weston_keyboard_end_grab(keyboard);
4385 if (keyboard->input_method_resource)
4386 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004387 free(switcher);
4388}
4389
4390static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004391switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004392 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004393{
4394 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004395 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004396
Daniel Stonec9785ea2012-05-30 16:31:52 +01004397 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004398 switcher_next(switcher);
4399}
4400
4401static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004402switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004403 uint32_t mods_depressed, uint32_t mods_latched,
4404 uint32_t mods_locked, uint32_t group)
4405{
4406 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004407 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004408
Daniel Stone351eb612012-05-31 15:27:47 -04004409 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4410 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004411}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004412
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004413static void
4414switcher_cancel(struct weston_keyboard_grab *grab)
4415{
4416 struct switcher *switcher = container_of(grab, struct switcher, grab);
4417
4418 switcher_destroy(switcher);
4419}
4420
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004421static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004422 switcher_key,
4423 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004424 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004425};
4426
4427static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004428switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004429 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004430{
Tiago Vignattibe143262012-04-16 17:31:41 +03004431 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004432 struct switcher *switcher;
4433
4434 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004435 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004436 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004437 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004438 wl_list_init(&switcher->listener.link);
4439
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004440 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004441 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004442 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004443 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4444 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004445 switcher_next(switcher);
4446}
4447
Pekka Paalanen3c647232011-12-22 13:43:43 +02004448static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004449backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004450 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004451{
4452 struct weston_compositor *compositor = data;
4453 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004454 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004455
4456 /* TODO: we're limiting to simple use cases, where we assume just
4457 * control on the primary display. We'd have to extend later if we
4458 * ever get support for setting backlights on random desktop LCD
4459 * panels though */
4460 output = get_default_output(compositor);
4461 if (!output)
4462 return;
4463
4464 if (!output->set_backlight)
4465 return;
4466
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004467 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4468 backlight_new = output->backlight_current - 25;
4469 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4470 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004471
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004472 if (backlight_new < 5)
4473 backlight_new = 5;
4474 if (backlight_new > 255)
4475 backlight_new = 255;
4476
4477 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004478 output->set_backlight(output, output->backlight_current);
4479}
4480
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004481struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004482 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004483 struct weston_seat *seat;
4484 uint32_t key[2];
4485 int key_released[2];
4486};
4487
4488static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004489debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004490 uint32_t key, uint32_t state)
4491{
4492 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004493 struct weston_compositor *ec = db->seat->compositor;
4494 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004495 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004496 uint32_t serial;
4497 int send = 0, terminate = 0;
4498 int check_binding = 1;
4499 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004500 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004501
4502 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4503 /* Do not run bindings on key releases */
4504 check_binding = 0;
4505
4506 for (i = 0; i < 2; i++)
4507 if (key == db->key[i])
4508 db->key_released[i] = 1;
4509
4510 if (db->key_released[0] && db->key_released[1]) {
4511 /* All key releases been swalled so end the grab */
4512 terminate = 1;
4513 } else if (key != db->key[0] && key != db->key[1]) {
4514 /* Should not swallow release of other keys */
4515 send = 1;
4516 }
4517 } else if (key == db->key[0] && !db->key_released[0]) {
4518 /* Do not check bindings for the first press of the binding
4519 * key. This allows it to be used as a debug shortcut.
4520 * We still need to swallow this event. */
4521 check_binding = 0;
4522 } else if (db->key[1]) {
4523 /* If we already ran a binding don't process another one since
4524 * we can't keep track of all the binding keys that were
4525 * pressed in order to swallow the release events. */
4526 send = 1;
4527 check_binding = 0;
4528 }
4529
4530 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004531 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4532 key, state)) {
4533 /* We ran a binding so swallow the press and keep the
4534 * grab to swallow the released too. */
4535 send = 0;
4536 terminate = 0;
4537 db->key[1] = key;
4538 } else {
4539 /* Terminate the grab since the key pressed is not a
4540 * debug binding key. */
4541 send = 1;
4542 terminate = 1;
4543 }
4544 }
4545
4546 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004547 serial = wl_display_next_serial(display);
4548 resource_list = &grab->keyboard->focus_resource_list;
4549 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004550 wl_keyboard_send_key(resource, serial, time, key, state);
4551 }
4552 }
4553
4554 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004555 weston_keyboard_end_grab(grab->keyboard);
4556 if (grab->keyboard->input_method_resource)
4557 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004558 free(db);
4559 }
4560}
4561
4562static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004563debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004564 uint32_t mods_depressed, uint32_t mods_latched,
4565 uint32_t mods_locked, uint32_t group)
4566{
4567 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004568 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004569
Neil Roberts96d790e2013-09-19 17:32:00 +01004570 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004571
Neil Roberts96d790e2013-09-19 17:32:00 +01004572 wl_resource_for_each(resource, resource_list) {
4573 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4574 mods_latched, mods_locked, group);
4575 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004576}
4577
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004578static void
4579debug_binding_cancel(struct weston_keyboard_grab *grab)
4580{
4581 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4582
4583 weston_keyboard_end_grab(grab->keyboard);
4584 free(db);
4585}
4586
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004587struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004588 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004589 debug_binding_modifiers,
4590 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004591};
4592
4593static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004594debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004595{
4596 struct debug_binding_grab *grab;
4597
4598 grab = calloc(1, sizeof *grab);
4599 if (!grab)
4600 return;
4601
4602 grab->seat = (struct weston_seat *) seat;
4603 grab->key[0] = key;
4604 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004605 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004606}
4607
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004608static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004609force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004610 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004611{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004612 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004613 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004614 struct desktop_shell *shell = data;
4615 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004616 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004617
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004618 focus_surface = seat->keyboard->focus;
4619 if (!focus_surface)
4620 return;
4621
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004622 wl_signal_emit(&compositor->kill_signal, focus_surface);
4623
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004624 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004625 wl_client_get_credentials(client, &pid, NULL, NULL);
4626
4627 /* Skip clients that we launched ourselves (the credentials of
4628 * the socketpair is ours) */
4629 if (pid == getpid())
4630 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004631
Daniel Stone325fc2d2012-05-30 16:31:58 +01004632 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004633}
4634
4635static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004636workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004637 uint32_t key, void *data)
4638{
4639 struct desktop_shell *shell = data;
4640 unsigned int new_index = shell->workspaces.current;
4641
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004642 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004643 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004644 if (new_index != 0)
4645 new_index--;
4646
4647 change_workspace(shell, new_index);
4648}
4649
4650static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004651workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004652 uint32_t key, void *data)
4653{
4654 struct desktop_shell *shell = data;
4655 unsigned int new_index = shell->workspaces.current;
4656
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004657 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004658 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004659 if (new_index < shell->workspaces.num - 1)
4660 new_index++;
4661
4662 change_workspace(shell, new_index);
4663}
4664
4665static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004666workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004667 uint32_t key, void *data)
4668{
4669 struct desktop_shell *shell = data;
4670 unsigned int new_index;
4671
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004672 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004673 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004674 new_index = key - KEY_F1;
4675 if (new_index >= shell->workspaces.num)
4676 new_index = shell->workspaces.num - 1;
4677
4678 change_workspace(shell, new_index);
4679}
4680
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004681static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004682workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004683 uint32_t key, void *data)
4684{
4685 struct desktop_shell *shell = data;
4686 unsigned int new_index = shell->workspaces.current;
4687
4688 if (shell->locked)
4689 return;
4690
4691 if (new_index != 0)
4692 new_index--;
4693
4694 take_surface_to_workspace_by_seat(shell, seat, new_index);
4695}
4696
4697static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004698workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004699 uint32_t key, void *data)
4700{
4701 struct desktop_shell *shell = data;
4702 unsigned int new_index = shell->workspaces.current;
4703
4704 if (shell->locked)
4705 return;
4706
4707 if (new_index < shell->workspaces.num - 1)
4708 new_index++;
4709
4710 take_surface_to_workspace_by_seat(shell, seat, new_index);
4711}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004712
4713static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004714handle_output_destroy(struct wl_listener *listener, void *data)
4715{
4716 struct shell_output *output_listener =
4717 container_of(listener, struct shell_output, destroy_listener);
4718
4719 wl_list_remove(&output_listener->destroy_listener.link);
4720 wl_list_remove(&output_listener->link);
4721 free(output_listener);
4722}
4723
4724static void
4725create_shell_output(struct desktop_shell *shell,
4726 struct weston_output *output)
4727{
4728 struct shell_output *shell_output;
4729
4730 shell_output = zalloc(sizeof *shell_output);
4731 if (shell_output == NULL)
4732 return;
4733
4734 shell_output->output = output;
4735 shell_output->shell = shell;
4736 shell_output->destroy_listener.notify = handle_output_destroy;
4737 wl_signal_add(&output->destroy_signal,
4738 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004739 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004740}
4741
4742static void
4743handle_output_create(struct wl_listener *listener, void *data)
4744{
4745 struct desktop_shell *shell =
4746 container_of(listener, struct desktop_shell, output_create_listener);
4747 struct weston_output *output = (struct weston_output *)data;
4748
4749 create_shell_output(shell, output);
4750}
4751
4752static void
4753setup_output_destroy_handler(struct weston_compositor *ec,
4754 struct desktop_shell *shell)
4755{
4756 struct weston_output *output;
4757
4758 wl_list_init(&shell->output_list);
4759 wl_list_for_each(output, &ec->output_list, link)
4760 create_shell_output(shell, output);
4761
4762 shell->output_create_listener.notify = handle_output_create;
4763 wl_signal_add(&ec->output_created_signal,
4764 &shell->output_create_listener);
4765}
4766
4767static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004768shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004769{
Tiago Vignattibe143262012-04-16 17:31:41 +03004770 struct desktop_shell *shell =
4771 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004772 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004773 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004774
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004775 if (shell->child.client)
4776 wl_client_destroy(shell->child.client);
4777
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004778 wl_list_remove(&shell->idle_listener.link);
4779 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004780 wl_list_remove(&shell->show_input_panel_listener.link);
4781 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004782
Xiong Zhang6b481422013-10-23 13:58:32 +08004783 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4784 wl_list_remove(&shell_output->destroy_listener.link);
4785 wl_list_remove(&shell_output->link);
4786 free(shell_output);
4787 }
4788
4789 wl_list_remove(&shell->output_create_listener.link);
4790
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004791 wl_array_for_each(ws, &shell->workspaces.array)
4792 workspace_destroy(*ws);
4793 wl_array_release(&shell->workspaces.array);
4794
Pekka Paalanen3c647232011-12-22 13:43:43 +02004795 free(shell->screensaver.path);
4796 free(shell);
4797}
4798
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004799static void
4800shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4801{
4802 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004803 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004804
4805 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004806 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4807 MODIFIER_CTRL | MODIFIER_ALT,
4808 terminate_binding, ec);
4809 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4810 click_to_activate_binding,
4811 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004812 weston_compositor_add_touch_binding(ec, 0,
4813 touch_to_activate_binding,
4814 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004815 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4816 MODIFIER_SUPER | MODIFIER_ALT,
4817 surface_opacity_binding, NULL);
4818 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4819 MODIFIER_SUPER, zoom_axis_binding,
4820 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004821
4822 /* configurable bindings */
4823 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4825 zoom_key_binding, NULL);
4826 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4827 zoom_key_binding, NULL);
4828 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4829 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004830 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004831 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4832 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004833
4834 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4835 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4836 rotate_binding, NULL);
4837
Daniel Stone325fc2d2012-05-30 16:31:58 +01004838 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4839 shell);
4840 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4841 ec);
4842 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4843 backlight_binding, ec);
4844 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4845 ec);
4846 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4847 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004848 weston_compositor_add_key_binding(ec, KEY_K, mod,
4849 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004850 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4851 workspace_up_binding, shell);
4852 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4853 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004854 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4855 workspace_move_surface_up_binding,
4856 shell);
4857 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4858 workspace_move_surface_down_binding,
4859 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004860
4861 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4862 if (shell->workspaces.num > 1) {
4863 num_workspace_bindings = shell->workspaces.num;
4864 if (num_workspace_bindings > 6)
4865 num_workspace_bindings = 6;
4866 for (i = 0; i < num_workspace_bindings; i++)
4867 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4868 workspace_f_binding,
4869 shell);
4870 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004871
4872 /* Debug bindings */
4873 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4874 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004875}
4876
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004877WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004878module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004879 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004880{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004881 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004882 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004883 struct workspace **pws;
4884 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004885 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004886
Peter Huttererf3d62272013-08-08 11:57:05 +10004887 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004888 if (shell == NULL)
4889 return -1;
4890
Kristian Høgsberg75840622011-09-06 13:48:16 -04004891 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004892
4893 shell->destroy_listener.notify = shell_destroy;
4894 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004895 shell->idle_listener.notify = idle_handler;
4896 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4897 shell->wake_listener.notify = wake_handler;
4898 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004899 shell->show_input_panel_listener.notify = show_input_panels;
4900 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4901 shell->hide_input_panel_listener.notify = hide_input_panels;
4902 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004903 shell->update_input_panel_listener.notify = update_input_panels;
4904 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004905 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004906 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004907 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004908 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004909 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004910 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004911 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004912 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004913 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004914 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004915 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004916
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004917 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004918
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004919 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4920 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004921 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4922 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004923 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004924
4925 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004926 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004927
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004928 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004929
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004930 for (i = 0; i < shell->workspaces.num; i++) {
4931 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4932 if (pws == NULL)
4933 return -1;
4934
4935 *pws = workspace_create();
4936 if (*pws == NULL)
4937 return -1;
4938 }
4939 activate_workspace(shell, 0);
4940
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004941 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004942 wl_list_init(&shell->workspaces.animation.link);
4943 shell->workspaces.animation.frame = animate_workspace_change_frame;
4944
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004945 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004946 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004947 return -1;
4948
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004949 if (wl_global_create(ec->wl_display,
4950 &desktop_shell_interface, 2,
4951 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004952 return -1;
4953
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004954 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4955 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004956 return -1;
4957
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004958 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004959 shell, bind_input_panel) == NULL)
4960 return -1;
4961
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004962 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4963 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004964 return -1;
4965
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004966 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004967
Xiong Zhang6b481422013-10-23 13:58:32 +08004968 setup_output_destroy_handler(ec, shell);
4969
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004970 loop = wl_display_get_event_loop(ec->wl_display);
4971 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004972
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004973 shell->screensaver.timer =
4974 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4975
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004976 wl_list_for_each(seat, &ec->seat_list, link)
4977 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004978
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004979 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004980
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004981 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004982
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004983 return 0;
4984}