blob: fe332e14ecd5b63e20b26013d772860b71d117b1 [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øgsberg912e0a12013-10-30 08:59:55 -0700493 if (shell->startup_animation_type == ANIMATION_ZOOM)
494 shell->startup_animation_type = ANIMATION_NONE;
495
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400496 weston_config_section_get_uint(section, "num-workspaces",
497 &shell->workspaces.num,
498 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200499}
500
501static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200502focus_state_destroy(struct focus_state *state)
503{
504 wl_list_remove(&state->seat_destroy_listener.link);
505 wl_list_remove(&state->surface_destroy_listener.link);
506 free(state);
507}
508
509static void
510focus_state_seat_destroy(struct wl_listener *listener, void *data)
511{
512 struct focus_state *state = container_of(listener,
513 struct focus_state,
514 seat_destroy_listener);
515
516 wl_list_remove(&state->link);
517 focus_state_destroy(state);
518}
519
520static void
521focus_state_surface_destroy(struct wl_listener *listener, void *data)
522{
523 struct focus_state *state = container_of(listener,
524 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400525 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400526 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500527 struct weston_surface *main_surface, *next;
528 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200529
Pekka Paalanen01388e22013-04-25 13:57:44 +0300530 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
531
Kristian Høgsberge3778222012-07-31 17:29:30 -0400532 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500533 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
534 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400535 continue;
536
Jason Ekstranda7af7042013-10-12 22:38:11 -0500537 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400538 break;
539 }
540
Pekka Paalanen01388e22013-04-25 13:57:44 +0300541 /* if the focus was a sub-surface, activate its main surface */
542 if (main_surface != state->keyboard_focus)
543 next = main_surface;
544
Kristian Høgsberge3778222012-07-31 17:29:30 -0400545 if (next) {
546 shell = state->seat->compositor->shell_interface.shell;
547 activate(shell, next, state->seat);
548 } else {
549 wl_list_remove(&state->link);
550 focus_state_destroy(state);
551 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200552}
553
554static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400555focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200556{
Jonas Ådahl04769742012-06-13 00:01:24 +0200557 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200558
559 state = malloc(sizeof *state);
560 if (state == NULL)
561 return NULL;
562
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400563 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200564 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400565 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200566
567 state->seat_destroy_listener.notify = focus_state_seat_destroy;
568 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400569 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200570 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400571 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200572
573 return state;
574}
575
Jonas Ådahl8538b222012-08-29 22:13:03 +0200576static struct focus_state *
577ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
578{
579 struct workspace *ws = get_current_workspace(shell);
580 struct focus_state *state;
581
582 wl_list_for_each(state, &ws->focus_list, link)
583 if (state->seat == seat)
584 break;
585
586 if (&state->link == &ws->focus_list)
587 state = focus_state_create(seat, ws);
588
589 return state;
590}
591
Jonas Ådahl04769742012-06-13 00:01:24 +0200592static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400593restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200594{
595 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400596 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200597
598 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400599 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200600
Kristian Høgsberge3148752013-05-06 23:19:49 -0400601 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200602 }
603}
604
605static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200606replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
607 struct weston_seat *seat)
608{
609 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400610 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200611
612 wl_list_for_each(state, &ws->focus_list, link) {
613 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400614 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500615 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200616 return;
617 }
618 }
619}
620
621static void
622drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
623 struct weston_surface *surface)
624{
625 struct focus_state *state;
626
627 wl_list_for_each(state, &ws->focus_list, link)
628 if (state->keyboard_focus == surface)
629 state->keyboard_focus = NULL;
630}
631
632static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200633workspace_destroy(struct workspace *ws)
634{
Jonas Ådahl04769742012-06-13 00:01:24 +0200635 struct focus_state *state, *next;
636
637 wl_list_for_each_safe(state, next, &ws->focus_list, link)
638 focus_state_destroy(state);
639
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200640 free(ws);
641}
642
Jonas Ådahl04769742012-06-13 00:01:24 +0200643static void
644seat_destroyed(struct wl_listener *listener, void *data)
645{
646 struct weston_seat *seat = data;
647 struct focus_state *state, *next;
648 struct workspace *ws = container_of(listener,
649 struct workspace,
650 seat_destroyed_listener);
651
652 wl_list_for_each_safe(state, next, &ws->focus_list, link)
653 if (state->seat == seat)
654 wl_list_remove(&state->link);
655}
656
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200657static struct workspace *
658workspace_create(void)
659{
660 struct workspace *ws = malloc(sizeof *ws);
661 if (ws == NULL)
662 return NULL;
663
664 weston_layer_init(&ws->layer, NULL);
665
Jonas Ådahl04769742012-06-13 00:01:24 +0200666 wl_list_init(&ws->focus_list);
667 wl_list_init(&ws->seat_destroyed_listener.link);
668 ws->seat_destroyed_listener.notify = seat_destroyed;
669
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200670 return ws;
671}
672
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200673static int
674workspace_is_empty(struct workspace *ws)
675{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500676 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200677}
678
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200679static struct workspace *
680get_workspace(struct desktop_shell *shell, unsigned int index)
681{
682 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200683 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200684 pws += index;
685 return *pws;
686}
687
688static struct workspace *
689get_current_workspace(struct desktop_shell *shell)
690{
691 return get_workspace(shell, shell->workspaces.current);
692}
693
694static void
695activate_workspace(struct desktop_shell *shell, unsigned int index)
696{
697 struct workspace *ws;
698
699 ws = get_workspace(shell, index);
700 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
701
702 shell->workspaces.current = index;
703}
704
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200705static unsigned int
706get_output_height(struct weston_output *output)
707{
708 return abs(output->region.extents.y1 - output->region.extents.y2);
709}
710
711static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500712view_translate(struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200713{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500714 struct shell_surface *shsurf = get_shell_surface(view->surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200715 struct weston_transform *transform;
716
717 transform = &shsurf->workspace_transform;
718 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500719 wl_list_insert(view->geometry.transformation_list.prev,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200720 &shsurf->workspace_transform.link);
721
722 weston_matrix_init(&shsurf->workspace_transform.matrix);
723 weston_matrix_translate(&shsurf->workspace_transform.matrix,
724 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500725 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200726}
727
728static void
729workspace_translate_out(struct workspace *ws, double fraction)
730{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200732 unsigned int height;
733 double d;
734
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
736 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200737 d = height * fraction;
738
Jason Ekstranda7af7042013-10-12 22:38:11 -0500739 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200740 }
741}
742
743static void
744workspace_translate_in(struct workspace *ws, double fraction)
745{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500746 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200747 unsigned int height;
748 double d;
749
Jason Ekstranda7af7042013-10-12 22:38:11 -0500750 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
751 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200752
753 if (fraction > 0)
754 d = -(height - height * fraction);
755 else
756 d = height + height * fraction;
757
Jason Ekstranda7af7042013-10-12 22:38:11 -0500758 view_translate(view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200759 }
760}
761
762static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200763broadcast_current_workspace_state(struct desktop_shell *shell)
764{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700765 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200766
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700767 wl_resource_for_each(resource, &shell->workspaces.client_list)
768 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200769 shell->workspaces.current,
770 shell->workspaces.num);
771}
772
773static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774reverse_workspace_change_animation(struct desktop_shell *shell,
775 unsigned int index,
776 struct workspace *from,
777 struct workspace *to)
778{
779 shell->workspaces.current = index;
780
781 shell->workspaces.anim_to = to;
782 shell->workspaces.anim_from = from;
783 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
784 shell->workspaces.anim_timestamp = 0;
785
Scott Moreau4272e632012-08-13 09:58:41 -0600786 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200787}
788
789static void
790workspace_deactivate_transforms(struct workspace *ws)
791{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200793 struct shell_surface *shsurf;
794
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
796 shsurf = get_shell_surface(view->surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200797 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
798 wl_list_remove(&shsurf->workspace_transform.link);
799 wl_list_init(&shsurf->workspace_transform.link);
800 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200802 }
803}
804
805static void
806finish_workspace_change_animation(struct desktop_shell *shell,
807 struct workspace *from,
808 struct workspace *to)
809{
Scott Moreau4272e632012-08-13 09:58:41 -0600810 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200811
812 wl_list_remove(&shell->workspaces.animation.link);
813 workspace_deactivate_transforms(from);
814 workspace_deactivate_transforms(to);
815 shell->workspaces.anim_to = NULL;
816
817 wl_list_remove(&shell->workspaces.anim_from->layer.link);
818}
819
820static void
821animate_workspace_change_frame(struct weston_animation *animation,
822 struct weston_output *output, uint32_t msecs)
823{
824 struct desktop_shell *shell =
825 container_of(animation, struct desktop_shell,
826 workspaces.animation);
827 struct workspace *from = shell->workspaces.anim_from;
828 struct workspace *to = shell->workspaces.anim_to;
829 uint32_t t;
830 double x, y;
831
832 if (workspace_is_empty(from) && workspace_is_empty(to)) {
833 finish_workspace_change_animation(shell, from, to);
834 return;
835 }
836
837 if (shell->workspaces.anim_timestamp == 0) {
838 if (shell->workspaces.anim_current == 0.0)
839 shell->workspaces.anim_timestamp = msecs;
840 else
841 shell->workspaces.anim_timestamp =
842 msecs -
843 /* Invers of movement function 'y' below. */
844 (asin(1.0 - shell->workspaces.anim_current) *
845 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
846 M_2_PI);
847 }
848
849 t = msecs - shell->workspaces.anim_timestamp;
850
851 /*
852 * x = [0, π/2]
853 * y(x) = sin(x)
854 */
855 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
856 y = sin(x);
857
858 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600859 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200860
861 workspace_translate_out(from, shell->workspaces.anim_dir * y);
862 workspace_translate_in(to, shell->workspaces.anim_dir * y);
863 shell->workspaces.anim_current = y;
864
Scott Moreau4272e632012-08-13 09:58:41 -0600865 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200866 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200867 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200869}
870
871static void
872animate_workspace_change(struct desktop_shell *shell,
873 unsigned int index,
874 struct workspace *from,
875 struct workspace *to)
876{
877 struct weston_output *output;
878
879 int dir;
880
881 if (index > shell->workspaces.current)
882 dir = -1;
883 else
884 dir = 1;
885
886 shell->workspaces.current = index;
887
888 shell->workspaces.anim_dir = dir;
889 shell->workspaces.anim_from = from;
890 shell->workspaces.anim_to = to;
891 shell->workspaces.anim_current = 0.0;
892 shell->workspaces.anim_timestamp = 0;
893
894 output = container_of(shell->compositor->output_list.next,
895 struct weston_output, link);
896 wl_list_insert(&output->animation_list,
897 &shell->workspaces.animation.link);
898
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200899 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200900
901 workspace_translate_in(to, 0);
902
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400903 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200904
Scott Moreau4272e632012-08-13 09:58:41 -0600905 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200906}
907
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200908static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200909update_workspace(struct desktop_shell *shell, unsigned int index,
910 struct workspace *from, struct workspace *to)
911{
912 shell->workspaces.current = index;
913 wl_list_insert(&from->layer.link, &to->layer.link);
914 wl_list_remove(&from->layer.link);
915}
916
917static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918change_workspace(struct desktop_shell *shell, unsigned int index)
919{
920 struct workspace *from;
921 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200922
923 if (index == shell->workspaces.current)
924 return;
925
926 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200928 return;
929
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930 from = get_current_workspace(shell);
931 to = get_workspace(shell, index);
932
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200933 if (shell->workspaces.anim_from == to &&
934 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200935 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200936 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200937 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200938 return;
939 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200941 if (shell->workspaces.anim_to != NULL)
942 finish_workspace_change_animation(shell,
943 shell->workspaces.anim_from,
944 shell->workspaces.anim_to);
945
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200946 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200947
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200948 if (workspace_is_empty(to) && workspace_is_empty(from))
949 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 else
951 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200952
953 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200954}
955
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956static bool
957workspace_has_only(struct workspace *ws, struct weston_surface *surface)
958{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200960 struct wl_list *e;
961
962 if (wl_list_empty(list))
963 return false;
964
965 e = list->next;
966
967 if (e->next != list)
968 return false;
969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200971}
972
973static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974move_view_to_workspace(struct desktop_shell *shell,
975 struct weston_view *view,
976 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +0200977{
978 struct workspace *from;
979 struct workspace *to;
980 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300981 struct weston_surface *focus;
982
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 assert(weston_surface_get_main_surface(view->surface) == view->surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200984
985 if (workspace == shell->workspaces.current)
986 return;
987
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200988 if (workspace >= shell->workspaces.num)
989 workspace = shell->workspaces.num - 1;
990
Jonas Ådahle9d22502012-08-29 22:13:01 +0200991 from = get_current_workspace(shell);
992 to = get_workspace(shell, workspace);
993
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 wl_list_remove(&view->layer_link);
995 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300998 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
999 if (!seat->keyboard)
1000 continue;
1001
1002 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001004 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001005 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001006
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001008}
1009
1010static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001011take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001012 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001013 unsigned int index)
1014{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001015 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001017 struct shell_surface *shsurf;
1018 struct workspace *from;
1019 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001020 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001021
Pekka Paalanen01388e22013-04-25 13:57:44 +03001022 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 view = get_default_view(surface);
1024 if (view == NULL ||
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001025 index == shell->workspaces.current)
1026 return;
1027
1028 from = get_current_workspace(shell);
1029 to = get_workspace(shell, index);
1030
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 wl_list_remove(&view->layer_link);
1032 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001033
Jonas Ådahle9d22502012-08-29 22:13:01 +02001034 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001035 drop_focus_state(shell, from, surface);
1036
1037 if (shell->workspaces.anim_from == to &&
1038 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001039 wl_list_remove(&to->layer.link);
1040 wl_list_insert(from->layer.link.prev, &to->layer.link);
1041
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001042 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001043 broadcast_current_workspace_state(shell);
1044
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001045 return;
1046 }
1047
1048 if (shell->workspaces.anim_to != NULL)
1049 finish_workspace_change_animation(shell,
1050 shell->workspaces.anim_from,
1051 shell->workspaces.anim_to);
1052
1053 if (workspace_is_empty(from) &&
1054 workspace_has_only(to, surface))
1055 update_workspace(shell, index, from, to);
1056 else {
1057 shsurf = get_shell_surface(surface);
1058 if (wl_list_empty(&shsurf->workspace_transform.link))
1059 wl_list_insert(&shell->workspaces.anim_sticky_list,
1060 &shsurf->workspace_transform.link);
1061
1062 animate_workspace_change(shell, index, from, to);
1063 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001064
1065 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001066
1067 state = ensure_focus_state(shell, seat);
1068 if (state != NULL)
1069 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001070}
1071
1072static void
1073workspace_manager_move_surface(struct wl_client *client,
1074 struct wl_resource *resource,
1075 struct wl_resource *surface_resource,
1076 uint32_t workspace)
1077{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001078 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001079 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001080 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001081 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001083
Pekka Paalanen01388e22013-04-25 13:57:44 +03001084 main_surface = weston_surface_get_main_surface(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 view = get_default_view(main_surface);
1086 if (!view)
1087 return;
1088 move_view_to_workspace(shell, view, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001089}
1090
1091static const struct workspace_manager_interface workspace_manager_implementation = {
1092 workspace_manager_move_surface,
1093};
1094
1095static void
1096unbind_resource(struct wl_resource *resource)
1097{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001098 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001099}
1100
1101static void
1102bind_workspace_manager(struct wl_client *client,
1103 void *data, uint32_t version, uint32_t id)
1104{
1105 struct desktop_shell *shell = data;
1106 struct wl_resource *resource;
1107
Jason Ekstranda85118c2013-06-27 20:17:02 -05001108 resource = wl_resource_create(client,
1109 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001110
1111 if (resource == NULL) {
1112 weston_log("couldn't add workspace manager object");
1113 return;
1114 }
1115
Jason Ekstranda85118c2013-06-27 20:17:02 -05001116 wl_resource_set_implementation(resource,
1117 &workspace_manager_implementation,
1118 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001119 wl_list_insert(&shell->workspaces.client_list,
1120 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001121
1122 workspace_manager_send_state(resource,
1123 shell->workspaces.current,
1124 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001125}
1126
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001127static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001128touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1129 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1130{
1131}
1132
1133static void
1134touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1135{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001136 struct weston_touch_move_grab *move =
1137 (struct weston_touch_move_grab *) container_of(
1138 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001139
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001140 if (grab->touch->seat->num_tp == 0) {
1141 shell_touch_grab_end(&move->base);
1142 free(move);
1143 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001144}
1145
1146static void
1147touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1148 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1149{
1150 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1151 struct shell_surface *shsurf = move->base.shsurf;
1152 struct weston_surface *es;
1153 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1154 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1155
1156 if (!shsurf)
1157 return;
1158
1159 es = shsurf->surface;
1160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 weston_view_configure(shsurf->view, dx, dy,
1162 shsurf->view->geometry.width,
1163 shsurf->view->geometry.height);
Rusty Lynch1084da52013-08-15 09:10:08 -07001164
1165 weston_compositor_schedule_repaint(es->compositor);
1166}
1167
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001168static void
1169touch_move_grab_cancel(struct weston_touch_grab *grab)
1170{
1171 struct weston_touch_move_grab *move =
1172 (struct weston_touch_move_grab *) container_of(
1173 grab, struct shell_touch_grab, grab);
1174
1175 shell_touch_grab_end(&move->base);
1176 free(move);
1177}
1178
Rusty Lynch1084da52013-08-15 09:10:08 -07001179static const struct weston_touch_grab_interface touch_move_grab_interface = {
1180 touch_move_grab_down,
1181 touch_move_grab_up,
1182 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001183 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001184};
1185
1186static int
1187surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1188{
1189 struct weston_touch_move_grab *move;
1190
1191 if (!shsurf)
1192 return -1;
1193
1194 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1195 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001196 if (shsurf->grabbed)
1197 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001198
1199 move = malloc(sizeof *move);
1200 if (!move)
1201 return -1;
1202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001204 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001206 seat->touch->grab_y;
1207
1208 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1209 seat->touch);
1210
1211 return 0;
1212}
1213
1214static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001215noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001216{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001217}
1218
1219static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001220move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1221 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001222{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001223 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001224 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001225 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001226 int dx, dy;
1227
1228 weston_pointer_move(pointer, x, y);
1229 dx = wl_fixed_to_int(pointer->x + move->dx);
1230 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001231
1232 if (!shsurf)
1233 return;
1234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 weston_view_configure(shsurf->view, dx, dy,
1236 shsurf->view->geometry.width,
1237 shsurf->view->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001240}
1241
1242static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001243move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001244 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001245{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001246 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1247 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001248 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001249 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001250
Daniel Stone4dbadb12012-05-30 16:31:51 +01001251 if (pointer->button_count == 0 &&
1252 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001253 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001254 free(grab);
1255 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001256}
1257
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001258static void
1259move_grab_cancel(struct weston_pointer_grab *grab)
1260{
1261 struct shell_grab *shell_grab =
1262 container_of(grab, struct shell_grab, grab);
1263
1264 shell_grab_end(shell_grab);
1265 free(grab);
1266}
1267
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001268static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001269 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001270 move_grab_motion,
1271 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001272 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001273};
1274
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001275static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001277{
1278 struct weston_move_grab *move;
1279
1280 if (!shsurf)
1281 return -1;
1282
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001283 if (shsurf->grabbed)
1284 return 0;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001285 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1286 return 0;
1287
1288 move = malloc(sizeof *move);
1289 if (!move)
1290 return -1;
1291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001293 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001295 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001296
1297 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001298 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001299
1300 return 0;
1301}
1302
1303static void
1304shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1305 struct wl_resource *seat_resource, uint32_t serial)
1306{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001307 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001308 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001309 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001310
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001311 if (seat->pointer &&
1312 seat->pointer->button_count > 0 &&
1313 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001315 if ((surface == shsurf->surface) &&
1316 (surface_move(shsurf, seat) < 0))
1317 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001318 } else if (seat->touch &&
1319 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001321 if ((surface == shsurf->surface) &&
1322 (surface_touch_move(shsurf, seat) < 0))
1323 wl_resource_post_no_memory(resource);
1324 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001325}
1326
1327struct weston_resize_grab {
1328 struct shell_grab base;
1329 uint32_t edges;
1330 int32_t width, height;
1331};
1332
1333static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001334resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1335 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001336{
1337 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001338 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001339 struct shell_surface *shsurf = resize->base.shsurf;
1340 int32_t width, height;
1341 wl_fixed_t from_x, from_y;
1342 wl_fixed_t to_x, to_y;
1343
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001344 weston_pointer_move(pointer, x, y);
1345
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001346 if (!shsurf)
1347 return;
1348
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 weston_view_from_global_fixed(shsurf->view,
1350 pointer->grab_x, pointer->grab_y,
1351 &from_x, &from_y);
1352 weston_view_from_global_fixed(shsurf->view,
1353 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001354
1355 width = resize->width;
1356 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1357 width += wl_fixed_to_int(from_x - to_x);
1358 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1359 width += wl_fixed_to_int(to_x - from_x);
1360 }
1361
1362 height = resize->height;
1363 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1364 height += wl_fixed_to_int(from_y - to_y);
1365 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1366 height += wl_fixed_to_int(to_y - from_y);
1367 }
1368
1369 shsurf->client->send_configure(shsurf->surface,
1370 resize->edges, width, height);
1371}
1372
1373static void
1374send_configure(struct weston_surface *surface,
1375 uint32_t edges, int32_t width, int32_t height)
1376{
1377 struct shell_surface *shsurf = get_shell_surface(surface);
1378
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001379 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001380 edges, width, height);
1381}
1382
1383static const struct weston_shell_client shell_client = {
1384 send_configure
1385};
1386
1387static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001388resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001389 uint32_t time, uint32_t button, uint32_t state_w)
1390{
1391 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001392 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001393 enum wl_pointer_button_state state = state_w;
1394
1395 if (pointer->button_count == 0 &&
1396 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1397 shell_grab_end(&resize->base);
1398 free(grab);
1399 }
1400}
1401
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001402static void
1403resize_grab_cancel(struct weston_pointer_grab *grab)
1404{
1405 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1406
1407 shell_grab_end(&resize->base);
1408 free(grab);
1409}
1410
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001411static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001412 noop_grab_focus,
1413 resize_grab_motion,
1414 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001415 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001416};
1417
Giulio Camuffob8366642013-04-25 13:57:46 +03001418/*
1419 * Returns the bounding box of a surface and all its sub-surfaces,
1420 * in the surface coordinates system. */
1421static void
1422surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1423 int32_t *y, int32_t *w, int32_t *h) {
1424 pixman_region32_t region;
1425 pixman_box32_t *box;
1426 struct weston_subsurface *subsurface;
1427
1428 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 surface->width,
1430 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001431
1432 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1433 pixman_region32_union_rect(&region, &region,
1434 subsurface->position.x,
1435 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 subsurface->surface->width,
1437 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001438 }
1439
1440 box = pixman_region32_extents(&region);
1441 if (x)
1442 *x = box->x1;
1443 if (y)
1444 *y = box->y1;
1445 if (w)
1446 *w = box->x2 - box->x1;
1447 if (h)
1448 *h = box->y2 - box->y1;
1449
1450 pixman_region32_fini(&region);
1451}
1452
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001453static int
1454surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001455 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001456{
1457 struct weston_resize_grab *resize;
1458
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001459 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1460 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001461 return 0;
1462
1463 if (edges == 0 || edges > 15 ||
1464 (edges & 3) == 3 || (edges & 12) == 12)
1465 return 0;
1466
1467 resize = malloc(sizeof *resize);
1468 if (!resize)
1469 return -1;
1470
1471 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001472 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1473 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001474
1475 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001476 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001477
1478 return 0;
1479}
1480
1481static void
1482shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1483 struct wl_resource *seat_resource, uint32_t serial,
1484 uint32_t edges)
1485{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001486 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001487 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001488 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001489
1490 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1491 return;
1492
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001494 if (seat->pointer->button_count == 0 ||
1495 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001496 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001497 return;
1498
Kristian Høgsberge3148752013-05-06 23:19:49 -04001499 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001500 wl_resource_post_no_memory(resource);
1501}
1502
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001503static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001504end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1505
1506static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001507busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001508{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001509 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001510 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001512 wl_fixed_t sx, sy;
1513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 view = weston_compositor_pick_view(pointer->seat->compositor,
1515 pointer->x, pointer->y,
1516 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001517
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001519 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001520}
1521
1522static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001523busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1524 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001525{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001526 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001527}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001528
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001529static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001530busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001531 uint32_t time, uint32_t button, uint32_t state)
1532{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001533 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001534 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001535 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001536
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001537 if (shsurf && button == BTN_LEFT && state) {
1538 activate(shsurf->shell, shsurf->surface, seat);
1539 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001540 } else if (shsurf && button == BTN_RIGHT && state) {
1541 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001542 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001543 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001544}
1545
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001546static void
1547busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1548{
1549 struct shell_grab *grab = (struct shell_grab *) base;
1550
1551 shell_grab_end(grab);
1552 free(grab);
1553}
1554
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001555static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001556 busy_cursor_grab_focus,
1557 busy_cursor_grab_motion,
1558 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001559 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001560};
1561
1562static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001563set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001564{
1565 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001566
1567 grab = malloc(sizeof *grab);
1568 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001569 return;
1570
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001571 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1572 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001573}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001574
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001575static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001576end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001577{
1578 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1579
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001580 if (grab->grab.interface == &busy_cursor_grab_interface &&
1581 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001582 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001583 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001584 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001585}
1586
Scott Moreau9521d5e2012-04-19 13:06:17 -06001587static void
1588ping_timer_destroy(struct shell_surface *shsurf)
1589{
1590 if (!shsurf || !shsurf->ping_timer)
1591 return;
1592
1593 if (shsurf->ping_timer->source)
1594 wl_event_source_remove(shsurf->ping_timer->source);
1595
1596 free(shsurf->ping_timer);
1597 shsurf->ping_timer = NULL;
1598}
1599
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001600static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001601ping_timeout_handler(void *data)
1602{
1603 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001604 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001605
Scott Moreau9521d5e2012-04-19 13:06:17 -06001606 /* Client is not responding */
1607 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001608
1609 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 if (seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001611 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001612
1613 return 1;
1614}
1615
1616static void
1617ping_handler(struct weston_surface *surface, uint32_t serial)
1618{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001619 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001620 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001621 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001622
1623 if (!shsurf)
1624 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001625 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001626 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001627
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001628 if (shsurf->surface == shsurf->shell->grab_surface)
1629 return;
1630
Scott Moreauff1db4a2012-04-17 19:06:18 -06001631 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001632 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001633 if (!shsurf->ping_timer)
1634 return;
1635
1636 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001637 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1638 shsurf->ping_timer->source =
1639 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1640 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1641
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001642 wl_shell_surface_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001643 }
1644}
1645
1646static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001647handle_pointer_focus(struct wl_listener *listener, void *data)
1648{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001649 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001651 struct weston_compositor *compositor;
1652 struct shell_surface *shsurf;
1653 uint32_t serial;
1654
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001656 return;
1657
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658 compositor = view->surface->compositor;
1659 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001660
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001661 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001662 set_busy_cursor(shsurf, pointer);
1663 } else {
1664 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001666 }
1667}
1668
1669static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001670create_pointer_focus_listener(struct weston_seat *seat)
1671{
1672 struct wl_listener *listener;
1673
Kristian Høgsberge3148752013-05-06 23:19:49 -04001674 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001675 return;
1676
1677 listener = malloc(sizeof *listener);
1678 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001679 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001680}
1681
1682static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001683shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1684 uint32_t serial)
1685{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001686 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001687 struct weston_seat *seat;
1688 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001689
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001690 if (shsurf->ping_timer == NULL)
1691 /* Just ignore unsolicited pong. */
1692 return;
1693
Scott Moreauff1db4a2012-04-17 19:06:18 -06001694 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001695 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001696 wl_list_for_each(seat, &ec->seat_list, link) {
1697 if(seat->pointer)
1698 end_busy_cursor(shsurf, seat->pointer);
1699 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001700 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001701 }
1702}
1703
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001704static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001705set_title(struct shell_surface *shsurf, const char *title)
1706{
1707 free(shsurf->title);
1708 shsurf->title = strdup(title);
1709}
1710
1711static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001712shell_surface_set_title(struct wl_client *client,
1713 struct wl_resource *resource, const char *title)
1714{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001715 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001716
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001717 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001718}
1719
1720static void
1721shell_surface_set_class(struct wl_client *client,
1722 struct wl_resource *resource, const char *class)
1723{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001724 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001725
1726 free(shsurf->class);
1727 shsurf->class = strdup(class);
1728}
1729
Juan Zhao96879df2012-02-07 08:45:41 +08001730static struct weston_output *
1731get_default_output(struct weston_compositor *compositor)
1732{
1733 return container_of(compositor->output_list.next,
1734 struct weston_output, link);
1735}
1736
Alex Wu4539b082012-03-01 12:57:46 +08001737static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001738restore_output_mode(struct weston_output *output)
1739{
Hardening57388e42013-09-18 23:56:36 +02001740 if (output->current_mode != output->original_mode ||
1741 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001742 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001743 output->original_mode,
1744 output->original_scale,
1745 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001746}
1747
1748static void
1749restore_all_output_modes(struct weston_compositor *compositor)
1750{
1751 struct weston_output *output;
1752
1753 wl_list_for_each(output, &compositor->output_list, link)
1754 restore_output_mode(output);
1755}
1756
1757static void
Alex Wu4539b082012-03-01 12:57:46 +08001758shell_unset_fullscreen(struct shell_surface *shsurf)
1759{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001760 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001761 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001762 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1763 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001764 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08001765 }
Alex Wu4539b082012-03-01 12:57:46 +08001766 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1767 shsurf->fullscreen.framerate = 0;
1768 wl_list_remove(&shsurf->fullscreen.transform.link);
1769 wl_list_init(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 if (shsurf->fullscreen.black_view)
1771 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1772 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001773 shsurf->fullscreen_output = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 weston_view_set_position(shsurf->view,
1775 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001776 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001778 &shsurf->rotation.transform.link);
1779 shsurf->saved_rotation_valid = false;
1780 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001781
1782 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 wl_list_remove(&shsurf->view->layer_link);
1784 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001785}
1786
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001787static void
1788shell_unset_maximized(struct shell_surface *shsurf)
1789{
1790 struct workspace *ws;
1791 /* undo all maximized things here */
1792 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 weston_view_set_position(shsurf->view,
1794 shsurf->saved_x,
1795 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001796
1797 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 wl_list_insert(&shsurf->view->geometry.transformation_list,
1799 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001800 shsurf->saved_rotation_valid = false;
1801 }
1802
1803 ws = get_current_workspace(shsurf->shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 wl_list_remove(&shsurf->view->layer_link);
1805 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001806}
1807
Pekka Paalanen98262232011-12-01 10:42:22 +02001808static int
1809reset_shell_surface_type(struct shell_surface *surface)
1810{
1811 switch (surface->type) {
1812 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001813 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001814 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001815 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001816 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001817 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001818 case SHELL_SURFACE_NONE:
1819 case SHELL_SURFACE_TOPLEVEL:
1820 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001821 case SHELL_SURFACE_POPUP:
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001822 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen98262232011-12-01 10:42:22 +02001823 break;
1824 }
1825
1826 surface->type = SHELL_SURFACE_NONE;
1827 return 0;
1828}
1829
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001830static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001831set_surface_type(struct shell_surface *shsurf)
1832{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001833 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001835
1836 reset_shell_surface_type(shsurf);
1837
1838 shsurf->type = shsurf->next_type;
1839 shsurf->next_type = SHELL_SURFACE_NONE;
1840
1841 switch (shsurf->type) {
1842 case SHELL_SURFACE_TOPLEVEL:
1843 break;
1844 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845 if (pev)
1846 weston_view_set_position(shsurf->view,
1847 pev->geometry.x + shsurf->transient.x,
1848 pev->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001849 break;
1850
1851 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001852 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853 shsurf->saved_x = shsurf->view->geometry.x;
1854 shsurf->saved_y = shsurf->view->geometry.y;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001855 shsurf->saved_position_valid = true;
1856
1857 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1858 wl_list_remove(&shsurf->rotation.transform.link);
1859 wl_list_init(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 weston_view_geometry_dirty(shsurf->view);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001861 shsurf->saved_rotation_valid = true;
1862 }
1863 break;
1864
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001865 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 weston_view_set_position(shsurf->view, shsurf->transient.x,
1867 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001868 break;
1869
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001870 default:
1871 break;
1872 }
1873}
1874
1875static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001876set_toplevel(struct shell_surface *shsurf)
1877{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001878 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001879}
1880
1881static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001882shell_surface_set_toplevel(struct wl_client *client,
1883 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001884{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001885 struct shell_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001886
Tiago Vignattibc052c92012-04-19 16:18:18 +03001887 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001888}
1889
1890static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001891set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001892 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001893{
1894 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001895 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001896 shsurf->transient.x = x;
1897 shsurf->transient.y = y;
1898 shsurf->transient.flags = flags;
1899 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1900}
1901
1902static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001903shell_surface_set_transient(struct wl_client *client,
1904 struct wl_resource *resource,
1905 struct wl_resource *parent_resource,
1906 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001907{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001908 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001909 struct weston_surface *parent =
1910 wl_resource_get_user_data(parent_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02001911
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001912 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001913}
1914
Tiago Vignattibe143262012-04-16 17:31:41 +03001915static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001916shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001917{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001918 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001919}
1920
1921static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001922get_output_panel_height(struct desktop_shell *shell,
1923 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001924{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 struct weston_view *view;
Juan Zhao96879df2012-02-07 08:45:41 +08001926 int panel_height = 0;
1927
1928 if (!output)
1929 return 0;
1930
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1932 if (view->surface->output == output) {
1933 panel_height = view->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001934 break;
1935 }
1936 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001937
Juan Zhao96879df2012-02-07 08:45:41 +08001938 return panel_height;
1939}
1940
1941static void
1942shell_surface_set_maximized(struct wl_client *client,
1943 struct wl_resource *resource,
1944 struct wl_resource *output_resource )
1945{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001946 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Juan Zhao96879df2012-02-07 08:45:41 +08001947 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001948 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001949 uint32_t edges = 0, panel_height = 0;
1950
1951 /* get the default output, if the client set it as NULL
1952 check whether the ouput is available */
1953 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001954 shsurf->output = wl_resource_get_user_data(output_resource);
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001955 else if (es->output)
1956 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001957 else
1958 shsurf->output = get_default_output(es->compositor);
1959
Tiago Vignattibe143262012-04-16 17:31:41 +03001960 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001961 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001962 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001963
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001964 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001965 shsurf->output->width,
1966 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001967
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001968 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001969}
1970
Alex Wu21858432012-04-01 20:13:08 +08001971static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001972black_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 +08001973
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08001975create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001976 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001977 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001978{
1979 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08001981
1982 surface = weston_surface_create(ec);
1983 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001984 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001985 return NULL;
1986 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987 view = weston_view_create(surface);
1988 if (surface == NULL) {
1989 weston_log("no memory\n");
1990 weston_surface_destroy(surface);
1991 return NULL;
1992 }
Alex Wu4539b082012-03-01 12:57:46 +08001993
Alex Wu21858432012-04-01 20:13:08 +08001994 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001995 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001996 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001997 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001998 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001999 pixman_region32_fini(&surface->input);
2000 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002001
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 weston_view_configure(view, x, y, w, h);
2003
2004 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002005}
2006
2007/* Create black surface and append it to the associated fullscreen surface.
2008 * Handle size dismatch and positioning according to the method. */
2009static void
2010shell_configure_fullscreen(struct shell_surface *shsurf)
2011{
2012 struct weston_output *output = shsurf->fullscreen_output;
2013 struct weston_surface *surface = shsurf->surface;
2014 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002015 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002016 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002017
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002018 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2019 restore_output_mode(output);
2020
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 if (!shsurf->fullscreen.black_view)
2022 shsurf->fullscreen.black_view =
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002023 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08002024 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002025 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002026 output->width,
2027 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002028
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2030 wl_list_insert(&shsurf->view->layer_link,
2031 &shsurf->fullscreen.black_view->layer_link);
2032 shsurf->fullscreen.black_view->surface->output = output;
2033 shsurf->fullscreen.black_view->output = output;
Alex Wu4539b082012-03-01 12:57:46 +08002034
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002036 &surf_width, &surf_height);
2037
Alex Wu4539b082012-03-01 12:57:46 +08002038 switch (shsurf->fullscreen.type) {
2039 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002040 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002042 break;
2043 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002044 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002045 if (output->width == surf_width &&
2046 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 weston_view_set_position(shsurf->view,
2048 output->x - surf_x,
2049 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002050 break;
2051 }
2052
Alex Wu4539b082012-03-01 12:57:46 +08002053 matrix = &shsurf->fullscreen.transform.matrix;
2054 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002055
Scott Moreau1bad5db2012-08-18 01:04:05 -06002056 output_aspect = (float) output->width /
2057 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 /* XXX: Use surf_width and surf_height here? */
2059 surface_aspect = (float) surface->width /
2060 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002061 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002062 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002063 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002064 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002065 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002066 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002067
Alex Wu4539b082012-03-01 12:57:46 +08002068 weston_matrix_scale(matrix, scale, scale, 1);
2069 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002071 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002072 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2073 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002075
Alex Wu4539b082012-03-01 12:57:46 +08002076 break;
2077 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002078 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002079 struct weston_mode mode = {0,
Alexander Larsson355748e2013-05-28 16:23:38 +02002080 surf_width * surface->buffer_scale,
2081 surf_height * surface->buffer_scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002082 shsurf->fullscreen.framerate};
2083
Hardening57388e42013-09-18 23:56:36 +02002084 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
2085 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 weston_view_set_position(shsurf->view,
2087 output->x - surf_x,
2088 output->y - surf_y);
2089 weston_view_configure(shsurf->fullscreen.black_view,
2090 output->x - surf_x,
2091 output->y - surf_y,
2092 output->width,
2093 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002094 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002095 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002096 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002098 }
Alex Wubd3354b2012-04-17 17:20:49 +08002099 }
Alex Wu4539b082012-03-01 12:57:46 +08002100 break;
2101 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002103 break;
2104 default:
2105 break;
2106 }
2107}
2108
2109/* make the fullscreen and black surface at the top */
2110static void
2111shell_stack_fullscreen(struct shell_surface *shsurf)
2112{
Alex Wubd3354b2012-04-17 17:20:49 +08002113 struct weston_output *output = shsurf->fullscreen_output;
Tiago Vignattibe143262012-04-16 17:31:41 +03002114 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002115
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 wl_list_remove(&shsurf->view->layer_link);
2117 wl_list_insert(&shell->fullscreen_layer.view_list,
2118 &shsurf->view->layer_link);
2119 weston_surface_damage(shsurf->surface);
Alex Wubd3354b2012-04-17 17:20:49 +08002120
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (!shsurf->fullscreen.black_view)
2122 shsurf->fullscreen.black_view =
2123 create_black_surface(shsurf->surface->compositor,
2124 shsurf->surface,
Alex Wubd3354b2012-04-17 17:20:49 +08002125 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002126 output->width,
2127 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08002128
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2130 wl_list_insert(&shsurf->view->layer_link,
2131 &shsurf->fullscreen.black_view->layer_link);
2132 weston_surface_damage(shsurf->fullscreen.black_view->surface);
Alex Wu4539b082012-03-01 12:57:46 +08002133}
2134
2135static void
2136shell_map_fullscreen(struct shell_surface *shsurf)
2137{
Alex Wu4539b082012-03-01 12:57:46 +08002138 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08002139 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002140}
2141
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002142static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002143set_fullscreen(struct shell_surface *shsurf,
2144 uint32_t method,
2145 uint32_t framerate,
2146 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002147{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002148 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08002149
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002150 if (output)
2151 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04002152 else if (es->output)
2153 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08002154 else
2155 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002156
Alex Wu4539b082012-03-01 12:57:46 +08002157 shsurf->fullscreen_output = shsurf->output;
2158 shsurf->fullscreen.type = method;
2159 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002160 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08002161
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002162 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002163 shsurf->output->width,
2164 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002165}
2166
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002167static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002168shell_surface_set_fullscreen(struct wl_client *client,
2169 struct wl_resource *resource,
2170 uint32_t method,
2171 uint32_t framerate,
2172 struct wl_resource *output_resource)
2173{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002174 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002175 struct weston_output *output;
2176
2177 if (output_resource)
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002178 output = wl_resource_get_user_data(output_resource);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002179 else
2180 output = NULL;
2181
2182 set_fullscreen(shsurf, method, framerate, output);
2183}
2184
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002185static void
2186set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2187{
2188 /* XXX: using the same fields for transient type */
2189 shsurf->transient.x = x;
2190 shsurf->transient.y = y;
2191 shsurf->transient.flags = flags;
2192 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2193}
2194
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002195static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002196
2197static void
2198destroy_shell_seat(struct wl_listener *listener, void *data)
2199{
2200 struct shell_seat *shseat =
2201 container_of(listener,
2202 struct shell_seat, seat_destroy_listener);
2203 struct shell_surface *shsurf, *prev = NULL;
2204
2205 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002206 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002207 shseat->popup_grab.client = NULL;
2208
2209 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2210 shsurf->popup.shseat = NULL;
2211 if (prev) {
2212 wl_list_init(&prev->popup.grab_link);
2213 }
2214 prev = shsurf;
2215 }
2216 wl_list_init(&prev->popup.grab_link);
2217 }
2218
2219 wl_list_remove(&shseat->seat_destroy_listener.link);
2220 free(shseat);
2221}
2222
2223static struct shell_seat *
2224create_shell_seat(struct weston_seat *seat)
2225{
2226 struct shell_seat *shseat;
2227
2228 shseat = calloc(1, sizeof *shseat);
2229 if (!shseat) {
2230 weston_log("no memory to allocate shell seat\n");
2231 return NULL;
2232 }
2233
2234 shseat->seat = seat;
2235 wl_list_init(&shseat->popup_grab.surfaces_list);
2236
2237 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2238 wl_signal_add(&seat->destroy_signal,
2239 &shseat->seat_destroy_listener);
2240
2241 return shseat;
2242}
2243
2244static struct shell_seat *
2245get_shell_seat(struct weston_seat *seat)
2246{
2247 struct wl_listener *listener;
2248
2249 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2250 if (listener == NULL)
2251 return create_shell_seat(seat);
2252
2253 return container_of(listener,
2254 struct shell_seat, seat_destroy_listener);
2255}
2256
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002257static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002258popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002259{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002260 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002261 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002262 struct shell_seat *shseat =
2263 container_of(grab, struct shell_seat, popup_grab.grab);
2264 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002265 wl_fixed_t sx, sy;
2266
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 view = weston_compositor_pick_view(pointer->seat->compositor,
2268 pointer->x, pointer->y,
2269 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002270
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271 if (view && view->surface->resource &&
2272 wl_resource_get_client(view->surface->resource) == client) {
2273 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002274 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002275 weston_pointer_set_focus(pointer, NULL,
2276 wl_fixed_from_int(0),
2277 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002278 }
2279}
2280
2281static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002282popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2283 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002284{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002285 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002286 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002287 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002288
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002289 weston_pointer_move(pointer, x, y);
2290
Neil Roberts96d790e2013-09-19 17:32:00 +01002291 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 weston_view_from_global_fixed(pointer->focus,
2293 pointer->x, pointer->y,
2294 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002295 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002296 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002297}
2298
2299static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002300popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002301 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002302{
2303 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002304 struct shell_seat *shseat =
2305 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002306 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002307 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002308 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002309 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002310
Neil Roberts96d790e2013-09-19 17:32:00 +01002311 resource_list = &grab->pointer->focus_resource_list;
2312 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002313 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002314 wl_resource_for_each(resource, resource_list) {
2315 wl_pointer_send_button(resource, serial,
2316 time, button, state);
2317 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002318 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002319 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002320 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002321 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002322 }
2323
Daniel Stone4dbadb12012-05-30 16:31:51 +01002324 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002325 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002326}
2327
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002328static void
2329popup_grab_cancel(struct weston_pointer_grab *grab)
2330{
2331 popup_grab_end(grab->pointer);
2332}
2333
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002334static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002335 popup_grab_focus,
2336 popup_grab_motion,
2337 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002338 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002339};
2340
2341static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002342popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002343{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002344 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002345 struct shell_seat *shseat =
2346 container_of(grab, struct shell_seat, popup_grab.grab);
2347 struct shell_surface *shsurf;
2348 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002349
2350 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002351 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002352 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002353 shseat->popup_grab.grab.interface = NULL;
2354 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002355 /* Send the popup_done event to all the popups open */
2356 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002357 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002358 shsurf->popup.shseat = NULL;
2359 if (prev) {
2360 wl_list_init(&prev->popup.grab_link);
2361 }
2362 prev = shsurf;
2363 }
2364 wl_list_init(&prev->popup.grab_link);
2365 wl_list_init(&shseat->popup_grab.surfaces_list);
2366 }
2367}
2368
2369static void
2370add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2371{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002372 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002373
2374 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002375 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002376 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002377 /* We must make sure here that this popup was opened after
2378 * a mouse press, and not just by moving around with other
2379 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002380 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002381 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002382
Rob Bradforddfe31052013-06-26 19:49:11 +01002383 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002384 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002385 } else {
2386 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002387 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002388}
2389
2390static void
2391remove_popup_grab(struct shell_surface *shsurf)
2392{
2393 struct shell_seat *shseat = shsurf->popup.shseat;
2394
2395 wl_list_remove(&shsurf->popup.grab_link);
2396 wl_list_init(&shsurf->popup.grab_link);
2397 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002398 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002399 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002400 }
2401}
2402
2403static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002404shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002405{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002406 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002407 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002408
Jason Ekstranda7af7042013-10-12 22:38:11 -05002409 shsurf->surface->output = parent_view->output;
2410 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002411
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 weston_view_set_transform_parent(shsurf->view, parent_view);
2413 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2414 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002415
Kristian Høgsberge3148752013-05-06 23:19:49 -04002416 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002417 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002418 } else {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002419 wl_shell_surface_send_popup_done(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002420 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002421 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002422}
2423
2424static void
2425shell_surface_set_popup(struct wl_client *client,
2426 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002427 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002428 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002429 struct wl_resource *parent_resource,
2430 int32_t x, int32_t y, uint32_t flags)
2431{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002432 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002433
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002434 shsurf->type = SHELL_SURFACE_POPUP;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002435 shsurf->parent = wl_resource_get_user_data(parent_resource);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002436 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002437 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002438 shsurf->popup.x = x;
2439 shsurf->popup.y = y;
2440}
2441
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002442static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002443 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002444 shell_surface_move,
2445 shell_surface_resize,
2446 shell_surface_set_toplevel,
2447 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002448 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002449 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002450 shell_surface_set_maximized,
2451 shell_surface_set_title,
2452 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002453};
2454
2455static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002456destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002457{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002458 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2459
Giulio Camuffo5085a752013-03-25 21:42:45 +01002460 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2461 remove_popup_grab(shsurf);
2462 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002463
Alex Wubd3354b2012-04-17 17:20:49 +08002464 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002465 shell_surface_is_top_fullscreen(shsurf))
2466 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002467
Jason Ekstranda7af7042013-10-12 22:38:11 -05002468 if (shsurf->fullscreen.black_view)
2469 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002470
Alex Wubd3354b2012-04-17 17:20:49 +08002471 /* As destroy_resource() use wl_list_for_each_safe(),
2472 * we can always remove the listener.
2473 */
2474 wl_list_remove(&shsurf->surface_destroy_listener.link);
2475 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002476 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002477 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002478
Jason Ekstranda7af7042013-10-12 22:38:11 -05002479 weston_view_destroy(shsurf->view);
2480
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002481 wl_list_remove(&shsurf->link);
2482 free(shsurf);
2483}
2484
2485static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002486shell_destroy_shell_surface(struct wl_resource *resource)
2487{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002488 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002489
2490 destroy_shell_surface(shsurf);
2491}
2492
2493static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002494shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002495{
2496 struct shell_surface *shsurf = container_of(listener,
2497 struct shell_surface,
2498 surface_destroy_listener);
2499
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002500 if (shsurf->resource)
2501 wl_resource_destroy(shsurf->resource);
2502 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002503 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002504}
2505
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002506static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002507shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002508
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002509static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002510get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002511{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002512 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002513 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002514 else
2515 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002516}
2517
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002518static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002519create_shell_surface(void *shell, struct weston_surface *surface,
2520 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002521{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002522 struct shell_surface *shsurf;
2523
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002524 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002525 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002526 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002527 }
2528
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002529 shsurf = calloc(1, sizeof *shsurf);
2530 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002531 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002532 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002533 }
2534
Jason Ekstranda7af7042013-10-12 22:38:11 -05002535 shsurf->view = weston_view_create(surface);
2536 if (!shsurf->view) {
2537 weston_log("no memory to allocate shell surface\n");
2538 free(shsurf);
2539 return NULL;
2540 }
2541
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002542 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002543 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002544
Tiago Vignattibc052c92012-04-19 16:18:18 +03002545 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002546 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002547 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002548 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002549 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002550 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2551 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002553 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002554 wl_list_init(&shsurf->fullscreen.transform.link);
2555
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002556 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002557 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002558 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002559 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002560
2561 /* init link so its safe to always remove it in destroy_shell_surface */
2562 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002563 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002564
Pekka Paalanen460099f2012-01-20 16:48:25 +02002565 /* empty when not in use */
2566 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002567 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002568
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002569 wl_list_init(&shsurf->workspace_transform.link);
2570
Pekka Paalanen98262232011-12-01 10:42:22 +02002571 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002572 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002573
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002574 shsurf->client = client;
2575
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002576 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002577}
2578
Jason Ekstranda7af7042013-10-12 22:38:11 -05002579static struct weston_view *
2580get_primary_view(void *shell, struct shell_surface *shsurf)
2581{
2582 return shsurf->view;
2583}
2584
Tiago Vignattibc052c92012-04-19 16:18:18 +03002585static void
2586shell_get_shell_surface(struct wl_client *client,
2587 struct wl_resource *resource,
2588 uint32_t id,
2589 struct wl_resource *surface_resource)
2590{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002591 struct weston_surface *surface =
2592 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002593 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002594 struct shell_surface *shsurf;
2595
2596 if (get_shell_surface(surface)) {
2597 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002598 WL_DISPLAY_ERROR_INVALID_OBJECT,
2599 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002600 return;
2601 }
2602
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002603 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002604 if (!shsurf) {
2605 wl_resource_post_error(surface_resource,
2606 WL_DISPLAY_ERROR_INVALID_OBJECT,
2607 "surface->configure already set");
2608 return;
2609 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002610
Jason Ekstranda85118c2013-06-27 20:17:02 -05002611 shsurf->resource =
2612 wl_resource_create(client,
2613 &wl_shell_surface_interface, 1, id);
2614 wl_resource_set_implementation(shsurf->resource,
2615 &shell_surface_implementation,
2616 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002617}
2618
2619static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002620 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002621};
2622
Kristian Høgsberg07937562011-04-12 17:25:42 -04002623static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002624shell_fade(struct desktop_shell *shell, enum fade_type type);
2625
2626static int
2627screensaver_timeout(void *data)
2628{
2629 struct desktop_shell *shell = data;
2630
2631 shell_fade(shell, FADE_OUT);
2632
2633 return 1;
2634}
2635
2636static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002637handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002638{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002639 struct desktop_shell *shell =
2640 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002641
Pekka Paalanen18027e52011-12-02 16:31:49 +02002642 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002643
2644 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002645 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002646}
2647
2648static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002649launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002650{
2651 if (shell->screensaver.binding)
2652 return;
2653
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002654 if (!shell->screensaver.path) {
2655 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002656 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002657 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002658
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002659 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002660 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002661 return;
2662 }
2663
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002664 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002665 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002666 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002667 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002668}
2669
2670static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002671terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002672{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002673 if (shell->screensaver.process.pid == 0)
2674 return;
2675
2676 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002677}
2678
2679static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680configure_static_view(struct weston_view *ev, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002681{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002682 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002683
Giulio Camuffo184df502013-02-21 11:29:21 +01002684 if (width == 0)
2685 return;
2686
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
2688 if (v->output == ev->output && v != ev) {
2689 weston_view_unmap(v);
2690 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002691 }
2692 }
2693
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 weston_view_configure(ev, ev->output->x, ev->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002695
Jason Ekstranda7af7042013-10-12 22:38:11 -05002696 if (wl_list_empty(&ev->layer_link)) {
2697 wl_list_insert(&layer->view_list, &ev->layer_link);
2698 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002699 }
2700}
2701
2702static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002703background_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 -04002704{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002705 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002707
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 view = container_of(es->views.next, struct weston_view, surface_link);
2709
2710 configure_static_view(view, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002711}
2712
2713static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002714desktop_shell_set_background(struct wl_client *client,
2715 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002716 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002717 struct wl_resource *surface_resource)
2718{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002719 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002720 struct weston_surface *surface =
2721 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002723
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002724 if (surface->configure) {
2725 wl_resource_post_error(surface_resource,
2726 WL_DISPLAY_ERROR_INVALID_OBJECT,
2727 "surface role already assigned");
2728 return;
2729 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002730
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2732 weston_view_destroy(view);
2733 view = weston_view_create(surface);
2734
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002735 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002736 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002737 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002739 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002740 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002741 surface->output->width,
2742 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002743}
2744
2745static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002746panel_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 -04002747{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002748 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002749 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002750
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 view = container_of(es->views.next, struct weston_view, surface_link);
2752
2753 configure_static_view(view, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002754}
2755
2756static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002757desktop_shell_set_panel(struct wl_client *client,
2758 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002759 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002760 struct wl_resource *surface_resource)
2761{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002762 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002763 struct weston_surface *surface =
2764 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002765 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002766
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002767 if (surface->configure) {
2768 wl_resource_post_error(surface_resource,
2769 WL_DISPLAY_ERROR_INVALID_OBJECT,
2770 "surface role already assigned");
2771 return;
2772 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002773
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2775 weston_view_destroy(view);
2776 view = weston_view_create(surface);
2777
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002778 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002779 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002780 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002782 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002783 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002784 surface->output->width,
2785 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002786}
2787
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002788static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002789lock_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 -04002790{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002791 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002792 struct weston_view *view;
2793
2794 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002795
Giulio Camuffo184df502013-02-21 11:29:21 +01002796 if (width == 0)
2797 return;
2798
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 surface->width = width;
2800 surface->height = height;
2801 view->geometry.width = width;
2802 view->geometry.height = height;
2803 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002804
2805 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002806 wl_list_insert(&shell->lock_layer.view_list,
2807 &view->layer_link);
2808 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002809 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002810 }
2811}
2812
2813static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002814handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002815{
Tiago Vignattibe143262012-04-16 17:31:41 +03002816 struct desktop_shell *shell =
2817 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002818
Martin Minarik6d118362012-06-07 18:01:59 +02002819 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002820 shell->lock_surface = NULL;
2821}
2822
2823static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002824desktop_shell_set_lock_surface(struct wl_client *client,
2825 struct wl_resource *resource,
2826 struct wl_resource *surface_resource)
2827{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002828 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002829 struct weston_surface *surface =
2830 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02002831
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002832 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002833
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002834 if (!shell->locked)
2835 return;
2836
Pekka Paalanen98262232011-12-01 10:42:22 +02002837 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002838
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002839 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002840 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002841 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002842
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07002843 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002844 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002845 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002846}
2847
2848static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002849resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002850{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002851 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002852
Pekka Paalanen77346a62011-11-30 16:26:35 +02002853 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002854
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002855 wl_list_remove(&shell->lock_layer.link);
2856 wl_list_insert(&shell->compositor->cursor_layer.link,
2857 &shell->fullscreen_layer.link);
2858 wl_list_insert(&shell->fullscreen_layer.link,
2859 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002860 if (shell->showing_input_panels) {
2861 wl_list_insert(&shell->panel_layer.link,
2862 &shell->input_panel_layer.link);
2863 wl_list_insert(&shell->input_panel_layer.link,
2864 &ws->layer.link);
2865 } else {
2866 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2867 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002868
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002869 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002870
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002871 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002872 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002873 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002874}
2875
2876static void
2877desktop_shell_unlock(struct wl_client *client,
2878 struct wl_resource *resource)
2879{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002880 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002881
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002882 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002883
2884 if (shell->locked)
2885 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002886}
2887
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002888static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002889desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002890 struct wl_resource *resource,
2891 struct wl_resource *surface_resource)
2892{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002893 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002894
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002895 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07002896 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002897}
2898
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002899static void
2900desktop_shell_desktop_ready(struct wl_client *client,
2901 struct wl_resource *resource)
2902{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002903 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002904
2905 shell_fade_startup(shell);
2906}
2907
Kristian Høgsberg75840622011-09-06 13:48:16 -04002908static const struct desktop_shell_interface desktop_shell_implementation = {
2909 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002910 desktop_shell_set_panel,
2911 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002912 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03002913 desktop_shell_set_grab_surface,
2914 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04002915};
2916
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002917static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002918get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002919{
2920 struct shell_surface *shsurf;
2921
2922 shsurf = get_shell_surface(surface);
2923 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002924 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002925 return shsurf->type;
2926}
2927
Kristian Høgsberg75840622011-09-06 13:48:16 -04002928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002929move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002930{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002931 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002932 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002933 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002934
Pekka Paalanen01388e22013-04-25 13:57:44 +03002935 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002936 if (surface == NULL)
2937 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002938
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002939 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002940 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2941 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002942 return;
2943
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002944 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002945}
2946
2947static void
Neil Robertsaba0f252013-10-03 16:43:05 +01002948touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2949{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002950 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01002951 struct weston_surface *surface;
2952 struct shell_surface *shsurf;
2953
2954 surface = weston_surface_get_main_surface(focus);
2955 if (surface == NULL)
2956 return;
2957
2958 shsurf = get_shell_surface(surface);
2959 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2960 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2961 return;
2962
2963 surface_touch_move(shsurf, (struct weston_seat *) seat);
2964}
2965
2966static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002967resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002968{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07002969 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002970 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002971 uint32_t edges = 0;
2972 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002973 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002974
Pekka Paalanen01388e22013-04-25 13:57:44 +03002975 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002976 if (surface == NULL)
2977 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002978
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002979 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002980 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2981 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002982 return;
2983
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 weston_view_from_global(shsurf->view,
2985 wl_fixed_to_int(seat->pointer->grab_x),
2986 wl_fixed_to_int(seat->pointer->grab_y),
2987 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002988
Jason Ekstranda7af7042013-10-12 22:38:11 -05002989 if (x < shsurf->view->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002990 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 else if (x < 2 * shsurf->view->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002992 edges |= 0;
2993 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002994 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002995
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 if (y < shsurf->view->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002997 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002998 else if (y < 2 * shsurf->view->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002999 edges |= 0;
3000 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003001 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003002
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003003 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003004}
3005
3006static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003007surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003008 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003009{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003010 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003011 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003012 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003013 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003014
Pekka Paalanen01388e22013-04-25 13:57:44 +03003015 /* XXX: broken for windows containing sub-surfaces */
3016 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003017 if (surface == NULL)
3018 return;
3019
3020 shsurf = get_shell_surface(surface);
3021 if (!shsurf)
3022 return;
3023
Jason Ekstranda7af7042013-10-12 22:38:11 -05003024 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003025
Jason Ekstranda7af7042013-10-12 22:38:11 -05003026 if (shsurf->view->alpha > 1.0)
3027 shsurf->view->alpha = 1.0;
3028 if (shsurf->view->alpha < step)
3029 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003030
Jason Ekstranda7af7042013-10-12 22:38:11 -05003031 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003032 weston_surface_damage(surface);
3033}
3034
3035static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003036do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003037 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003038{
Daniel Stone37816df2012-05-16 18:45:18 +01003039 struct weston_seat *ws = (struct weston_seat *) seat;
3040 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003041 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003042 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003043
3044 wl_list_for_each(output, &compositor->output_list, link) {
3045 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003046 wl_fixed_to_double(seat->pointer->x),
3047 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003048 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003049 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003050 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003051 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003052 increment = -output->zoom.increment;
3053 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003054 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003055 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003056 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003057 else
3058 increment = 0;
3059
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003060 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003061
Scott Moreaue6603982012-06-11 13:07:51 -06003062 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003063 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003064 else if (output->zoom.level > output->zoom.max_level)
3065 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003066 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003067 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003068 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003069
Scott Moreaue6603982012-06-11 13:07:51 -06003070 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003071
Jason Ekstranda7af7042013-10-12 22:38:11 -05003072 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003073 }
3074 }
3075}
3076
Scott Moreauccbf29d2012-02-22 14:21:41 -07003077static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003078zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003079 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003080{
3081 do_zoom(seat, time, 0, axis, value);
3082}
3083
3084static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003085zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003086 void *data)
3087{
3088 do_zoom(seat, time, key, 0, 0);
3089}
3090
3091static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003092terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003093 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003094{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003095 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003096
Daniel Stone325fc2d2012-05-30 16:31:58 +01003097 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003098}
3099
3100static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003101rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3102 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003103{
3104 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003105 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003106 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003107 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003108 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003109
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003110 weston_pointer_move(pointer, x, y);
3111
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003112 if (!shsurf)
3113 return;
3114
Jason Ekstranda7af7042013-10-12 22:38:11 -05003115 cx = 0.5f * shsurf->view->geometry.width;
3116 cy = 0.5f * shsurf->view->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003117
Daniel Stone37816df2012-05-16 18:45:18 +01003118 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3119 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003120 r = sqrtf(dx * dx + dy * dy);
3121
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003122 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003123 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003124
3125 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003126 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003127 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003128
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003129 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003130 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003131
3132 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003133 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003134 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003135 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003136 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003137
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003138 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003139 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003140 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003141 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003142 wl_list_init(&shsurf->rotation.transform.link);
3143 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003144 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003145 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003146
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003147 /* We need to adjust the position of the surface
3148 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003149 cposx = shsurf->view->geometry.x + cx;
3150 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003151 dposx = rotate->center.x - cposx;
3152 dposy = rotate->center.y - cposy;
3153 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 weston_view_set_position(shsurf->view,
3155 shsurf->view->geometry.x + dposx,
3156 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003157 }
3158
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003159 /* Repaint implies weston_surface_update_transform(), which
3160 * lazily applies the damage due to rotation update.
3161 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003162 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003163}
3164
3165static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003166rotate_grab_button(struct weston_pointer_grab *grab,
3167 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003168{
3169 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003170 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003171 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003172 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003173 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003174
Daniel Stone4dbadb12012-05-30 16:31:51 +01003175 if (pointer->button_count == 0 &&
3176 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003177 if (shsurf)
3178 weston_matrix_multiply(&shsurf->rotation.rotation,
3179 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003180 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003181 free(rotate);
3182 }
3183}
3184
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003185static void
3186rotate_grab_cancel(struct weston_pointer_grab *grab)
3187{
3188 struct rotate_grab *rotate =
3189 container_of(grab, struct rotate_grab, base.grab);
3190
3191 shell_grab_end(&rotate->base);
3192 free(rotate);
3193}
3194
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003195static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003196 noop_grab_focus,
3197 rotate_grab_motion,
3198 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003199 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003200};
3201
3202static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003203surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003204{
Pekka Paalanen460099f2012-01-20 16:48:25 +02003205 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003206 float dx, dy;
3207 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003208
Pekka Paalanen460099f2012-01-20 16:48:25 +02003209 rotate = malloc(sizeof *rotate);
3210 if (!rotate)
3211 return;
3212
Jason Ekstranda7af7042013-10-12 22:38:11 -05003213 weston_view_to_global_float(surface->view,
3214 surface->view->geometry.width * 0.5f,
3215 surface->view->geometry.height * 0.5f,
3216 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003217
Daniel Stone37816df2012-05-16 18:45:18 +01003218 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3219 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003220 r = sqrtf(dx * dx + dy * dy);
3221 if (r > 20.0f) {
3222 struct weston_matrix inverse;
3223
3224 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003225 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003226 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003227
3228 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003229 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003230 } else {
3231 weston_matrix_init(&surface->rotation.rotation);
3232 weston_matrix_init(&rotate->rotation);
3233 }
3234
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003235 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3236 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003237}
3238
3239static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003240rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003241 void *data)
3242{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003243 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003244 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003245 struct shell_surface *surface;
3246
Pekka Paalanen01388e22013-04-25 13:57:44 +03003247 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003248 if (base_surface == NULL)
3249 return;
3250
3251 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01003252 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3253 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003254 return;
3255
3256 surface_rotate(surface, seat);
3257}
3258
3259static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003260lower_fullscreen_layer(struct desktop_shell *shell)
3261{
3262 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003263 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003264
3265 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003266 wl_list_for_each_reverse_safe(view, prev,
3267 &shell->fullscreen_layer.view_list,
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003268 layer_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003269 weston_view_restack(view, &ws->layer.view_list);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003270}
3271
3272static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003273activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01003274 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003275{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003276 struct weston_surface *main_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003277 struct weston_view *main_view;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003278 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003279 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003280
Pekka Paalanen01388e22013-04-25 13:57:44 +03003281 main_surface = weston_surface_get_main_surface(es);
3282
Daniel Stone37816df2012-05-16 18:45:18 +01003283 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003284
Jonas Ådahl8538b222012-08-29 22:13:03 +02003285 state = ensure_focus_state(shell, seat);
3286 if (state == NULL)
3287 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003288
3289 state->keyboard_focus = es;
3290 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003291 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003292
Pekka Paalanen01388e22013-04-25 13:57:44 +03003293 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08003294 case SHELL_SURFACE_FULLSCREEN:
3295 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03003296 shell_stack_fullscreen(get_shell_surface(main_surface));
3297 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08003298 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003299 default:
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003300 restore_all_output_modes(shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003301 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003302 main_view = get_default_view(main_surface);
3303 if (main_view)
3304 weston_view_restack(main_view, &ws->layer.view_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003305 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003306 }
3307}
3308
Alex Wu21858432012-04-01 20:13:08 +08003309/* no-op func for checking black surface */
3310static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003311black_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 +08003312{
3313}
3314
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003315static bool
Alex Wu21858432012-04-01 20:13:08 +08003316is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3317{
3318 if (es->configure == black_surface_configure) {
3319 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003320 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08003321 return true;
3322 }
3323 return false;
3324}
3325
Kristian Høgsberg75840622011-09-06 13:48:16 -04003326static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003327activate_binding(struct weston_seat *seat,
3328 struct desktop_shell *shell,
3329 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003330{
Pekka Paalanen01388e22013-04-25 13:57:44 +03003331 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003332
Alex Wu9c35e6b2012-03-05 14:13:13 +08003333 if (!focus)
3334 return;
3335
Pekka Paalanen01388e22013-04-25 13:57:44 +03003336 if (is_black_surface(focus, &main_surface))
3337 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08003338
Pekka Paalanen01388e22013-04-25 13:57:44 +03003339 main_surface = weston_surface_get_main_surface(focus);
3340 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003341 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003342
Neil Robertsa28c6932013-10-03 16:43:04 +01003343 activate(shell, focus, seat);
3344}
3345
3346static void
3347click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3348 void *data)
3349{
3350 if (seat->pointer->grab != &seat->pointer->default_grab)
3351 return;
3352
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003353 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01003354}
3355
3356static void
3357touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3358{
3359 if (seat->touch->grab != &seat->touch->default_grab)
3360 return;
3361
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003362 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003363}
3364
3365static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003366lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003367{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003368 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003369
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003370 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003371 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003372 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003373 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003374
3375 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003376
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003377 /* Hide all surfaces by removing the fullscreen, panel and
3378 * toplevel layers. This way nothing else can show or receive
3379 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003380
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003381 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003382 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003383 if (shell->showing_input_panels)
3384 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003385 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003386 wl_list_insert(&shell->compositor->cursor_layer.link,
3387 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003388
Pekka Paalanen77346a62011-11-30 16:26:35 +02003389 launch_screensaver(shell);
3390
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003391 /* TODO: disable bindings that should not work while locked. */
3392
3393 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003394}
3395
3396static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003397unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003398{
Pekka Paalanend81c2162011-11-16 13:47:34 +02003399 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003400 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003401 return;
3402 }
3403
3404 /* If desktop-shell client has gone away, unlock immediately. */
3405 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003406 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003407 return;
3408 }
3409
3410 if (shell->prepare_event_sent)
3411 return;
3412
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003413 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003414 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003415}
3416
3417static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003418shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003419{
3420 struct desktop_shell *shell = data;
3421
3422 shell->fade.animation = NULL;
3423
3424 switch (shell->fade.type) {
3425 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003426 weston_surface_destroy(shell->fade.view->surface);
3427 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003428 break;
3429 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003430 lock(shell);
3431 break;
3432 }
3433}
3434
Jason Ekstranda7af7042013-10-12 22:38:11 -05003435static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003436shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003437{
3438 struct weston_compositor *compositor = shell->compositor;
3439 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003440 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003441
3442 surface = weston_surface_create(compositor);
3443 if (!surface)
3444 return NULL;
3445
Jason Ekstranda7af7042013-10-12 22:38:11 -05003446 view = weston_view_create(surface);
3447 if (!view) {
3448 weston_surface_destroy(surface);
3449 return NULL;
3450 }
3451
3452 weston_view_configure(view, 0, 0, 8192, 8192);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003453 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003454 wl_list_insert(&compositor->fade_layer.view_list,
3455 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003456 pixman_region32_init(&surface->input);
3457
Jason Ekstranda7af7042013-10-12 22:38:11 -05003458 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003459}
3460
3461static void
3462shell_fade(struct desktop_shell *shell, enum fade_type type)
3463{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003464 float tint;
3465
3466 switch (type) {
3467 case FADE_IN:
3468 tint = 0.0;
3469 break;
3470 case FADE_OUT:
3471 tint = 1.0;
3472 break;
3473 default:
3474 weston_log("shell: invalid fade type\n");
3475 return;
3476 }
3477
3478 shell->fade.type = type;
3479
Jason Ekstranda7af7042013-10-12 22:38:11 -05003480 if (shell->fade.view == NULL) {
3481 shell->fade.view = shell_fade_create_surface(shell);
3482 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003483 return;
3484
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 shell->fade.view->alpha = 1.0 - tint;
3486 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003487 }
3488
3489 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003490 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003491 else
3492 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003493 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003494 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003495 shell_fade_done, shell);
3496}
3497
3498static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003499do_shell_fade_startup(void *data)
3500{
3501 struct desktop_shell *shell = data;
3502
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003503 if (shell->startup_animation_type == ANIMATION_FADE)
3504 shell_fade(shell, FADE_IN);
3505 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003506 weston_surface_destroy(shell->fade.view->surface);
3507 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003508 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003509}
3510
3511static void
3512shell_fade_startup(struct desktop_shell *shell)
3513{
3514 struct wl_event_loop *loop;
3515
3516 if (!shell->fade.startup_timer)
3517 return;
3518
3519 wl_event_source_remove(shell->fade.startup_timer);
3520 shell->fade.startup_timer = NULL;
3521
3522 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3523 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3524}
3525
3526static int
3527fade_startup_timeout(void *data)
3528{
3529 struct desktop_shell *shell = data;
3530
3531 shell_fade_startup(shell);
3532 return 0;
3533}
3534
3535static void
3536shell_fade_init(struct desktop_shell *shell)
3537{
3538 /* Make compositor output all black, and wait for the desktop-shell
3539 * client to signal it is ready, then fade in. The timer triggers a
3540 * fade-in, in case the desktop-shell client takes too long.
3541 */
3542
3543 struct wl_event_loop *loop;
3544
Jason Ekstranda7af7042013-10-12 22:38:11 -05003545 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003546 weston_log("%s: warning: fade surface already exists\n",
3547 __func__);
3548 return;
3549 }
3550
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 shell->fade.view = shell_fade_create_surface(shell);
3552 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003553 return;
3554
Jason Ekstranda7af7042013-10-12 22:38:11 -05003555 weston_view_update_transform(shell->fade.view);
3556 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003557
3558 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3559 shell->fade.startup_timer =
3560 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3561 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3562}
3563
3564static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003565idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003566{
3567 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003568 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003569
3570 shell_fade(shell, FADE_OUT);
3571 /* lock() is called from shell_fade_done() */
3572}
3573
3574static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003575wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003576{
3577 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003578 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003579
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003580 unlock(shell);
3581}
3582
3583static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003584show_input_panels(struct wl_listener *listener, void *data)
3585{
3586 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003587 container_of(listener, struct desktop_shell,
3588 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003589 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003590
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003591 shell->text_input.surface = (struct weston_surface*)data;
3592
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003593 if (shell->showing_input_panels)
3594 return;
3595
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003596 shell->showing_input_panels = true;
3597
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003598 if (!shell->locked)
3599 wl_list_insert(&shell->panel_layer.link,
3600 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003601
Jason Ekstranda7af7042013-10-12 22:38:11 -05003602 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003603 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003604 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003605 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003606 wl_list_insert(&shell->input_panel_layer.view_list,
3607 &ipsurf->view->layer_link);
3608 weston_view_geometry_dirty(ipsurf->view);
3609 weston_view_update_transform(ipsurf->view);
3610 weston_surface_damage(ipsurf->surface);
3611 weston_slide_run(ipsurf->view, ipsurf->view->geometry.height,
3612 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003613 }
3614}
3615
3616static void
3617hide_input_panels(struct wl_listener *listener, void *data)
3618{
3619 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003620 container_of(listener, struct desktop_shell,
3621 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003623
Jan Arne Petersen61381972013-01-31 15:52:21 +01003624 if (!shell->showing_input_panels)
3625 return;
3626
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003627 shell->showing_input_panels = false;
3628
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003629 if (!shell->locked)
3630 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003631
Jason Ekstranda7af7042013-10-12 22:38:11 -05003632 wl_list_for_each_safe(view, next,
3633 &shell->input_panel_layer.view_list, layer_link)
3634 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003635}
3636
3637static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003638update_input_panels(struct wl_listener *listener, void *data)
3639{
3640 struct desktop_shell *shell =
3641 container_of(listener, struct desktop_shell,
3642 update_input_panel_listener);
3643
3644 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3645}
3646
3647static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003648center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003649{
Giulio Camuffob8366642013-04-25 13:57:46 +03003650 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003651 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003652
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003654
3655 x = output->x + (output->width - width) / 2 - surf_x / 2;
3656 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003657
Jason Ekstranda7af7042013-10-12 22:38:11 -05003658 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003659}
3660
3661static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003662weston_view_set_initial_position(struct weston_view *view,
3663 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003664{
3665 struct weston_compositor *compositor = shell->compositor;
3666 int ix = 0, iy = 0;
3667 int range_x, range_y;
3668 int dx, dy, x, y, panel_height;
3669 struct weston_output *output, *target_output = NULL;
3670 struct weston_seat *seat;
3671
3672 /* As a heuristic place the new window on the same output as the
3673 * pointer. Falling back to the output containing 0, 0.
3674 *
3675 * TODO: Do something clever for touch too?
3676 */
3677 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003678 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003679 ix = wl_fixed_to_int(seat->pointer->x);
3680 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003681 break;
3682 }
3683 }
3684
3685 wl_list_for_each(output, &compositor->output_list, link) {
3686 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3687 target_output = output;
3688 break;
3689 }
3690 }
3691
3692 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003693 weston_view_set_position(view, 10 + random() % 400,
3694 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003695 return;
3696 }
3697
3698 /* Valid range within output where the surface will still be onscreen.
3699 * If this is negative it means that the surface is bigger than
3700 * output.
3701 */
3702 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003703 range_x = target_output->width - view->geometry.width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003704 range_y = (target_output->height - panel_height) -
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 view->geometry.height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003706
Rob Bradford4cb88c72012-08-13 15:18:44 +01003707 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003708 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003709 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003710 dx = 0;
3711
3712 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003713 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003714 else
3715 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003716
3717 x = target_output->x + dx;
3718 y = target_output->y + dy;
3719
Jason Ekstranda7af7042013-10-12 22:38:11 -05003720 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003721}
3722
3723static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724map(struct desktop_shell *shell, struct shell_surface *shsurf,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003725 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003726{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003727 struct weston_compositor *compositor = shell->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 struct weston_view *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003729 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003730 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003731 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03003732 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003733
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734 shsurf->view->geometry.width = width;
3735 shsurf->view->geometry.height = height;
3736 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003737
3738 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003739 switch (shsurf->type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02003740 case SHELL_SURFACE_TOPLEVEL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003741 weston_view_set_initial_position(shsurf->view, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003742 break;
Alex Wu4539b082012-03-01 12:57:46 +08003743 case SHELL_SURFACE_FULLSCREEN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003744 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003745 shell_map_fullscreen(shsurf);
3746 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003747 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003748 /* use surface configure to set the geometry */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003749 panel_height = get_output_panel_height(shell, shsurf->output);
3750 surface_subsurfaces_boundingbox(shsurf->surface,
3751 &surf_x, &surf_y, NULL, NULL);
3752 weston_view_set_position(shsurf->view,
3753 shsurf->output->x - surf_x,
3754 shsurf->output->y + panel_height - surf_y);
Juan Zhao96879df2012-02-07 08:45:41 +08003755 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003756 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003757 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003758 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003759 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003760 weston_view_set_position(shsurf->view,
3761 shsurf->view->geometry.x + sx,
3762 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003763 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003764 default:
3765 ;
3766 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003767
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003768 /* surface stacking order, see also activate() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003769 switch (shsurf->type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003770 case SHELL_SURFACE_POPUP:
3771 case SHELL_SURFACE_TRANSIENT:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003772 /* TODO: Handle a parent with multiple views */
3773 parent = get_default_view(shsurf->parent);
3774 if (parent) {
3775 wl_list_remove(&shsurf->view->layer_link);
3776 wl_list_insert(parent->layer_link.prev,
3777 &shsurf->view->layer_link);
3778 }
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003779 break;
Alex Wu4539b082012-03-01 12:57:46 +08003780 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003781 case SHELL_SURFACE_NONE:
3782 break;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003783 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003784 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003785 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003786 wl_list_remove(&shsurf->view->layer_link);
3787 wl_list_insert(&ws->layer.view_list, &shsurf->view->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003788 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003789 }
3790
Jason Ekstranda7af7042013-10-12 22:38:11 -05003791 if (shsurf->type != SHELL_SURFACE_NONE) {
3792 weston_view_update_transform(shsurf->view);
3793 if (shsurf->type == SHELL_SURFACE_MAXIMIZED) {
3794 shsurf->surface->output = shsurf->output;
3795 shsurf->view->output = shsurf->output;
3796 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003797 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003798
Jason Ekstranda7af7042013-10-12 22:38:11 -05003799 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003800 /* XXX: xwayland's using the same fields for transient type */
3801 case SHELL_SURFACE_XWAYLAND:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003802 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003803 if (shsurf->transient.flags ==
3804 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3805 break;
3806 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003807 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003808 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003809 if (!shell->locked) {
3810 wl_list_for_each(seat, &compositor->seat_list, link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05003811 activate(shell, shsurf->surface, seat);
Daniel Stoneb2104682012-05-30 16:31:56 +01003812 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003813 break;
3814 default:
3815 break;
3816 }
3817
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 if (shsurf->type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003819 {
3820 switch (shell->win_animation_type) {
3821 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003822 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003823 break;
3824 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003825 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003826 break;
3827 default:
3828 break;
3829 }
3830 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003831}
3832
3833static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003834configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003835 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003836{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003837 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3838 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003839 struct weston_view *view;
Giulio Camuffob8366642013-04-25 13:57:46 +03003840 int32_t surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003841
Pekka Paalanen77346a62011-11-30 16:26:35 +02003842 shsurf = get_shell_surface(surface);
3843 if (shsurf)
3844 surface_type = shsurf->type;
3845
Jason Ekstranda7af7042013-10-12 22:38:11 -05003846 /* TODO:
3847 * This should probably be changed to be more shell_surface
3848 * dependent
3849 */
3850 wl_list_for_each(view, &surface->views, surface_link)
3851 weston_view_configure(view, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003852
3853 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003854 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003855 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003856 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003857 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003858 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003859 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03003860 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3861 NULL, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003862 shsurf->view->geometry.x = shsurf->output->x - surf_x;
3863 shsurf->view->geometry.y = shsurf->output->y +
3864 get_output_panel_height(shell,shsurf->output) - surf_y;
Juan Zhao96879df2012-02-07 08:45:41 +08003865 break;
Alex Wu4539b082012-03-01 12:57:46 +08003866 case SHELL_SURFACE_TOPLEVEL:
3867 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003868 default:
3869 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003870 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003871
Alex Wu4539b082012-03-01 12:57:46 +08003872 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003873 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003874 wl_list_for_each(view, &surface->views, surface_link)
3875 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003876
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003877 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003878 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003879 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003880}
3881
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003882static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003883shell_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 +03003884{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003885 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003886 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003887
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003888 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003889
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04003890 if (!weston_surface_is_mapped(es) &&
3891 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003892 remove_popup_grab(shsurf);
3893 }
3894
Giulio Camuffo184df502013-02-21 11:29:21 +01003895 if (width == 0)
3896 return;
3897
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003898 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003899 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003900 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003901 type_changed = 1;
3902 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003903
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003904 if (!weston_surface_is_mapped(es)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003905 map(shell, shsurf, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003906 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstranda7af7042013-10-12 22:38:11 -05003907 shsurf->view->geometry.width != width ||
3908 shsurf->view->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003909 float from_x, from_y;
3910 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003911
Jason Ekstranda7af7042013-10-12 22:38:11 -05003912 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
3913 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003914 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003915 shsurf->view->geometry.x + to_x - from_x,
3916 shsurf->view->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003917 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003918 }
3919}
3920
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003921static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003922
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003923static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003924desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003925{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003926 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003927 struct desktop_shell *shell =
3928 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003929
3930 shell->child.process.pid = 0;
3931 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003932
3933 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3934 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003935 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003936 shell->child.deathstamp = time;
3937 shell->child.deathcount = 0;
3938 }
3939
3940 shell->child.deathcount++;
3941 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003942 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003943 return;
3944 }
3945
Martin Minarik6d118362012-06-07 18:01:59 +02003946 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003947 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003948 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003949}
3950
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003951static void
3952launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003953{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003954 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003955 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003956
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003957 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003958 &shell->child.process,
3959 shell_exe,
3960 desktop_shell_sigchld);
3961
3962 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003963 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003964}
3965
3966static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003967bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3968{
Tiago Vignattibe143262012-04-16 17:31:41 +03003969 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003970 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003971
Jason Ekstranda85118c2013-06-27 20:17:02 -05003972 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3973 if (resource)
3974 wl_resource_set_implementation(resource, &shell_implementation,
3975 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003976}
3977
Kristian Høgsberg75840622011-09-06 13:48:16 -04003978static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003979unbind_desktop_shell(struct wl_resource *resource)
3980{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003981 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003982
3983 if (shell->locked)
3984 resume_desktop(shell);
3985
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003986 shell->child.desktop_shell = NULL;
3987 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003988}
3989
3990static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003991bind_desktop_shell(struct wl_client *client,
3992 void *data, uint32_t version, uint32_t id)
3993{
Tiago Vignattibe143262012-04-16 17:31:41 +03003994 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003995 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003996
Jason Ekstranda85118c2013-06-27 20:17:02 -05003997 resource = wl_resource_create(client, &desktop_shell_interface,
3998 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003999
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004000 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004001 wl_resource_set_implementation(resource,
4002 &desktop_shell_implementation,
4003 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004004 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004005
4006 if (version < 2)
4007 shell_fade_startup(shell);
4008
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004009 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004010 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004011
4012 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4013 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004014 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004015}
4016
Pekka Paalanen6e168112011-11-24 11:34:05 +02004017static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004018screensaver_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 -04004019{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004020 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004022
Giulio Camuffo184df502013-02-21 11:29:21 +01004023 if (width == 0)
4024 return;
4025
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02004026 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004027 if (!shell->locked)
4028 return;
4029
Jason Ekstranda7af7042013-10-12 22:38:11 -05004030 view = container_of(surface->views.next, struct weston_view, surface_link);
4031 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004032
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 if (wl_list_empty(&view->layer_link)) {
4034 wl_list_insert(shell->lock_layer.view_list.prev,
4035 &view->layer_link);
4036 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004037 wl_event_source_timer_update(shell->screensaver.timer,
4038 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004039 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004040 }
4041}
4042
4043static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02004044screensaver_set_surface(struct wl_client *client,
4045 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004046 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02004047 struct wl_resource *output_resource)
4048{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004049 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004050 struct weston_surface *surface =
4051 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004052 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 struct weston_view *view, *next;
4054
4055 /* Make sure we only have one view */
4056 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4057 weston_view_destroy(view);
4058 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004059
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04004060 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004061 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004062 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004063}
4064
4065static const struct screensaver_interface screensaver_implementation = {
4066 screensaver_set_surface
4067};
4068
4069static void
4070unbind_screensaver(struct wl_resource *resource)
4071{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004072 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004073
Pekka Paalanen77346a62011-11-30 16:26:35 +02004074 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004075}
4076
4077static void
4078bind_screensaver(struct wl_client *client,
4079 void *data, uint32_t version, uint32_t id)
4080{
Tiago Vignattibe143262012-04-16 17:31:41 +03004081 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004082 struct wl_resource *resource;
4083
Jason Ekstranda85118c2013-06-27 20:17:02 -05004084 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004085
Pekka Paalanen77346a62011-11-30 16:26:35 +02004086 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004087 wl_resource_set_implementation(resource,
4088 &screensaver_implementation,
4089 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004090 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02004091 return;
4092 }
4093
4094 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4095 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004096 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02004097}
4098
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004099static void
Giulio Camuffo184df502013-02-21 11:29:21 +01004100input_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 -04004101{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004102 struct input_panel_surface *ip_surface = surface->configure_private;
4103 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004104 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004105 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004106
Giulio Camuffo184df502013-02-21 11:29:21 +01004107 if (width == 0)
4108 return;
4109
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004110 if (!weston_surface_is_mapped(surface)) {
4111 if (!shell->showing_input_panels)
4112 return;
4113
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004114 show_surface = 1;
4115 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01004116
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004117 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004118
4119 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004120 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
4121 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004122 } else {
Rob Bradfordbdeb5d22013-07-11 13:20:53 +01004123 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4124 y = ip_surface->output->y + ip_surface->output->height - height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004125 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004126
Jason Ekstranda7af7042013-10-12 22:38:11 -05004127 weston_view_configure(ip_surface->view, x, y, width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004128
4129 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004130 wl_list_insert(&shell->input_panel_layer.view_list,
4131 &ip_surface->view->layer_link);
4132 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004133 weston_surface_damage(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004134 weston_slide_run(ip_surface->view, ip_surface->view->geometry.height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004135 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004136}
4137
4138static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004139destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004140{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004141 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4142
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004143 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004144 wl_list_remove(&input_panel_surface->link);
4145
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004146 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004147 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004148
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004149 free(input_panel_surface);
4150}
4151
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004152static struct input_panel_surface *
4153get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004154{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004155 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004156 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004157 } else {
4158 return NULL;
4159 }
4160}
4161
4162static void
4163input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4164{
4165 struct input_panel_surface *ipsurface = container_of(listener,
4166 struct input_panel_surface,
4167 surface_destroy_listener);
4168
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004169 if (ipsurface->resource) {
4170 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004171 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004172 destroy_input_panel_surface(ipsurface);
4173 }
4174}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004175
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004176static struct input_panel_surface *
4177create_input_panel_surface(struct desktop_shell *shell,
4178 struct weston_surface *surface)
4179{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004180 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004181
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004182 input_panel_surface = calloc(1, sizeof *input_panel_surface);
4183 if (!input_panel_surface)
4184 return NULL;
4185
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004186 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004187 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004188
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004189 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004190
4191 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004192 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004193
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004194 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004195 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004196 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004197 &input_panel_surface->surface_destroy_listener);
4198
4199 wl_list_init(&input_panel_surface->link);
4200
4201 return input_panel_surface;
4202}
4203
4204static void
4205input_panel_surface_set_toplevel(struct wl_client *client,
4206 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02004207 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004208 uint32_t position)
4209{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004210 struct input_panel_surface *input_panel_surface =
4211 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004212 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004213
4214 wl_list_insert(&shell->input_panel.surfaces,
4215 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004216
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004217 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004218 input_panel_surface->panel = 0;
4219}
4220
4221static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004222input_panel_surface_set_overlay_panel(struct wl_client *client,
4223 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004224{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004225 struct input_panel_surface *input_panel_surface =
4226 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004227 struct desktop_shell *shell = input_panel_surface->shell;
4228
4229 wl_list_insert(&shell->input_panel.surfaces,
4230 &input_panel_surface->link);
4231
4232 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004233}
4234
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004235static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004236 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02004237 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004238};
4239
4240static void
4241destroy_input_panel_surface_resource(struct wl_resource *resource)
4242{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05004243 struct input_panel_surface *ipsurf =
4244 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004245
4246 destroy_input_panel_surface(ipsurf);
4247}
4248
4249static void
4250input_panel_get_input_panel_surface(struct wl_client *client,
4251 struct wl_resource *resource,
4252 uint32_t id,
4253 struct wl_resource *surface_resource)
4254{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004255 struct weston_surface *surface =
4256 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004257 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004258 struct input_panel_surface *ipsurf;
4259
4260 if (get_input_panel_surface(surface)) {
4261 wl_resource_post_error(surface_resource,
4262 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004263 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004264 return;
4265 }
4266
4267 ipsurf = create_input_panel_surface(shell, surface);
4268 if (!ipsurf) {
4269 wl_resource_post_error(surface_resource,
4270 WL_DISPLAY_ERROR_INVALID_OBJECT,
4271 "surface->configure already set");
4272 return;
4273 }
4274
Jason Ekstranda85118c2013-06-27 20:17:02 -05004275 ipsurf->resource =
4276 wl_resource_create(client,
4277 &wl_input_panel_surface_interface, 1, id);
4278 wl_resource_set_implementation(ipsurf->resource,
4279 &input_panel_surface_implementation,
4280 ipsurf,
4281 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004282}
4283
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004284static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01004285 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004286};
4287
4288static void
4289unbind_input_panel(struct wl_resource *resource)
4290{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004291 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004292
4293 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004294}
4295
4296static void
4297bind_input_panel(struct wl_client *client,
4298 void *data, uint32_t version, uint32_t id)
4299{
4300 struct desktop_shell *shell = data;
4301 struct wl_resource *resource;
4302
Jason Ekstranda85118c2013-06-27 20:17:02 -05004303 resource = wl_resource_create(client,
4304 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004305
4306 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004307 wl_resource_set_implementation(resource,
4308 &input_panel_implementation,
4309 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004310 shell->input_panel.binding = resource;
4311 return;
4312 }
4313
4314 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4315 "interface object already bound");
4316 wl_resource_destroy(resource);
4317}
4318
Kristian Høgsberg07045392012-02-19 18:52:44 -05004319struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004320 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004321 struct weston_surface *current;
4322 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004323 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004324};
4325
4326static void
4327switcher_next(struct switcher *switcher)
4328{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004330 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004331 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004332 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004333
Jason Ekstranda7af7042013-10-12 22:38:11 -05004334 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4335 switch (get_shell_surface_type(view->surface)) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004336 case SHELL_SURFACE_TOPLEVEL:
4337 case SHELL_SURFACE_FULLSCREEN:
4338 case SHELL_SURFACE_MAXIMIZED:
4339 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004340 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004341 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 next = view->surface;
4343 prev = view->surface;
4344 view->alpha = 0.25;
4345 weston_view_geometry_dirty(view);
4346 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004347 break;
4348 default:
4349 break;
4350 }
Alex Wu1659daa2012-04-01 20:13:09 +08004351
Jason Ekstranda7af7042013-10-12 22:38:11 -05004352 if (is_black_surface(view->surface, NULL)) {
4353 view->alpha = 0.25;
4354 weston_view_geometry_dirty(view);
4355 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004356 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004357 }
4358
4359 if (next == NULL)
4360 next = first;
4361
Alex Wu07b26062012-03-12 16:06:01 +08004362 if (next == NULL)
4363 return;
4364
Kristian Høgsberg07045392012-02-19 18:52:44 -05004365 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004366 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004367
4368 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 wl_list_for_each(view, &next->views, surface_link)
4370 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004371
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004372 shsurf = get_shell_surface(switcher->current);
4373 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004375}
4376
4377static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004378switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004379{
4380 struct switcher *switcher =
4381 container_of(listener, struct switcher, listener);
4382
4383 switcher_next(switcher);
4384}
4385
4386static void
Daniel Stone351eb612012-05-31 15:27:47 -04004387switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004388{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004389 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004390 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004391 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004392
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
4394 view->alpha = 1.0;
4395 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004396 }
4397
Alex Wu07b26062012-03-12 16:06:01 +08004398 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01004399 activate(switcher->shell, switcher->current,
4400 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004401 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004402 weston_keyboard_end_grab(keyboard);
4403 if (keyboard->input_method_resource)
4404 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004405 free(switcher);
4406}
4407
4408static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004409switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004410 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004411{
4412 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004413 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004414
Daniel Stonec9785ea2012-05-30 16:31:52 +01004415 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004416 switcher_next(switcher);
4417}
4418
4419static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004420switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004421 uint32_t mods_depressed, uint32_t mods_latched,
4422 uint32_t mods_locked, uint32_t group)
4423{
4424 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01004425 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004426
Daniel Stone351eb612012-05-31 15:27:47 -04004427 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4428 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004429}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004430
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004431static void
4432switcher_cancel(struct weston_keyboard_grab *grab)
4433{
4434 struct switcher *switcher = container_of(grab, struct switcher, grab);
4435
4436 switcher_destroy(switcher);
4437}
4438
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004439static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004440 switcher_key,
4441 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004442 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004443};
4444
4445static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004446switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004447 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004448{
Tiago Vignattibe143262012-04-16 17:31:41 +03004449 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004450 struct switcher *switcher;
4451
4452 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004453 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004454 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004455 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004456 wl_list_init(&switcher->listener.link);
4457
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004458 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004459 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004460 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004461 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4462 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004463 switcher_next(switcher);
4464}
4465
Pekka Paalanen3c647232011-12-22 13:43:43 +02004466static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004467backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004468 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004469{
4470 struct weston_compositor *compositor = data;
4471 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004472 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004473
4474 /* TODO: we're limiting to simple use cases, where we assume just
4475 * control on the primary display. We'd have to extend later if we
4476 * ever get support for setting backlights on random desktop LCD
4477 * panels though */
4478 output = get_default_output(compositor);
4479 if (!output)
4480 return;
4481
4482 if (!output->set_backlight)
4483 return;
4484
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004485 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4486 backlight_new = output->backlight_current - 25;
4487 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4488 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004489
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004490 if (backlight_new < 5)
4491 backlight_new = 5;
4492 if (backlight_new > 255)
4493 backlight_new = 255;
4494
4495 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004496 output->set_backlight(output, output->backlight_current);
4497}
4498
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004499struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004500 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004501 struct weston_seat *seat;
4502 uint32_t key[2];
4503 int key_released[2];
4504};
4505
4506static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004507debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004508 uint32_t key, uint32_t state)
4509{
4510 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01004511 struct weston_compositor *ec = db->seat->compositor;
4512 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004513 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004514 uint32_t serial;
4515 int send = 0, terminate = 0;
4516 int check_binding = 1;
4517 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01004518 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004519
4520 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4521 /* Do not run bindings on key releases */
4522 check_binding = 0;
4523
4524 for (i = 0; i < 2; i++)
4525 if (key == db->key[i])
4526 db->key_released[i] = 1;
4527
4528 if (db->key_released[0] && db->key_released[1]) {
4529 /* All key releases been swalled so end the grab */
4530 terminate = 1;
4531 } else if (key != db->key[0] && key != db->key[1]) {
4532 /* Should not swallow release of other keys */
4533 send = 1;
4534 }
4535 } else if (key == db->key[0] && !db->key_released[0]) {
4536 /* Do not check bindings for the first press of the binding
4537 * key. This allows it to be used as a debug shortcut.
4538 * We still need to swallow this event. */
4539 check_binding = 0;
4540 } else if (db->key[1]) {
4541 /* If we already ran a binding don't process another one since
4542 * we can't keep track of all the binding keys that were
4543 * pressed in order to swallow the release events. */
4544 send = 1;
4545 check_binding = 0;
4546 }
4547
4548 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004549 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4550 key, state)) {
4551 /* We ran a binding so swallow the press and keep the
4552 * grab to swallow the released too. */
4553 send = 0;
4554 terminate = 0;
4555 db->key[1] = key;
4556 } else {
4557 /* Terminate the grab since the key pressed is not a
4558 * debug binding key. */
4559 send = 1;
4560 terminate = 1;
4561 }
4562 }
4563
4564 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01004565 serial = wl_display_next_serial(display);
4566 resource_list = &grab->keyboard->focus_resource_list;
4567 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004568 wl_keyboard_send_key(resource, serial, time, key, state);
4569 }
4570 }
4571
4572 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004573 weston_keyboard_end_grab(grab->keyboard);
4574 if (grab->keyboard->input_method_resource)
4575 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004576 free(db);
4577 }
4578}
4579
4580static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004581debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004582 uint32_t mods_depressed, uint32_t mods_latched,
4583 uint32_t mods_locked, uint32_t group)
4584{
4585 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01004586 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004587
Neil Roberts96d790e2013-09-19 17:32:00 +01004588 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004589
Neil Roberts96d790e2013-09-19 17:32:00 +01004590 wl_resource_for_each(resource, resource_list) {
4591 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4592 mods_latched, mods_locked, group);
4593 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004594}
4595
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004596static void
4597debug_binding_cancel(struct weston_keyboard_grab *grab)
4598{
4599 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4600
4601 weston_keyboard_end_grab(grab->keyboard);
4602 free(db);
4603}
4604
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004605struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004606 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004607 debug_binding_modifiers,
4608 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004609};
4610
4611static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004612debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004613{
4614 struct debug_binding_grab *grab;
4615
4616 grab = calloc(1, sizeof *grab);
4617 if (!grab)
4618 return;
4619
4620 grab->seat = (struct weston_seat *) seat;
4621 grab->key[0] = key;
4622 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004623 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004624}
4625
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004626static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004627force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004628 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004629{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004630 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004631 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004632 struct desktop_shell *shell = data;
4633 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004634 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004635
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004636 focus_surface = seat->keyboard->focus;
4637 if (!focus_surface)
4638 return;
4639
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004640 wl_signal_emit(&compositor->kill_signal, focus_surface);
4641
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004642 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004643 wl_client_get_credentials(client, &pid, NULL, NULL);
4644
4645 /* Skip clients that we launched ourselves (the credentials of
4646 * the socketpair is ours) */
4647 if (pid == getpid())
4648 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004649
Daniel Stone325fc2d2012-05-30 16:31:58 +01004650 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004651}
4652
4653static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004654workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004655 uint32_t key, void *data)
4656{
4657 struct desktop_shell *shell = data;
4658 unsigned int new_index = shell->workspaces.current;
4659
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004660 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004661 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004662 if (new_index != 0)
4663 new_index--;
4664
4665 change_workspace(shell, new_index);
4666}
4667
4668static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004669workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004670 uint32_t key, void *data)
4671{
4672 struct desktop_shell *shell = data;
4673 unsigned int new_index = shell->workspaces.current;
4674
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004675 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004676 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004677 if (new_index < shell->workspaces.num - 1)
4678 new_index++;
4679
4680 change_workspace(shell, new_index);
4681}
4682
4683static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004684workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004685 uint32_t key, void *data)
4686{
4687 struct desktop_shell *shell = data;
4688 unsigned int new_index;
4689
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004690 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004691 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004692 new_index = key - KEY_F1;
4693 if (new_index >= shell->workspaces.num)
4694 new_index = shell->workspaces.num - 1;
4695
4696 change_workspace(shell, new_index);
4697}
4698
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004699static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004700workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004701 uint32_t key, void *data)
4702{
4703 struct desktop_shell *shell = data;
4704 unsigned int new_index = shell->workspaces.current;
4705
4706 if (shell->locked)
4707 return;
4708
4709 if (new_index != 0)
4710 new_index--;
4711
4712 take_surface_to_workspace_by_seat(shell, seat, new_index);
4713}
4714
4715static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004716workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004717 uint32_t key, void *data)
4718{
4719 struct desktop_shell *shell = data;
4720 unsigned int new_index = shell->workspaces.current;
4721
4722 if (shell->locked)
4723 return;
4724
4725 if (new_index < shell->workspaces.num - 1)
4726 new_index++;
4727
4728 take_surface_to_workspace_by_seat(shell, seat, new_index);
4729}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004730
4731static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004732handle_output_destroy(struct wl_listener *listener, void *data)
4733{
4734 struct shell_output *output_listener =
4735 container_of(listener, struct shell_output, destroy_listener);
4736
4737 wl_list_remove(&output_listener->destroy_listener.link);
4738 wl_list_remove(&output_listener->link);
4739 free(output_listener);
4740}
4741
4742static void
4743create_shell_output(struct desktop_shell *shell,
4744 struct weston_output *output)
4745{
4746 struct shell_output *shell_output;
4747
4748 shell_output = zalloc(sizeof *shell_output);
4749 if (shell_output == NULL)
4750 return;
4751
4752 shell_output->output = output;
4753 shell_output->shell = shell;
4754 shell_output->destroy_listener.notify = handle_output_destroy;
4755 wl_signal_add(&output->destroy_signal,
4756 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004757 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004758}
4759
4760static void
4761handle_output_create(struct wl_listener *listener, void *data)
4762{
4763 struct desktop_shell *shell =
4764 container_of(listener, struct desktop_shell, output_create_listener);
4765 struct weston_output *output = (struct weston_output *)data;
4766
4767 create_shell_output(shell, output);
4768}
4769
4770static void
4771setup_output_destroy_handler(struct weston_compositor *ec,
4772 struct desktop_shell *shell)
4773{
4774 struct weston_output *output;
4775
4776 wl_list_init(&shell->output_list);
4777 wl_list_for_each(output, &ec->output_list, link)
4778 create_shell_output(shell, output);
4779
4780 shell->output_create_listener.notify = handle_output_create;
4781 wl_signal_add(&ec->output_created_signal,
4782 &shell->output_create_listener);
4783}
4784
4785static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004786shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004787{
Tiago Vignattibe143262012-04-16 17:31:41 +03004788 struct desktop_shell *shell =
4789 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004790 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004791 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004792
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004793 if (shell->child.client)
4794 wl_client_destroy(shell->child.client);
4795
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004796 wl_list_remove(&shell->idle_listener.link);
4797 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004798 wl_list_remove(&shell->show_input_panel_listener.link);
4799 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004800
Xiong Zhang6b481422013-10-23 13:58:32 +08004801 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4802 wl_list_remove(&shell_output->destroy_listener.link);
4803 wl_list_remove(&shell_output->link);
4804 free(shell_output);
4805 }
4806
4807 wl_list_remove(&shell->output_create_listener.link);
4808
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004809 wl_array_for_each(ws, &shell->workspaces.array)
4810 workspace_destroy(*ws);
4811 wl_array_release(&shell->workspaces.array);
4812
Pekka Paalanen3c647232011-12-22 13:43:43 +02004813 free(shell->screensaver.path);
4814 free(shell);
4815}
4816
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004817static void
4818shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4819{
4820 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004821 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004822
4823 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4825 MODIFIER_CTRL | MODIFIER_ALT,
4826 terminate_binding, ec);
4827 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4828 click_to_activate_binding,
4829 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004830 weston_compositor_add_touch_binding(ec, 0,
4831 touch_to_activate_binding,
4832 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004833 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4834 MODIFIER_SUPER | MODIFIER_ALT,
4835 surface_opacity_binding, NULL);
4836 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4837 MODIFIER_SUPER, zoom_axis_binding,
4838 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004839
4840 /* configurable bindings */
4841 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004842 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4843 zoom_key_binding, NULL);
4844 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4845 zoom_key_binding, NULL);
4846 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4847 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004848 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004849 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4850 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004851
4852 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4853 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4854 rotate_binding, NULL);
4855
Daniel Stone325fc2d2012-05-30 16:31:58 +01004856 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4857 shell);
4858 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4859 ec);
4860 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4861 backlight_binding, ec);
4862 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4863 ec);
4864 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4865 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004866 weston_compositor_add_key_binding(ec, KEY_K, mod,
4867 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004868 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4869 workspace_up_binding, shell);
4870 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4871 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004872 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4873 workspace_move_surface_up_binding,
4874 shell);
4875 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4876 workspace_move_surface_down_binding,
4877 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004878
4879 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4880 if (shell->workspaces.num > 1) {
4881 num_workspace_bindings = shell->workspaces.num;
4882 if (num_workspace_bindings > 6)
4883 num_workspace_bindings = 6;
4884 for (i = 0; i < num_workspace_bindings; i++)
4885 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4886 workspace_f_binding,
4887 shell);
4888 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004889
4890 /* Debug bindings */
4891 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4892 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004893}
4894
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004895WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004896module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004897 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004898{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004899 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004900 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004901 struct workspace **pws;
4902 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004903 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004904
Peter Huttererf3d62272013-08-08 11:57:05 +10004905 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004906 if (shell == NULL)
4907 return -1;
4908
Kristian Høgsberg75840622011-09-06 13:48:16 -04004909 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004910
4911 shell->destroy_listener.notify = shell_destroy;
4912 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004913 shell->idle_listener.notify = idle_handler;
4914 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4915 shell->wake_listener.notify = wake_handler;
4916 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004917 shell->show_input_panel_listener.notify = show_input_panels;
4918 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4919 shell->hide_input_panel_listener.notify = hide_input_panels;
4920 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004921 shell->update_input_panel_listener.notify = update_input_panels;
4922 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004923 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004924 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004925 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004926 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004927 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004928 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004929 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004930 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004931 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004932 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02004933 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004934
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004935 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004936
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004937 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4938 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004939 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4940 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004941 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004942
4943 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004944 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004945
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004946 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004947
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004948 for (i = 0; i < shell->workspaces.num; i++) {
4949 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4950 if (pws == NULL)
4951 return -1;
4952
4953 *pws = workspace_create();
4954 if (*pws == NULL)
4955 return -1;
4956 }
4957 activate_workspace(shell, 0);
4958
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004959 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004960 wl_list_init(&shell->workspaces.animation.link);
4961 shell->workspaces.animation.frame = animate_workspace_change_frame;
4962
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004963 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004964 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004965 return -1;
4966
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004967 if (wl_global_create(ec->wl_display,
4968 &desktop_shell_interface, 2,
4969 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004970 return -1;
4971
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004972 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4973 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02004974 return -1;
4975
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004976 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004977 shell, bind_input_panel) == NULL)
4978 return -1;
4979
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004980 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4981 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02004982 return -1;
4983
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004984 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004985
Xiong Zhang6b481422013-10-23 13:58:32 +08004986 setup_output_destroy_handler(ec, shell);
4987
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004988 loop = wl_display_get_event_loop(ec->wl_display);
4989 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004990
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004991 shell->screensaver.timer =
4992 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4993
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004994 wl_list_for_each(seat, &ec->seat_list, link)
4995 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004996
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004997 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004998
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004999 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005000
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005001 return 0;
5002}